【问题标题】:Remove Stopwords in French AND English in TfidfVectorizer在 TfidfVectorizer 中删除法语和英语中的停用词
【发布时间】:2019-12-13 01:25:16
【问题描述】:

我正在尝试在 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。

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

【问题讨论】:

  • 这可能不相关,但以防万一,可能存在 unicode/utf8 字符,具体取决于您用于 stop_words 数组的列表/文件,您可能需要更改它们以避免警告。方法如下:stackoverflow.com/questions/25443802/…

标签: python nltk stop-words tfidfvectorizer


【解决方案1】:

您可以使用来自NLTKSpacy 的优秀停用词包,这两个用于Python 的超级流行的NLP 库。由于 achultz 已经添加了使用 stop-words 库的 sn-p,我将展示如何使用 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 个停用词。

【讨论】:

  • PS:Spacy 是我个人的最爱。
【解决方案2】:

根据我的经验,解决此问题的最简单方法是在预处理阶段手动删除停用词(同时从其他地方获取最常见的法语短语列表)。

此外,应该可以方便地检查您的文本/模型中哪些停用词最常出现在英语和法语中(仅通过它们的出现或 idf)并将它们添加到您在预处理阶段排除的停用词。

如果您更喜欢使用 tfidfvectorizer 内置方法删除单词,请考虑制作一个您想要同时包含法语和英语的停用词列表并将它们传递为

stopwords=[a,he,she,le,...]
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
                             min_df=0.2, stop_words=stopwords,analyzer=’word’,
                             use_idf=True, tokenizer=tokenize_and_stem) 

重要的是,引用documentation:

“english”是目前唯一支持的字符串值

因此,现在您必须手动添加一些停用词列表,您可以在网络上的任何位置找到这些停用词,然后根据您的主题进行调整,例如: stopwords

【讨论】:

    【解决方案3】:

    Igor Sharm 指出了手动执行操作的方法,但也许您也可以安装 stop-words package。然后,由于 TfidfVectorizer 允许列表作为 stop_words 参数,

    from stop_words import get_stop_words
    
    my_stop_word_list = get_stop_words('english') + get_stop_words('french')
    
    tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
                                 min_df=0.2, stop_words=my_stop_word_list,
                                 use_idf=True, tokenizer=tokenize_and_stem, 
    ngram_range=(1,3))
    

    如果您只想包含一些单词,也可以根据需要读取和解析french.txt file in the github project

    【讨论】:

    • 完全同意这也是一个好方法,因为需要较少的操作,但是 imo 在现实生活中的每个研究领域的应用中都有一些特定的停用词,当您知道研究(例如,当所有文档都与运动有关时,您可能会通过删除“运动”、“运动员”等词来增强搜索),甚至对数据的 idf 进行小型研究也可以提供有价值的见解,哪些词最有可能是您的停用词(ofc 包括众所周知的停用词总是一个好主意,但一些额外的东西可以大大改善模型)。
    猜你喜欢
    • 2019-12-07
    • 2017-11-28
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多