Spaces:
Sleeping
Sleeping
from whoosh.qparser import QueryParser | |
from whoosh.index import open_dir | |
import re | |
from whoosh.query import Term | |
ix = open_dir("indexdir", indexname='article_index') | |
with ix.searcher() as searcher: | |
query = QueryParser("原文", ix.schema).parse("季氏旅于泰山。") | |
results = searcher.search(query) | |
if results: | |
hit = results[0] | |
print("原始命中记录:", hit) | |
批判文本 = hit.get("批判", "") | |
matches = re.findall(r'\d+[\·\.]\d+', 批判文本) | |
print("匹配到的 index 值:", matches) | |
map_hit = dict(hit) | |
map_hit['extra'] = [] | |
for index_ref in matches: | |
index_ref_normalized = index_ref.replace( '.' , '·') | |
term_query = Term("index", index_ref_normalized) | |
related_results = searcher.search(term_query) | |
for related_hit in related_results: | |
print(f"\n关联 index {index_ref_normalized} 的记录:") | |
map_hit['extra'].append(dict(related_hit)) | |
print(map_hit) | |