【问题标题】:Removing stopwords from a list of text files从文本文件列表中删除停用词
【发布时间】:2017-06-21 21:10:39
【问题描述】:

我有一个处理过的文本文件列表,看起来有点像这样:

text = "这是第一个文本文档"这是第二个文本文档"这是第三个文档"

我已经能够成功地标记句子:

sentences = sent_tokenize(text)
    for ii, sentence in enumerate(sentences):
        sentences[ii] = remove_punctuation(sentence)
sentence_tokens = [word_tokenize(sentence) for sentence in sentences]

现在我想从这个标记列表中删除停用词。
但是,因为它是文本文档列表中的句子列表,我似乎无法弄清楚如何做到这一点。

这是我迄今为止尝试过的,但它没有返回任何结果:

sentence_tokens_no_stopwords = [w for w in sentence_tokens if w not in stopwords]

我假设实现这一点需要某种 for 循环,但我现在所拥有的不起作用。任何帮助将不胜感激!

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    您可以像这样创建两个列表生成器:

    sentence_tokens_no_stopwords = [[w for w in s if w not in stopwords] for s in sentence_tokens ]
    

    【讨论】:

    • 完美运行!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2016-01-19
    • 2021-03-21
    • 2011-05-03
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多