在某种程度上处理函数:
def get_feature_name_by_tfidf(text_to_process):
with open(master_path + '\\additional_stopwords.txt', 'r') as f:
additional_stop_words = ast.literal_eval(f.read())
stop_words = text.ENGLISH_STOP_WORDS.union(set(additional_stop_words))
tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 4), min_df=0, stop_words=stop_words)
tfidf_matrix = tf.fit_transform(text_to_process.split(','))
tagged = nltk.pos_tag(tf.get_feature_names())
feature_names_with_tags = {k: v for k, v in dict(tagged).items() if v != 'VBP'}
return list(feature_names_with_tags.keys())
返回传递文本中的关键字列表。 有没有办法让关键字与提供的相同? 喜欢传递的字符串
输入:
a = "TIME is the company where I work"
而不是将关键字列表作为:
['time', 'company']
我想得到:
['TIME', 'company']
答案 0 :(得分:1)
默认情况下,TfidfVectorizer会将单词转换为小写。请使用以下行:
tf = TfidfVectorizer(analyzer='word',lowercase=False, ngram_range=(1, 4), min_df=0, stop_words=stop_words)
它应该有效。使用此链接进行参考。 TfidfVectorizer