我正试图从短剧-学习中向CountVectorizer添加Lematization,如下所示
import nltk
from pattern.es import lemma
from nltk import word_tokenize
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
from nltk.stem import WordNetLemmatizer
class LemmaTokenizer(object):
def __call__(self, text):
return [lemma(t) for t in word_tokenize(text)]
vectorizer = CountVectorizer(stop_words=stopwords.words('spanish'),tokenizer=LemmaTokenizer())
sentence = ["EVOLUCIÓN de los sucesos y la EXPANSIÓN, ellos juegan y yo les dije lo que hago","hola, qué tal vas?"]
vectorizer.fit_transform(sentence)这是输出:
[u',', u'?', u'car', u'decir', u'der', u'evoluci\xf3n', u'expansi\xf3n', u'hacer', u'holar', u'ir', u'jugar', u'lar', u'ler', u'sucesos', u'tal', u'yar']更新
这是出现并已被混淆的停止词:
U‘’lar,u‘’ler‘,u’‘der’
它引起所有的词,而不移除死字。那么,有什么想法吗?
发布于 2018-05-03 15:03:06
那是因为柠檬化是在停止词义删除之前完成的。然后,在stopwords.words('spanish')提供的停止词集中找不到柠檬化的停止词。
有关CountVectorizer的完整工作订单,请参阅my other answer here。是关于TfidfVectorizer的,但顺序是一样的。在这个答案中,第3步是柠檬化,第4步是停止词去除。
因此,现在要删除停止词,您有两个选项:
1)将设置好的停止词本身命名,然后将其传递到stop_words param in CountVectorizer中。
my_stop_words = [lemma(t) for t in stopwords.words('spanish')]
vectorizer = CountVectorizer(stop_words=my_stop_words,
tokenizer=LemmaTokenizer())2)将停止词移除在LemmaTokenizer本身中。
class LemmaTokenizer(object):
def __call__(self, text):
return [lemma(t) for t in word_tokenize(text) if t not in stopwords.words('spanish')]试试看,如果不起作用,请发表评论。
https://stackoverflow.com/questions/50155188
复制相似问题