我已经实现了一些python代码,可以使用BM25在whoosh上运行搜索,并且一切正常,但是现在我试图将评分机制更改为Cosine,我得到了这个错误:
文件"TFIDF.py",第18行,在tfidf中使用TFIDF.py作为搜索器: AttributeError:‘模块’对象没有属性‘余弦’
如果我进口余弦
from whoosh.scoring import Cosine我明白了:
File "TFIDF.py", line 4, in <module>
from whoosh.scoring import Cosine
ImportError: cannot import name Cosine我的代码如下:
import whoosh
from whoosh.scoring import Cosine
from whoosh.fields import *
from whoosh.scoring import *
from whoosh.qparser import *
from whoosh.query import *
from whoosh.index import open_dir
#Index path
lab3dir= "../lab3/Aula3_1/"
ix = open_dir(lab3dir + "indexdir") #Index generated in previous lab
def cosine(queryTerms):
list=[]
dict={} # dict com ID do doc e Similiaridade
with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher:
query = QueryParser("content", ix.schema, group=OrGroup).parse(u(queryTerms))
results = searcher.search(query, limit=100)
for i,r in enumerate(results):
list.append(r["id"])
print r, results.score(i)
dict[r["id"]]= results.score(i)
return dict有什么想法吗?
谢谢!
发布于 2013-03-26 20:57:03
关于http://pythonhosted.org/Whoosh/searching.html#the-searcher-object的文档(我认为这是您正在查看的内容)与您发现的不正确。查看实际可用选项的文档(http://pythonhosted.org/Whoosh/api/scoring.html#module-whoosh.scoring)或源代码(https://bitbucket.org/mchaput/whoosh/src/362fc2999c8cabc51370f433de7402fafd536ec6/src/whoosh/scoring.py?at=default)。
https://stackoverflow.com/questions/15646767
复制相似问题