我有一个歌曲列表,类似
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样…"one_answers"膝盖"当没有标记器时,我没有任何带点、逗号等的单词
您可以传递一个自定义的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}