我的输出没有提供与查询匹配的文档

2024-06-17 08:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为pads的文件夹,其中有六个记事本文档,每个文档中都有一些文本。我正在尝试构建一个whoosh代码,它将返回查询字符串的相应文档,但是我正在以运行时的形式获得输出,非常感谢

import os
from whoosh.index import create_in
from whoosh.fields import Schema, TEXT, ID
import sys
from whoosh.qparser import QueryParser
from whoosh import scoring
from whoosh.index import open_dir

def createSearchableData(root):   

'''
Schema definition: title(name of file), path(as ID), content(indexed
but not stored),textdata (stored text content)
'''
    schema = Schema(title=TEXT(stored=True),path=ID(stored=True),\
          content=TEXT,textdata=TEXT(stored=True))
    if not os.path.exists("indexdir"):
        os.mkdir("indexdir")

# Creating a index writer to add document as per schema
    ix = create_in("indexdir",schema)
    writer = ix.writer()

    filepaths = [os.path.join(root,i) for i in os.listdir(root)]
    for path in filepaths:
        fp = open(path,'r')
        print(path)
        text = fp.read()
        writer.add_document(title=path.split("\\")[0], path=path,\
          content=text,textdata=text)
        fp.close()
    writer.commit()

root = "pads"
createSearchableData(root)

---输出--- 垫子/5.txt 垫子/4.txt pads/6.txt文件 垫子/3.txt pads/2.txt文件 pads/1.txt文件

ix = open_dir("indexdir")
query_str = 'barzini'
# Top 'n' documents as result
topN = 3

qp = QueryParser("content", ix.schema)
q = qp.parse(query_str)

with ix.searcher() as searcher:
    results = searcher.search(q,limit=topN)
print(results)   

---输出--- Term('content','barzini')运行时的前1个结果=0.00048629400043864734>

我希望输出从Pad文件夹返回4.txt,因为它有字符串“barzini”。你能帮我查一下输出吗


Tags: pathtextinfromimporttxtosas