TfidfVectorizer使用我自己的停用词字典



我想问你我是否可以使用我自己的停用词字典而不是TfidfVectorizer中预先存在的字典。我建立了一个更大的停用词词典,我更喜欢使用它。但是,我很难将其包含在下面的代码中(不过显示了标准代码(。

def preprocessing(line):
line = line.lower()
line = re.sub(r"[{}]".format(string.punctuation), " ", line)
return line
tfidf_vectorizer = TfidfVectorizer(preprocessor=preprocessing,stop_words_='english')
tfidf = tfidf_vectorizer.fit_transform(df["0"]['Words']) # multiple dataframes
kmeans = KMeans(n_clusters=2).fit(tfidf)

但我收到以下错误:

TypeError: __init__() got an unexpected keyword argument 'stop_words_'

假设我的字典是:

stopwords["a","an", ... "been", "had",...]

我怎样才能包括它?

任何帮助将不胜感激。

对于您要做的事情来说,这是一种更好的方法:请注意,TfidfVectorizer有一个Tokenizer方法,该方法接受干净的单词数组。 我想也许这对你有用!

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
import re
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.corpus import stopwords
nltk.download(['stopwords'])
# here you can add to stopword_list any other word that you want or define your own array_like stopwords_list
stop_words = stopwords.words('english')
def preprocessing(line):
line = re.sub(r"[^a-zA-Z]", " ", line.lower())
words = word_tokenize(line)
words_lemmed = [WordNetLemmatizer().lemmatize(w) for w in words if w not in stop_words]
return words_lemmed
tfidf_vectorizer = TfidfVectorizer(tokenizer=preprocessing)
tfidf = tfidf_vectorizer.fit_transform(df['Texts'])
kmeans = KMeans(n_clusters=2).fit(tfidf)

TfidfVectorizer 没有参数 'stop_words_'。

如果您有自定义stop_words列表,如下所示:

smart_stoplist = ['a', 'an', 'the']

像这样使用它:

tfidf_vectorizer = TfidfVectorizer(preprocessor=preprocessing,stop_words=smart_stoplist)

最新更新