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.

python 去停用词的更多相关文章

  1. python使用jieba实现中文文档分词和去停用词

    分词工具的选择: 现在对于中文分词,分词工具有很多种,比如说:jieba分词.thulac.SnowNLP等.在这篇文档中,笔者使用的jieba分词,并且基于python3环境,选择jieba分词的理 ...

  2. IKAnalyzer进行中文分词和去停用词

    最近学习主题模型pLSA.LDA,就想拿来试试中文.首先就是找文本进行切词.去停用词等预处理,这里我找了开源工具IKAnalyzer2012,下载地址:(:(注意:这里尽量下载最新版本,我这里用的IK ...

  3. R系列:分词、去停用词、画词云(词云形状可自定义)

    附注:不要问我为什么写这么快,是16年写的. R的优点:免费.界面友好(个人认为没有matlab友好,matlab在我心中就是统计软件中极简主义的代表).小(压缩包就几十M,MATLAB.R2009b ...

  4. 更新几篇之前写在公众号上的文章:线性可分时SVM理论推导;关联分析做捆绑销售和推荐;分词、去停用词和画词云

    适合阅读人群:有一定的数学基础. 这几篇文章是16年写的,之前发布在个人公众号上,公众号现已弃用.回过头来再看这几篇文章,发现写的过于稚嫩,思考也不全面,这说明我又进步了,但还是作为学习笔记记在这里了 ...

  5. [超详细] Python3爬取豆瓣影评、去停用词、词云图、评论关键词绘图处理

    爬取豆瓣电影<大侦探皮卡丘>的影评,并做词云图和关键词绘图第一步:找到评论的网页url.https://movie.douban.com/subject/26835471/comments ...

  6. python去除停用词(结巴分词下)

    python 去除停用词  结巴分词 import jieba #stopwords = {}.fromkeys([ line.rstrip() for line in open('stopword. ...

  7. python调用jieba(结巴)分词 加入自定义词典和去停用词功能

    把语料从数据库提取出来以后就要进行分词啦,我是在linux环境下做的,先把jieba安装好,然后找到内容是build jieba PKG-INFO setup.py test的那个文件夹(我这边是ji ...

  8. python利用jieba进行中文分词去停用词

    中文分词(Chinese Word Segmentation) 指的是将一个汉字序列切分成一个一个单独的词. 分词模块jieba,它是python比较好用的分词模块.待分词的字符串可以是 unicod ...

  9. python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)

    # -*- coding:utf8 -*- import os import jieba def splitSentence(inputFile): fin = open(inputFile, 'r' ...

随机推荐

  1. windows环境下生成ssh keys

    参考:https://www.cnblogs.com/achengmu/p/6095046.html 1.首先你要安装Git工具 2.运行Git Bash here 3.输入指令,进入.ssh文件夹 ...

  2. javacript中apply和call的区别

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性.   接受的参数是一个字符串. call:和apply的意思一样,只不过是参数列表不一样. 接收的参数是一个数组. 例如: <s ...

  3. [未完结]数字微分分析法的直线绘制(DDA)

    注意! 本文被第1次更新,可能存在后续更新 直线画法 直线的斜截式方程 在二维空间下,一条直线的方程可以被描述为若干种形式,其中比较常见的一种是斜截式方程: \[y=kx+b\] 其中\(k\)称为直 ...

  4. JSP 随记

    jstl <c:forEach> 遍历,多个<option>时显示"全部".单个 option时,默认选中! 引入:<%@ taglib prefix ...

  5. Android开发之裁切(拍照+相冊)图像并设置头像小结

    先看效果:                                       watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...

  6. memcache及其telnet命令使用详解

    1.启动Memcache 常用参数memcached 1.4.3-p <num>      设置端口号(默认不设置为: 11211)-U <num>      UDP监听端口 ...

  7. JAVA调用命令行2

    package loadMBQL; import java.io.File; import java.io.FilenameFilter; public class LoadMBQL { /** * ...

  8. hdu5317 RGCDQ 统计

    // hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) ...

  9. 更改UISearchBar button属性

    //点击搜索框时触发 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { self.theSearchUserSearchBa ...

  10. c++通用写文件调试代码

    #include <stdio.h>#include <sstream>#include <iostream> std::stringstream strs;str ...