【问题标题】:Is there any way where i can filter out words in python? [closed]有什么办法可以过滤掉python中的单词吗? [关闭]
【发布时间】:2015-05-08 01:02:37
【问题描述】:

有什么方法可以过滤掉python中的文章(the、a、an)、代词、连词(and、but)和其他非主题词?或者有没有可以提供帮助的python包?

我知道我可以使用过滤器和其他东西,但我需要一个长长的所有单词列表。是否已经有包含这些单词的包?我需要摆脱这些词,以便能够处理删除这些词的列表所消耗的内存会轻得多。

谢谢

【问题讨论】:

    标签: python stop-words


    【解决方案1】:

    您要查找的术语称为停用词删除

    一个强大的库来完成这个是NLTK

    它可以处理更复杂的输入文本标记化,轻松删除停用词等等:

    import nltk
    from nltk.corpus import stopwords
    
    sentence = """At eight o'clock on Thursday morning ... Arthur didn't feel very good."""
    tokens = nltk.word_tokenize(sentence)
    
    filtered_tokens = [w for w in tokens if not w.lower() in stopwords.words('english')]
    
    print tokens
    print filtered_tokens
    

    这将打印:

    ['At', 'eight', "o'clock", 'on', 'Thursday', 'morning', '...', 'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
    ['eight', "o'clock", 'Thursday', 'morning', '...', 'Arthur', "n't", 'feel', 'good', '.']
    

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2015-07-13
      • 2018-08-03
      • 2012-11-19
      • 2014-03-14
      • 2012-04-12
      相关资源
      最近更新 更多