bag-of-words

    科技2022-08-06  115

    文章目录

    一、bag-of-words1.1 词袋特性 二、过滤2.1 stopwords2.1.1 停用词是什么2.1.2 停用词使用情景2.1.3 NLTK使用2.1.4 从一段文本中删除停用词

    一、bag-of-words

    1.1 词袋特性

    词袋是统计文本中每个单词出现的次数实质上是将文本展平为一维向量词袋没有顺序词袋也没有词语之间意思上的包含关系。比如动物包含狗,猫等。

    二、过滤

    2.1 stopwords

    2.1.1 停用词是什么

    将数据转换为计算机可以理解的内容的过程称为预处理。预处理的主要形式之一是过滤掉无用的数据。在自然语言处理中,无用的单词(数据)称为停用词。停用词是指搜索引擎已编程忽略的常用词(例如“the”,“a”,“an”,“in”)。我们不希望这些单词占用我们数据库中的空间,或占用宝贵的处理时间。为此,我们可以通过存储要停止使用的单词的列表轻松删除它们。python中的NLTK(自然语言工具包)具有以16种不同语言存储的停用词列表。可以在nltk_data目录中找到它们。home / pratima / nltk_data / corpora / stopwords是目录地址(不要忘记更改你的主目录名称)

    2.1.2 停用词使用情景

    对于分类任务,可以删除停止词

    但对于情感分析等需要分析语义的任务,则需要另外考虑

    2.1.3 NLTK使用

    其中的停用词包含了don’t的撇号,所以使用时文本要保留撇号其中停用词都是小写,使用时需要将文本都转换为小写

    2.1.4 从一段文本中删除停用词

    from nltk.corpus import stopwords from nltk.tokenize import word_tokenize example_sent = "This is a sample sentence, showing off the stop words filtration." stop_words = set(stopwords.words('english')) word_tokens = word_tokenize(example_sent) filtered_sentence = [w for w in word_tokens if not w in stop_words] print(word_tokens) print(filtered_sentence)

    输出

    ['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 'off', 'the', 'stop', 'words', 'filtration', '.'] ['This', 'sample', 'sentence', ',', 'showing', 'stop', 'words', 'filtration', '.'] ce', ',', 'showing', 'stop', 'words', 'filtration', '.']
    Processed: 0.033, SQL: 8