1

I am using using below code for searching text for documents using whoosh python library please help me how to get search result if any one use synonym words in query text search. Please help me for search synonyms text search. what line of code i need to add for searching synonyms words are available in text?

from whoosh.qparser import QueryParser
from whoosh import index
ix = index.open_dir("indexdir")

with ix.searcher() as searcher:

     query = QueryParser("content", ix.schema).parse("treatment of lung cancer in early stages")
     results = searcher.search(query, terms=True)

    for r in results:
        print(r, r.score)
         # Was this results object created with terms=True?
        if results.has_matched_terms():
           # What terms matched in the results?
           print(results.matched_terms())

       # What terms matched in each hit?
print("matched terms")
for hit in results:
    print(hit.matched_terms())  

1 Answers1

0
from whoosh.qparser import QueryParser
from whoosh import index

def post(self,search_string):
    from whoosh import query
    from whoosh.lang.wordnet import Thesaurus
    f = open("C:\\Users\\prakhar\\Downloads\\prolog\\wn_s.pl")
    t = Thesaurus.from_file(f)
    synonsm_stream = []
    stream_list = search_string.split(" ")
    for token in stream_list:
        m = t.synonyms(token)
        m.append(token)
        synonsm_stream.append(" OR ".join(m))
    synonym_string = " ".join(synonsm_stream)

    ix = index.open_dir("indexdir")
    with ix.searcher() as searcher:
        query = QueryParser("paragraph", ix.schema, termclass=query.Variations).parse(synonym_string)
        # "treatment of lung cancer in early stages"
        results = searcher.search(query, terms=True, )
  • Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. – ChrisGPT was on strike May 07 '20 at 19:39