2017-11-13 12 views
0

私はある種類の「dicts of dicts」を作成しようとしています。第1レベルの辞書は、単語 - 周波数のキー - 値の対である。下の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 { '目標':freq_dict、 'ウォルマート':freq_dct、等}

+0

doesnの」 t '{" Target ":freq_dict}'は動作しますか? –

+0

よく 'store1': 'door:1'、 'sound:2'など – Tony

+0

どの辞書がどの店舗に属しているかを特定する方法が必要です – Tony

答えて

0
// 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_dict 
関連する問題