python - 我如何有效地尝试在大量 XML 列表中查找大量单词

标签 python algorithm search

我有一个很大的化学名称列表 (~30,000,000) 和一个很大的文章列表 (~34,000),它们以 XML 的形式作为文件存储在服务器上。

我试图将每个 XML 解析为字符串,以提及一个或多个化学名称。最终结果将是一个制表符分隔的文本文件,其中我有一个文件名,然后是文件中出现的化学品列表。

当前的问题是我有一个 for 循环,它循环遍历所有化学物质,而 for 循环循环遍历所有 XML。嵌套在 for 循环中的是 python 中的 string in string 操作。有没有办法通过使用比 string in string 更有效的操作或通过重新排列 for 循环来提高性能?

我的伪代码:

for article is articles:
         chemicals_in_article = []
         temp_article = article.lower()
         for chemical in chemicals:
               if chemical in temp_article: chemicals_in_article.append(chemical)

         #Write the results into a text file
         output_file.write(article.file_name)
         for chemical in chemicals_in_article: 
               output_file.write("\t" + chemical)
         output_file.write("\n")

               

最佳答案

我不确定 30M 条目是否会破坏您的内存,但基于 trie 的方法可能是最快的。有几个包以稍微不同的形式实现它,例如 FlashText ;或 trieregex .两者都有与您的场景完全匹配的示例。

编辑:......至少在纯文本上。根据上面的评论,如果您想避免匹配标记的随机位,请构建一个 trie,然后使用 XPath matches 函数查找 trie 派生的正则表达式找到匹配项的文本节点。不幸的是,Python 的主要 XML 库不支持 matches(事实上,支持 XPath 2.0 的库很少),所以这不是很可行。

由于您只需要检测文档文本中任何位置是否存在关键字,一个可行的解决方法是将 XML 转换为文本,然后使用上述方法之一。这是一个例子:

#pip install libxml2-python3 trieregex

from trieregex import TrieRegEx as TRE
from libxml2 import parseDoc
import re


# prepare
words = ['lemon', 'lemons', 'lime', 'limes', 'pomelo', 'pomelos', 'orange', 'oranges', 'citrus', 'citruses']
tre = TRE(*words)
pattern = re.compile(fr"\b{tre.regex()}\b")
# => \b(?:l(?:emons?|imes?)|citrus(?:es)?|oranges?|pomelos?)\b


# search
xml = """
<?xml version="1.0"?>
<recipe>
  <substitute for="lemon">three limes</substitute>
  <substitute for="orange">pomelo</substitute>
</recipe>
""".strip()
doc = parseDoc(xml)
text = doc.getContent()
matches = pattern.findall(text)
print(matches)
# => ['limes', 'pomelo']
doc.freeDoc()

请注意,您只需要准备一次正则表达式;然后,您可以非常快速地将其应用于多个文档。

关于python - 我如何有效地尝试在大量 XML 列表中查找大量单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67792760/

相关文章:

python - 查找一组数据的中位数并应用于该组的成员

algorithm - 最短路径算法的变体

php - 有效搜索 php 迭代器

php - 过滤动态表结构

search - 如何按未在 ElasticSearch 中编制索引的外部数据进行过滤

python - Heroku 上的夹层

python - 在同一页上打印两次的文本

python - 导入声明全局变量的函数

algorithm - 两套元素。集合 A 的每个元素在集合 B 中唯一匹配。在 O(nlogn) 时间内将集合 A 的每个项目与集合 B 中的项目匹配

active-directory - 如何在System.DirectoryServices.Protocols中放弃长时间运行的搜索