使用 Python 进行实时文本处理。例如考虑这句话
I am going to schol today
我想要执行以下操作(实时):
1) tokenize 2) check spellings 3) stem(nltk.PorterStemmer()) 4) lemmatize (nltk.WordNetLemmatizer())
目前我正在使用NLTK库来执行这些操作,但它不是实时的(意味着它需要几秒钟才能完成这些操作)。我一次处理 1 个句子,是否可以提高效率
更新: 分析:
Fri Jul 8 17:59:32 2011 srj.profile 105503 function calls (101919 primitive calls) in 1.743 CPU seconds Ordered by: internal time List reduced from 1797 to 10 due to restriction ncalls tottime percall cumtime percall filename:lineno(function) 7450 0.136 0.000 0.208 0.000 sre_parse.py:182(__next) 602/179 0.130 0.000 0.583 0.003 sre_parse.py:379(_parse) 23467/22658 0.122 0.000 0.130 0.000 {len} 1158/142 0.092 0.000 0.313 0.002 sre_compile.py:32(_compile) 16152 0.081 0.000 0.081 0.000 {method 'append' of 'list' objects} 6365 0.070 0.000 0.249 0.000 sre_parse.py:201(get) 4947 0.058 0.000 0.086 0.000 sre_parse.py:130(__getitem__) 1641/639 0.039 0.000 0.055 0.000 sre_parse.py:140(getwidth) 457 0.035 0.000 0.103 0.000 sre_compile.py:207(_optimize_charset) 6512 0.034 0.000 0.034 0.000 {isinstance}
蒂姆:
t = timeit.Timer(main) print t.timeit(1000) => 3.7256231308
最佳答案
NLTK 的 WordNetLemmatizer
使用lazily-loaded WordNetCorpusReader (使用 LazyCorpusLoader
)。第一次调用lemmatize()
如果触发语料库加载,则可能比后面的调用花费更长的时间。
您可以向 lemmatize()
发出虚拟调用当您的应用程序启动时触发加载。
关于python - 使用 Python 进行实时文本处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621666/