【问题标题】:how to print link in file and later using it to reference terms in another file in java如何在文件中打印链接,然后在java中使用它来引用另一个文件中的术语
【发布时间】:2016-11-25 10:29:58
【问题描述】:
  1. 您需要编写一个程序反转来进行索引构建。程序的输入是文档集合。输出包括两个文件 - 一个字典文件和一个发布列表文件。字典中的每个条目都应包含一个术语、其文档频率和指向其发布列表的链接。您应该使用适当的数据结构来构建字典(例如散列或搜索树或其他)。该结构应该易于随机查找和插入新术语。所有术语应按字母顺序排序。每个term的postings列表应该包括该term出现的所有文档的posting(按照文档ID的顺序),并且posting中保存的信息包括文档ID、文档中的词频以及该词在文档中所有出现的位置文件。

我已经创建了这两个文件并读取了每个术语并将它们保存在这个哈希图中

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

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

【问题讨论】:

  • 这太长了。我会更多地隔离问题,以便人们更好地消化您的问题。
  • 根据要求我已经隔离了问题

标签: java information-retrieval


【解决方案1】:

这几乎是一个数据库风格的问题,其中您有由键表示的实体。因此,对于您为代表手头项目而创建的每个主要类,请考虑它们的索引。

假设你有类似的东西:

// 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&lt;int&gt; postingIds 将通过其postingId 键用于引用帖子。

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

使用Jackson ObjectMapper,当您阅读发布文件时,您将拥有大量Posting 对象,可以调用getPostingId(),然后将其与TermMetadata 中的信息结合起来List&lt;int&gt;postingIds`。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2011-05-15
    • 2020-02-08
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多