我有一个包含歌曲的列表,例如
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)
但它没有用。我做错了什么?
更新: 使用tokenizer,我有像"哦......"," s-like ..." ,"膝盖," 没有令牌器时,我没有任何带点,逗号等的单词
答案 0 :(得分:2)
您可以传递自定义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}