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

아무튼 ' t'{ "Target": freq_dict}'작동합니까? –

+0

음 'store1': 'door : 1', '소리 : 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