Try caching the stopwords object, as shown below. Constructing this each time you call the function seems to be the bottleneck.

    from nltk.corpus import stopwords

    cachedStopWords = stopwords.words("english")

    def testFuncOld():
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in stopwords.words("english")]) def testFuncNew():
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in cachedStopWords]) if __name__ == "__main__":
for i in xrange(10000):
testFuncOld()
testFuncNew()

I ran this through the profiler: python -m cProfile -s cumulative test.py. The relevant lines are posted below.

nCalls Cumulative Time

10000 7.723 words.py:7(testFuncOld)

10000 0.140 words.py:11(testFuncNew)

So, caching the stopwords instance gives a ~70x speedup.

最新文章

  1. js 数组赋值问题 :值传递还是引用?
  2. Linux内核完全注释阅读笔记1:O(1)时间复杂度查找timeout定时器
  3. ASP.NET MVC 拦截器IResultFilter
  4. PHP超级全局变量——Session 变量
  5. Java面试汇总
  6. c#扩展方法-摘自msdn
  7. C# winfrom 模拟ftp文件管理
  8. java 图片质量压缩
  9. 学习笔记TF026:多层感知机
  10. springboot mybatis 事务管理
  11. 【Python】 sys和os模块
  12. 三元运算和bytes数据类型笔记
  13. 第十三篇-通过Button设置文本背景颜色
  14. 性能测试五十:Jmeter+Influxdb+Grafana实时数据展示系统搭建
  15. C++:MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStart
  16. TCP/IP学习20180625-DNS
  17. java常用设计模式九:桥接模式
  18. python 获取有关访问者的浏览器的 细节
  19. scala学习之实现RPC通信
  20. 表单验证——jquery validate使用说明

热门文章

  1. ZOJ 3626 Treasure Hunt I(树形dp)
  2. ListView中button监听器 设置 及 优化
  3. java 是 传值还是传址 Pass-by-value or Pass-by-reference
  4. vs2012停止调试长时间不响应问题解决方法
  5. Lombok简介
  6. Vuex demo
  7. find命令用法
  8. Machine Learning in Action(3) 朴素贝叶斯算法
  9. socket 学习笔记
  10. Retina屏幕下image-set