简体   繁体   中英

Python Whoosh library also give search result if any one using synonyms words in given query text

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())  
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, )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM