统计lucene索引中的词频

发布于 2024-10-01 15:36:50 字数 105 浏览 1 评论 0原文

有人可以帮我找到所有lucene索引中的词频
例如,如果文档 A 有 3 个单词 (B),文档 C 有 2 个,我想要一个方法返回 5,显示单词 (B) 在所有 lucene 索引中的频率

Can someone help me finding the word frequency in all lucene index
for example if doc A has 3 number of word (B) and doc C has 2 of them, I'd like a method to return 5 showing the frequency of word (B) in all lucene index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-10-08 15:36:50

假设您使用 Lucene 3.x:

IndexReader ir = IndexReader.open(dir); 
TermDocs termDocs = ir.termDocs(new Term("your_field", "your_word"));
int count = 0;
while (termDocs.next()) {
   count += termDocs.freq();
}

一些注释:

dir 是 Lucene 目录类。 RAM 和文件系统索引的创建有所不同,详细信息请参阅 Lucene 文档。

“your_filed”是搜索术语的字段。如果您有多个字段,您可以为所有字段运行过程,或者,当您索引文件时,您可以创建特殊字段(例如“_content”)并保留所有其他字段的串联值。

Assuming you work with Lucene 3.x:

IndexReader ir = IndexReader.open(dir); 
TermDocs termDocs = ir.termDocs(new Term("your_field", "your_word"));
int count = 0;
while (termDocs.next()) {
   count += termDocs.freq();
}

Some comments:

dir is the instance of Lucene Directory class. It's creation differs for RAM and Filesystem indexes, see Lucene documentation for details.

"your_filed" is a filed to search a term. If you have multiple fields, you can run procedure for all of them or, alternatively, when you index your files, you can create special field (e.g. "_content") and keep there concatenated values of all other fields.

血之狂魔 2024-10-08 15:36:50

使用 lucene 3.4

简单的方法来获取计数,但是您需要两个数组:-/

int[] docs = new int[1000];
int[] freqs = new int[1000];
int count = indexReader.termDocs(term).read(docs, freqs);

注意:如果您将用于读取,您将无法再使用 next() ,因为在 read() 之后您已经位于末尾枚举的:

int[] docs = new int[1000];
int[] freqs = new int[1000];
TermDocs td = indexReader.termDocs(term);
int count = td.read(docs, freqs);
while (td.next()){ // always false, already at the end of the enumartion
}

using lucene 3.4

easy way to get the count, but you need two arrays :-/

int[] docs = new int[1000];
int[] freqs = new int[1000];
int count = indexReader.termDocs(term).read(docs, freqs);

beware: if you would use for read you are not able to use next() any more, because after the read() you are already at the end of the enumeration:

int[] docs = new int[1000];
int[] freqs = new int[1000];
TermDocs td = indexReader.termDocs(term);
int count = td.read(docs, freqs);
while (td.next()){ // always false, already at the end of the enumartion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文