gpt4 book ai didi

scikit-learn - CountVectorizer 上的词形还原不会删除停用词

转载 作者:行者123 更新时间:2023-12-04 05:19:36 26 4
gpt4 key购买 nike

我正在尝试将 Lematization 添加到来自 Skit-learn 的 CountVectorizer,如下

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'



它对所有单词进行词缀化,并且不会删除停用词。那么,有什么想法吗?

最佳答案

那是因为词形还原是在去除停用词之前完成的。然后在stopwords.words('spanish')提供的停用词集中没有找到词形化停用词。 .

CountVectorizer 的完整工作顺序请引用 my other answer here .它关于 TfidfVectorizer 但顺序是相同的。在那个答案中,第 3 步是词形还原,第 4 步是去除停用词。

所以现在要删除停用词,您有两个选择:

1) 您将停用词集本身词形化,然后将其传递给 stop_words 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')]

如果不起作用,请尝试这些并发表评论。

关于scikit-learn - CountVectorizer 上的词形还原不会删除停用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50155188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com