我目前正在使用PyLucene,但是由于没有它的文档,我想用Java的解决方案也可以(但是如果有人在Python中有这样的解决方案,那就更好了)。
我正在研究科学出版物,现在,我检索其中的关键词。但是,对于某些文档来说,根本没有关键字。另一种替代方法是获得N个单词(5-8),其TFIDF得分最高。
我不知道怎么做,也不知道什么时候。我的意思是:我是否必须在索引阶段告诉Lucene来计算这些值,在搜索索引时可以这样做。
对于每个查询,我想要的内容如下:
Query Ranking
Document1, top 5 TFIDF terms, Lucene score (default TFIDF)
Document2, " " , " "
...
还可以首先检索查询的排名,然后计算每个文档的前5个TF国防军术语。
有人知道我该怎么做吗?
发布于 2016-08-17 10:34:18
在邮件列表中查了一下之后,我最终得到了我想要的东西。
下面是我想出的方法:
def getTopTFIDFTerms(docID, reader):
termVector = reader.getTermVector(docID, "contents");
termsEnumvar = termVector.iterator(None)
termsref = BytesRefIterator.cast_(termsEnumvar)
tc_dict = {} # Counts of each term
dc_dict = {} # Number of docs associated with each term
tfidf_dict = {} # TF-IDF values of each term in the doc
N_terms = 0
try:
while (termsref.next()):
termval = TermsEnum.cast_(termsref)
fg = termval.term().utf8ToString() # Term in unicode
tc = termval.totalTermFreq() # Term count in the doc
# Number of docs having this term in the index
dc = reader.docFreq(Term("contents", termval.term()))
N_terms = N_terms + 1
tc_dict[fg]=tc
dc_dict[fg]=dc
except:
print 'error in term_dict'
# Compute TF-IDF for each term
for term in tc_dict:
tf = tc_dict[term] / N_terms
idf = 1 + math.log(N_DOCS_INDEX/(dc_dict[term]+1))
tfidf_dict[term] = tf*idf
# Here I get a representation of the sorted dictionary
sorted_x = sorted(tfidf_dict.items(), key=operator.itemgetter(1), reverse=True)
# Get the top 5
top5 = [i[0] for i in sorted_x[:5]] # replace 5 by TOP N
我不知道为什么我必须将termsEnum
转换为一个BytesRefIterator
,我从邮件列表中的一个线程中得到了这个消息,它可以找到这里。
希望这会有所帮助:)
发布于 2016-08-17 01:16:03
如果字段为索引,则可以使用getTerms检索文档频率。如果字段具有存储项向量,则可以使用getTermVector检索术语频率。
我还建议查看MoreLikeThis,它使用tf*国防军创建一个类似于文档的查询,您可以从中提取术语。
如果你想要更多的pythonic界面,那就是我做卢平的动机
from lupyne import engine
searcher = engine.IndexSearcher(<filepath>)
df = dict(searcher.terms(<field>, counts=True))
tf = dict(searcher.termvector(<docnum>, <field>, counts=True))
query = searcher.morelikethis(<docnum>, <field>)
https://stackoverflow.com/questions/38976466
复制相似问题