在文档中高效地搜索术语列表

2024-03-28 17:01:16 发布

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

我有上千个同义词的清单。我也有数以万计的文件,我想搜索这些条款。使用python(或伪代码)实现这一点的有效方法是什么?你知道吗

# this would work for single word synonyms, but there are multiple word synonyms too
synonymSet = set([...])
wordsInDocument = set([...])
synonymsInDocument = synonymSet.intersection(wordsInDocument)

# this would work, but sounds slow
matches = []
for document in documents:
    for synonym in synonymSet:
        if synonym in document:
            matches.append(synonym)

这个问题有没有好的解决办法,还是需要一段时间? 先谢谢你


Tags: inforthisdocumentwordbutworksynonyms
1条回答
网友
1楼 · 发布于 2024-03-28 17:01:16

从同义词列表中构建正则表达式如何:

import re

pattern = "|".join(synonymList)
regex = re.compile(pattern)

matches = regex.findall(document) # get a list of the matched synonyms
matchedSynonyms = set(matches)    # eliminate duplicates using a set

相关问题 更多 >