【问题标题】:Bag of words representation using sklearn plus Snowballstemmer使用 sklearn 和 Snowballstemmer 表示词袋
【发布时间】:2015-07-21 23:08:52
【问题描述】:

我有一个歌曲列表,比如

list2 = ["first song", "second song", "third song"...]

这是我的代码:

from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords

vectorizer = CountVectorizer(stop_words=stopwords.words('english'))
bagOfWords = vectorizer.fit(list2)
bagOfWords = vectorizer.transform(list2)

它正在工作,但我想列出我的话。

我已经尝试过这样做

def tokeni(self,data):
        return [SnowballStemmer("english").stem(word) for word in data.split()]

vectorizer = CountVectorizer(stop_words=stopwords.words('english'), 
                             tokenizer=self.tokeni)

但它没有工作。我做错了什么?

更新: 使用分词器,我有诸如“哦...”,“s-like ...”,“膝盖”之类的词 当没有标记器时,我没有任何带有点、逗号等的单词

【问题讨论】:

  • “它不起作用”到底是什么意思?请粘贴/描述您遇到的错误/您如何得出它不起作用的结论。

标签: python python-3.x scikit-learn nltk


【解决方案1】:

您可以传递一个自定义的preprocessor,它应该也可以正常工作,但保留tokenizer 的功能:

from sklearn.feature_extraction.text import CountVectorizer
from nltk.stem import SnowballStemmer

list2 = ["rain", "raining", "rainy", "rainful", "rains", "raining!", "rain?"]

def preprocessor(data):
        return " ".join([SnowballStemmer("english").stem(word) for word in data.split()])

vectorizer = CountVectorizer(preprocessor=preprocessor).fit(list2)
print vectorizer.vocabulary_

# Should print this:
# {'raining': 2, 'raini': 1, 'rain': 0}

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 2018-01-23
    • 2015-08-19
    • 1970-01-01
    • 2019-02-28
    • 2012-11-21
    • 2017-06-17
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多