我试图在Python中找到一种快速方法来检查术语列表是否可以匹配大小从50到50,000个字符的字符串.
一个术语可以是:
匹配是词边界周围存在单词或短语的位置,因此:
match(term='apple', string='An apple a day.') # True
match(term='berry pie', string='A delicious berry pie.') # True
match(term='berry pie', string='A delicious blueberry pie.') # False
Run Code Online (Sandbox Code Playgroud)
我目前有大约40个术语,其中大部分都是简单的单词.术语的数量会随着时间的推移而增加,但我不希望它超过400.
我对字符串匹配的术语或者匹配的字符串中的哪个字段不感兴趣,我只需要一个匹配每个字符串的true/false值 - 更可能是没有术语匹配字符串,所以对于500匹配的地方,我可以存储字符串以便进一步处理.
速度是最重要的标准,我想利用那些比我聪明的代码,而不是试图实施白皮书.:)
到目前为止,我提出的最快速的解决方案是:
def data():
return [
"The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae).",
"This resulted in early armies adopting the style of hunter-foraging.",
"Beef pie fillings are popular in Australia. Chicken pie fillings are too."
] …Run Code Online (Sandbox Code Playgroud)