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