python - 如何从 sklearn TfidfVectorizer 中删除所有非英语标记?
问题描述
TfidfVectorizer(analyzer='word', ngram_range=ngram_range, min_df=0, stop_words=lang)
我正在尝试对我的语料库进行矢量化,但我的语料库同时包含英语和阿拉伯语单词。我想删除阿拉伯语单词。
解决方案
你可以使用 strip_accents = "ascii" :
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
'This is the first document. সহজ نعم فعلا' ,
'This document is the second document. সহজ نعم فعلا',
'And this is the third one.',
'Is this the first document?',
]
vectorizer = TfidfVectorizer(strip_accents = "ascii")
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
输出:
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
推荐阅读
- r - 在 R 中:如何创建一个具有初始值的列,并且每个下一个值都是前一个值加上一个集合整数
- python - 右键单击不适用于在 IDLE 中设置断点。他们改变了这个功能吗?
- javascript - .then(function) 不运行函数
- .net - 实体框架 - 通过映射表加载带有标签的帖子
- sql - 如何修复 ORA-06575: 包或函数处于无效状态错误
- apache-spark - Spark 覆盖删除 db2 中已存在表的权限
- sql-server - 在删除所有记录并在 SSIS 中重新加载之前备份表
- flutter - StreamBuilder snapshot.hasError 在键盘显示/隐藏颤动时显示多次
- python - 从 MySQL 表中解密 Fernet 密文
- react-native - 状态更改不重新渲染组件