gpt4 book ai didi

python - 嗖嗖功能

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:47 25 4
gpt4 key购买 nike

我有超过 700 个本地存储的小型文本文件(大小不超过 2Kb)。每个文件都位于一个文件夹中,例如:

  • 2012//05//18.txt
  • 2013//09/22.txt
  • 2014//11//29.txt

(每个文件代表一天)。

内容包含每日“事件”信息。我感兴趣的是能够找到包含特定单词的文件。

  1. 文件可以通过 whoosh 批量索引吗?也就是说,可以编写 python/whoosh 代码来索引一组文件吗?
  2. 如果是这样,可以编写一个 python 脚本来搜索每个文件中的特定单词吗?
  3. 如果这不是 whoosh 的功能,或者 whoosh 对于此类任务来说太过分了,那么什么可以更好地完成此任务?

最佳答案

前言:我从来没有用过“whoosh”。我刚刚读了quickstart

看起来这完成起来相当简单。您必须做的工作的关键在于:

writer.add_document(title=u"First document", path=u"/a", content=u"This is the first document we've added!")

这是实际创建索引的地方。本质上,您似乎必须做的是使用类似 os.walk 的东西。抓取根目录并填写数据,并为找到的每个文件填写 writer.add_document 参数。

for root, dirs, files in os.walk("/your/path/to/logs"):
for file in files:
filepath = os.path.join(root, file)
with open(filepath, 'r') as content_file:
file_contents = content_file.read()
writer.add_document(title=file, content=the_contents, path=filepath)

这至少是一个起点。之后使用 Whoosh 提供的搜索器来运行索引文件似乎非常简单。

with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse("somesearchstring")
results = searcher.search(query)
print(results)

就像我说的,我从来没有使用过“whoosh”,但如果我是你,我就会这么做。

关于python - 嗖嗖功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23974979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com