您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中删除停用词的更快方法

在Python中删除停用词的更快方法

尝试缓存停用词对象,如下所示。每次调用函数时都要构造它,这似乎是瓶颈。

    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()

:我通过探查跑这 。相关行如下。

nCalls累积时间

10000 7.723个单词.py:7(testFuncOld)

10000 0.140个单词。py:11(testFuncNew)

因此,缓存停用词实例可以使速度提高约70倍。

python 2022/1/1 18:46:39 有325人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶