如何在文件中打印 link 并稍后使用它来引用 java 中另一个文件中的术语

how to print link in file and later using it to reference terms in another file in java

  1. 你需要写一个程序invert来做索引构建。程序的输入是文档集合。输出包括两个文件 - 一个字典文件和一个帖子列表文件。字典中的每个条目都应该包括一个术语、它的文档频率和一个 link 到它的发布列表。您应该使用适当的数据结构来构建字典(例如散列或搜索树或其他)。该结构应该易于随机查找和插入新术语。所有术语应按字母顺序排序。每个term的postings列表应该包含该term出现的所有文档的posting(按文档ID排序),一个posting中保存的信息包括文档ID,term在文档中出现的频率,term在文档中出现的位置文档。

我已经创建了两个文件并阅读了每个术语并将它们保存在这个 hashmap 中

    private static HashMap<dictionary, List<postings>> index = new HashMap<dictionary , List<postings>>();

我如何创建这个 link 因为对于第二个程序我 运行 它包含从 invert.java 创建的两个 txt 文件 我的 dictionary.txt 仅包含术语和频率,我该如何创建此 link?

这几乎是一个数据库风格的问题,其中实体由键表示。因此,对于您创建的每个专业 class 来代表手头的项目,想一想它们的索引是什么。

假设你有类似的东西:

// use Jackson to read/write your data files in Json format
import com.fasterxml.jackson.databind.ObjectMapper;

public class Inverter {
    private JsonFileWriter fileWriter;

    /*
     * Don't use HashMap since it isn't sorted
     */
    Map<String, TermMetadata> dictionary = new HashMap<>();

    public List<File> invert(final Collection<Document> documents) {
    }

    public void writeDictionation() {
        fileWriter.write(dictionary);
    }

    public void writePostings() {
        fileWriter.write(postings);
    }
}

public class Document {
    /**
     * Index is the documentId
     */
    int documentId;

    // other attributes ...
}

public class TermMetadata {
    /*
     * Index on the term
     */
    String term;
    int documentFrequency;
    List<int> postingsIds;
}

public class Posting {
    /**
     * Index on the posting id
     */
    int postingId;
    int documentId;
    int termFrequency;
    List<int> positions;
}

然后,无论您从文本文件中读回哪些值,都可以加入索引。

因此,TermMetadata 上的 List<int> postingIds 将用于通过 postingId 键来引用帖子。

我认为理想情况下,这将作为数据库的 Java 前端实现。但是,由于您使用的是文件,我建议使用一些标准的序列化机制,如 Json 来存储对象表示。

使用 Jackson ObjectMapper,当你去阅读帖子文件时,你将有一个很大的 Posting 对象列表,可以调用 getPostingId() 然后将其加入TermMetadataList<int> postingIds 中的信息。