我正在尝试创造一种“字典”。第一级字典是一个词频键值对。由下面的for循环创建,'cleaned_words_string‘是保存我正在分析的文本的字符串,对于每个商店都是唯一的。
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(cleaned_word_string)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w.lower() not in stop_words:
filtered_sentence.append(w)
fw_freq = nltk.FreqDist(filtered_sentence).most_common()
freq_dict = dict(fw_freq)如何修改此代码,使每个单独的“storename”都附加到其freq_dict?
类似于:
Store_dict = {storename: freq_dict}这样输出将是:
Store_dict {'Target':freq_dict,'WalMart':freq_dct等}
发布于 2017-11-13 10:08:58
// I suppose your store of sentences is in this format
store = {'store_name': cleaned_word_string}
store_dict = {}
for store_name in store:
store_dict[store_name] = get_freq_dict(store_dict[store_name])
def get_freq_dict(cleaned_word_string):
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(cleaned_word_string)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
fw_freq = nltk.FreqDist(filtered_sentence).most_common()
freq_dict = dict(fw_freq)
return freq_dicthttps://stackoverflow.com/questions/47255711
复制相似问题