TF-IDF 提取关键词

TF-IDF extracting keywords

像这样处理函数:

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())

其中 return 传递的文本中的关键字列表。 有没有办法在提供的情况下获取关键字? 喜欢传递的字符串

输入:

a = "TIME is the company where I work"

而不是获取关键字列表:

['time', 'company']

我喜欢得到:

['TIME', 'company']

默认情况下,TfidfVectorizer 将单词转换为 lowercase.Use 这一行:

  tf = TfidfVectorizer(analyzer='word',lowercase=False, ngram_range=(1, 4), min_df=0, stop_words=stop_words)  

它应该可以工作。使用此 link 作为参考。 TfidfVectorizer