python - 在 TfidfVectorizer 中删除法语和英语中的停用词

标签 python nltk stop-words tfidfvectorizer

我正在尝试删除 TfidfVectorizer 中法语和英语的停用词。到目前为止,我只成功地从英语中删除了停用词。当我尝试为 stop_words 输入法语时,收到一条错误消息,指出它不是内置的。

事实上,我收到以下错误消息:

ValueError: not a built-in stop list: french

我有一个文本文档,其中包含 700 行法语和英语混合的文本。

我正在使用 Python 做一个这 700 行的聚类项目。然而,我的集群出现了一个问题:我得到的集群充满了法语停用词,这扰乱了集群的效率。

我的问题如下:

有什么方法可以添加法语停用词或手动更新内置的英语停用词列表,以便我可以摆脱这些不必要的单词?

这是包含我的停用词代码的 TfidfVectorizer 代码:

tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
                             min_df=0.2, stop_words='english',
                             use_idf=True, tokenizer=tokenize_and_stem, 
ngram_range=(1,3))

删除这些法语停用词将使我能够拥有代表文档中重复出现的单词的集群。

对于有关此问题的相关性的任何疑问,我上周曾问过类似的问题。但是,它并不相似,因为它不使用 TfidfVectorizer。

任何帮助将不胜感激。谢谢。

最佳答案

您可以使用 NLTK 中的良好停用词包或Spacy ,两个 super 流行的 Python NLP 库。由于 achultz 已经添加了使用 stop-words 的代码片段库,我将展示如何使用 NLTK 或 Spacy。

NLTK:

from nltk.corpus import stopwords

final_stopwords_list = stopwords.words('english') + stopwords.words('french')
tfidf_vectorizer = TfidfVectorizer(max_df=0.8,
  max_features=200000,
  min_df=0.2,
  stop_words=final_stopwords_list,
  use_idf=True,
  tokenizer=tokenize_and_stem,
  ngram_range=(1,3))

NLTK 总共会给您 334 个停用词。

空间:

from spacy.lang.fr.stop_words import STOP_WORDS as fr_stop
from spacy.lang.en.stop_words import STOP_WORDS as en_stop

final_stopwords_list = list(fr_stop) + list(en_stop)
tfidf_vectorizer = TfidfVectorizer(max_df=0.8,
  max_features=200000,
  min_df=0.2,
  stop_words=final_stopwords_list,
  use_idf=True,
  tokenizer=tokenize_and_stem,
  ngram_range=(1,3))

Spacy 总共为您提供 890 个停用词。

关于python - 在 TfidfVectorizer 中删除法语和英语中的停用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57359982/

相关文章:

elasticsearch - 多语言Elasticsearch索引

Python对数字,'n'中的数字求和

python - 控制所有子图上的 xaxis 刻度线大小

nltk - 为什么 Sacrebleu 对于短句子返回零 BLEU 分数?

Python:txt 文件输出的停用词不是每行

python - 属性错误: 'list' object has no attribute 'split' when i try to split a row from csv file

java - 未从字符串中正确删除停用词

python - 将字符串的 Pandas DataFrame 转换为直方图

python - 如何生成将键生成为值的范围函数的 python 字典理解?

python - 包含引号的文本的句子标记化