python - 嗖嗖功能

标签 python whoosh

我有超过 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/

相关文章:

python - 无法运行 Whoosh 'quickstart' 示例 (FileNotFoundError)

python - whoosh 是否要求所有字符串都是 unicode?

python - CEF Python 隐藏浏览器

python - Django 在模型字段更新后将值转换为元组

python - 查找以 1 分钟为间隔采样的 pandas 时间序列数据帧中的空白,并用新行填充空白

python sqlalchemy动态获取列名?

django - Django Haystack 搜索中没有结果

python whoosh 索引大文件的时间太长

python - Django haystack+呼呼错误

python - 圆弧补丁之间的填充 - Matplotlib