2

I've implemented some python code to run search on whoosh using BM25 and all went out ok, but now that I'm trying to change scoring mechanism to Cosine, I'm getting this error:

File "TFIDF.py", line 18, in tfidf with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher: AttributeError: 'module' object has no attribute 'Cosine'

If I import Cosine

from whoosh.scoring import Cosine

I get this:

File "TFIDF.py", line 4, in <module>
    from whoosh.scoring import Cosine
ImportError: cannot import name Cosine

My code is below:

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

Any ideias???

Thank you!

David Dias
  • 1,792
  • 3
  • 16
  • 28
  • Did you find a way to do cosine scoring in Whoosh? – Nathan Breit Sep 18 '14 at 09:42
  • Yes, but honestly I'm not remembering totally what was the catch, but you can find the code of it working here: https://github.com/diasdavid/eadw-ist/blob/master/src/newsSearch/TFIDF.py if I'm not wrong TFIDF===cosine – David Dias Sep 19 '14 at 10:56
  • Thanks for replying, but I don't see the cosine scoring formula in your code or in Whoosh's TF_IDF implementation. – Nathan Breit Sep 23 '14 at 05:42

1 Answers1

0

The documentation on http://pythonhosted.org/Whoosh/searching.html#the-searcher-object, which I assume is what you're looking at is incorrect as you have found. Look at the documentation (http://pythonhosted.org/Whoosh/api/scoring.html#module-whoosh.scoring) or the source code (https://bitbucket.org/mchaput/whoosh/src/362fc2999c8cabc51370f433de7402fafd536ec6/src/whoosh/scoring.py?at=default) for the options actually available.

boxed
  • 3,895
  • 2
  • 24
  • 26