0

何千もの同義語のリストがあります。また、これらの用語を検索したいドキュメントが何万もあります。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)

この問題に対する適切な解決策はありますか、それともしばらく時間がかかりますか? 前もって感謝します

4

1 に答える 1

0

シノニムのリストから正規表現を作成するのはどうですか:

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
于 2012-05-23T22:04:39.140 に答える