【发布时间】:2019-12-01 14:37:29
【问题描述】:
我正在学习制作基于内容的图书推荐系统(参考:https://towardsdatascience.com/how-to-build-from-scratch-a-content-based-movie-recommender-with-natural-language-processing-25ad400eb243)。我使用 rake 函数从“Plot”列中提取关键字。如何将这些关键字分配给新列?
我正在使用 pandas、numpy、CountVectorizer、rake_nltk。我尝试了以下代码:row['Key_words'] = list(key_words_dict_scores.keys()) 但该列仍然是空的。
import pandas as pd
from rake_nltk import Rake
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
df = pd.read_csv('cleaned DATA set.csv')
df = df[['Book_ID','Title','Author','Genre1','Genre2','Plot']]
for index, row in df.iterrows():
plot = row['Plot']
# instantiating Rake, by default it uses english stopwords from NLTK
# and discards all puntuation characters as well
r = Rake()
# extracting the words by passing the text
r.extract_keywords_from_text(plot)
# getting the dictionary whith key words as keys and their scores as values
key_words_dict_scores = r.get_word_degrees()
# assigning the key words to the new column for the corresponding movie
row['Key_words'] = list(key_words_dict_scores.keys())
我希望看到添加了一个名为 'Key_words' 的新列,其中包含对应书名的所有关键字。
实际输出显示'key_words' 列是空的。
【问题讨论】:
标签: python pandas jupyter-notebook cosine-similarity countvectorizer