当前位置: 首页 > 面试题库 >

如何在Lucene 3.5.0中提取文档术语向量

尹何平
2023-03-14
问题内容

我正在使用Lucene 3.5.0,我想输出每个文档的术语向量。例如,我想知道所有文档和每个特定文档中术语的出现频率。我的索引代码是:

import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexer {
public static void main(String[] args) throws Exception {
        if (args.length != 2) {
        throw new IllegalArgumentException("Usage: java " + Indexer.class.getName() + " <index dir> <data dir>");
    }

    String indexDir = args[0];
    String dataDir = args[1];
    long start = System.currentTimeMillis();
    Indexer indexer = new Indexer(indexDir);
    int numIndexed;
    try {
        numIndexed = indexer.index(dataDir, new TextFilesFilter());
    } finally {
        indexer.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
}

private IndexWriter writer;

public Indexer(String indexDir) throws IOException {
    Directory dir = FSDirectory.open(new File(indexDir));
    writer = new IndexWriter(dir,
        new StandardAnalyzer(Version.LUCENE_35),
        true,
        IndexWriter.MaxFieldLength.UNLIMITED);
}

public void close() throws IOException {
    writer.close();
}

public int index(String dataDir, FileFilter filter) throws Exception {
    File[] files = new File(dataDir).listFiles();
    for (File f: files) {
        if (!f.isDirectory() &&
        !f.isHidden() &&
        f.exists() &&
        f.canRead() &&
        (filter == null || filter.accept(f))) {
            BufferedReader inputStream = new BufferedReader(new FileReader(f.getName()));
            String url = inputStream.readLine();
            inputStream.close();
            indexFile(f, url);
        }
    }
    return writer.numDocs();
}

private static class TextFilesFilter implements FileFilter {
    public boolean accept(File path) {
        return path.getName().toLowerCase().endsWith(".txt");
    }
}

protected Document getDocument(File f, String url) throws Exception {
    Document doc = new Document();
    doc.add(new Field("contents", new FileReader(f)));
    doc.add(new Field("urls", url, Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    return doc;
}

private void indexFile(File f, String url) throws Exception {
    System.out.println("Indexing " + f.getCanonicalPath());
    Document doc = getDocument(f, url);
    writer.addDocument(doc);
}
}

有人可以帮助我编写程序来做到这一点吗?谢谢。


问题答案:

首先,您不需要存储术语向量就可以只知道文档中术语的频率。Lucene仍然存储这些数字以用于TF-
IDF计算。您可以通过调用IndexReader.termDocs(term)并遍历结果来访问此信息。

如果您有其他目的,并且实际上需要访问术语向量,则需要通过Field.TermVector.YES作为Field构造函数的最后一个参数传递来告诉Lucene存储它们。然后,您可以使用检索向量IndexReader.getTermFreqVector()



 类似资料:
  • 问题内容: 如何 使用PHP 从PDF文档中提取文本? (我不能使用其他工具,我没有root用户访问权限) 我发现一些函数可用于纯文本,但是它们不能很好地处理Unicode字符: http://www.hashbangcode.com/blog/zend-lucene-and-pdf-documents-part-2-pdf- data-extraction-437.html 问题答案: 下载 c

  • 根据弹性搜索留档,https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html,术语向量只能应用于文档。有没有什么方法可以在索引级别应用它。 我的用例是在应用英语非索引过滤器后,计算添加到索引的所有文档中存在的字段(类型:字符串,基本上是句子)中存在的所有uni,bi和三元组的频率。 谢谢

  • 问题内容: 我是相当新的elasticsearch,使用6.5版。我的数据库包含网站页面及其内容,如下所示: 我已经能够执行一个简单的查询,该查询返回所有内容中包含“汽车”一词的文档(使用Python): 结果看起来像这样: “ _id”指的是一个域,所以我基本上回来了: abc.com def.com jkl.com 但我现在想知道如何往往是搜索关键词(“汽车”)出现 在 每个文档,如: abc

  • Lucene提到- 但是我们可以通过IndexWriter.setMaxFieldLength(int)对其进行配置。 我在ElasticSearch-http://localhost:9200/twitter中创建了一个索引,并发布了一个包含40,000个术语的文档。 映射- 我用message字段索引了一个文档,有40,000个术语-message:“text1text2....text400

  • 问题内容: 我需要遍历Lucene索引中的所有文档,并获取每个术语在每个文档中出现的位置。据我能从Lucene javadoc所了解的,做到这一点的方法是做这样的事情: 但是,即使(1)索引的确包含相关字段上的位置,并且(2)术语向量声称具有位置(即:tv.hasPositions()== true),我仍会为所有变量获取“ -1”职位。 首先,我做错什么了吗?是否有其他方法可以按文档迭代发布?第

  • 我正在尝试从Lucene索引中删除文档。我只想从lucene索引中删除指定的文件。 我下面的程序是删除可以使用关键字分析器搜索的索引,但是我需要的文件名只能使用标准分析器搜索。因此,它是任何方式设置标准分析器在我的任期或代替任期如何使用QueryParser从Lucene索引删除文档。