python - 使用 Python 进行实时文本处理

标签 python performance nlp text-processing nltk

使用 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/

相关文章:

python - 如何在多个条件下对 Pandas 的数据进行分组?

python - 使用电子表格数据时如何避免嵌套循环?

c++ - 有没有更有效的方法来包装 float ?

node.js - 新闻文章分类(通过 NLP 进行主题/实体分析?);最好在 Node.js 中

python - 如何使用 Python memcached 检查 Django 缓存的内容?

python - 通过 bool 索引数组进行 Numpy 数组赋值

javascript - 绑定(bind)/取消绑定(bind)事件或使用条件(jQuery)更好吗?

java - 您如何分析 Drools 应用程序?

python - Python 中的 Word Mover 距离

python - 将 Sklearn 的 CountVectorizer 词汇表设置为短语字典