繁体   English   中英

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

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

我正在使用以下代码使用 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())  
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, )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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