我已经使用Sklearn完成了TFIDF,但问题是我不能用英语单词作为停止语,因为我在马来西亚巴哈萨语(非英语)。我需要的是导入包含一个停止语列表的txt文件。
stopword.txt
saya
cintakan
awak
tfidf.py
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
'Saya cinta awak',
'Saya x happy awak',
'Saya geram awak',
'Saya taubat awak']
vocabulary = "taubat".split()
vectorizer = TfidfVectorizer(analyzer='word', vocabulary=vocabulary)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
您可以加载特定停止词列表,并将其作为参数传递给TfidfVectorizer
。在您的示例中:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
'Saya cinta awak',
'Saya x happy awak',
'Saya geram awak',
'Saya taubat awak']
# HERE YOU DO YOUR MAGIC: you open your file and load the list of STOP WORDS
stop_words = [unicode(x.strip(), 'utf-8') for x in open('stopword.txt','r').read().split('n')]
vectorizer = TfidfVectorizer(analyzer='word', stop_words = stop_words)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
带停止字的输出:
{u'taubat': 2.09861228866811, u'happy': 2.09861228866811, u'cinta': 2.09861228866811, u'benci': 2.09861228866811, u'geram': 2.09861228866811}
无停止字输出参数:
{u'benci': 2.09861228866811, u'taubat': 2.09861228866811, u'saya': 1.0, u'awak': 1.0, u'geram': 2.09861228866811, u'cinta': 2.09861228866811, u'happy': 2.09861228866811}
警告:我不会使用参数
vocabulary
,因为它告诉TfidfVectorizer
只注意其中指定的单词,而且通常比说出要忽略的单词更难知道要考虑的所有单词。因此,如果从示例中删除vocabulary
参数,并将stop_words
参数添加到列表中,它将按预期工作。
在Python3中,我建议使用以下过程来获取您自己的停止词列表:
- 打开相关文件路径,以列表形式读取存储在.txt中的停止字:
with open('C:\Users\mobarget\Google Drive\ACADEMIA\7_FeministDH for Susan\Stop words Letters_improved.txt', 'r') as file:
my_stopwords=[file.read().replace('n', ',')]
- 请参阅矢量器中的停止词:
vectorizer = text.CountVectorizer(input='filename', stop_words=my_stopwords, min_df=20)