【发布时间】:2014-02-26 08:38:10
【问题描述】:
我有一个小脚本想要根据几个条件提取一些独特的单词,并且检查这些条件需要很长时间。
可能是因为它检查了一个大字典,并且它还对每个标记应用了一个词干分析器。
条件是:
- 令牌不在所选字典中
- token 长度超过 1
- 标记不在一组固定的标点符号中
- token 不是纯数字
- 令牌不以“'s”结尾
是否有更快的多条件检查实现?任何基于 python 的解决方案都是可以接受的,即使使用子进程或 cython 或调用 c/c++ 实现。
请记住,实际上,有更多条件,字典最多有 100,000 个条目。我已经做了类似以下的事情,即使使用yield,链接多个条件也很慢。
import string
from nltk.stem import PorterStemmer
porter = PorterStemmer()
dictionary = ['apple', 'pear', 'orange', 'water', 'eat', 'the', 'with', 'an', 'pie', 'full', 'of', 'water', 'at', 'lake', 'on', 'wednesday', 'plus', 'and', 'many', 'more', 'word']
text = "PEAR eats the Orange, with an Apple's MX2000 full of water - h20 - at Lake 0129 on wednesday."
def extract(txt, dic):
for i in txt.split():
_i = i.strip().strip(string.punctuation).lower()
if _i not in dic and len(_i) > 1 and not _i.isdigit() \
and porter.stem(_i) not in dictionary and not i.endswith("'s"):
yield _i
for i in extract(text, dictionary):
print i
[出]
MX2000
h20
【问题讨论】:
-
你不会加紧条件的多样性;将它们与
and结合起来是最有用的方法(如果它们都需要为真)。但是你可能会在算法的其他点调整你的结果。您可以使用re.finditer()而不是split()来遍历txt中的标记;这样你就不会建立一个你不需要的所有令牌的列表。 -
您可以先将
dictionary设为真正的字典,而不是列表。 -
甚至是一套。
dictionary = { 'apple', 'pear', 'orange', ... }
标签: python c++ list dictionary cython