如果在给定的查询文本中使用同义词单词,Python Whoosh库也会给出搜索结果

问题描述 投票:1回答:1

我正在使用下面的代码使用Whoosh python库在文档中搜索文本,如果有人在查询文本搜索中使用同义词,请帮助我如何获取搜索结果。请帮助我进行搜索同义词文本搜索。我需要在搜索中添加哪几行代码来查找同义词,在文本中是否可用?

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())  
python whoosh
1个回答
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, )
© www.soinside.com 2019 - 2024. All rights reserved.