2017-02-17 12 views
0

私は、後でトリグラムのために使用される埋め込みの3つの層を持つデフォルトのdictを持っています。現在の辞書を別のPythonに入れ子にするにはどうすればいいですか?

counts = defaultdict(lambda:defaultdict(lambda:defaultdict(lambda:0))) 

その後、私は文書を通過し、それぞれの文字の数(およびbicountsとtricounts)を作成するループを持って

counts[letter1][letter2][letter3] = counts[letter1][letter2][letter3] + 1 

私はどうかを指定できるように、別の層を追加します文字は子音または母音です。

アルファベットのすべての文字の代わりに、私のバイグラムとトリグラムをConsonant vs. Vowelで実行できるようにしたいのですが、これを行う方法がわかりません。

+0

あなたは現在のコードを提供できますか? – mitoRibo

+0

私はあなたの質問を理解していません...単にあなたのdefaultdictに別の "レイヤー"を追加するだけでは問題を解決できませんか?どのように接近するのか正確に分からないのですか? –

+1

良い神様、あなたはそれをあまりにも憎んでいるのですか?それを使用していない(特にここでは)と比較して、遅くて冗長で冗長です: 'counts [letter1] [letter2] [letter3] + = 1' – ShadowRanger

答えて

0

私はあなたが何をしたい正確にわからないんだけど、私は、ネストされた辞書のアプローチは、文字(すなわちd['ab']の代わりd['a']['b'])を合わせた文字列によってキーフラット辞書を持つほどきれいではないと思います。私は、バイグラム/トライグラムが母音/子音または混合音だけで構成されているかどうかを確認するコードも入れました。

CODE:

from collections import defaultdict 


def all_ngrams(text,n): 
    ngrams = [text[ind:ind+n] for ind in range(len(text)-(n-1))] 
    ngrams = [ngram for ngram in ngrams if ' ' not in ngram] 
    return ngrams 


counts = defaultdict(int) 
text = 'hi hello hi this is hii hello' 
vowels = 'aeiouyAEIOUY' 
consonants = 'bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ' 

for n in [2,3]: 
    for ngram in all_ngrams(text,n): 
     if all([let in vowels for let in ngram]): 
      print(ngram+' is all vowels') 

     elif all([let in consonants for let in ngram]): 
      print(ngram+' is all consonants') 

     else: 
      print(ngram+' is a mixture of vowels/consonants') 

     counts[ngram] += 1 

print(counts) 

OUTPUT:

hi is a mixture of vowels/consonants 
he is a mixture of vowels/consonants 
el is a mixture of vowels/consonants 
ll is all consonants 
lo is a mixture of vowels/consonants 
hi is a mixture of vowels/consonants 
th is all consonants 
hi is a mixture of vowels/consonants 
is is a mixture of vowels/consonants 
is is a mixture of vowels/consonants 
hi is a mixture of vowels/consonants 
ii is all vowels 
he is a mixture of vowels/consonants 
el is a mixture of vowels/consonants 
ll is all consonants 
lo is a mixture of vowels/consonants 
hel is a mixture of vowels/consonants 
ell is a mixture of vowels/consonants 
llo is a mixture of vowels/consonants 
thi is a mixture of vowels/consonants 
his is a mixture of vowels/consonants 
hii is a mixture of vowels/consonants 
hel is a mixture of vowels/consonants 
ell is a mixture of vowels/consonants 
llo is a mixture of vowels/consonants 
defaultdict(<type 'int'>, {'el': 2, 'his': 1, 'thi': 1, 'ell': 2, 'lo': 2, 'll': 2, 'ii': 1, 'hi': 4, 'llo': 2, 'th': 1, 'hel': 2, 'hii': 1, 'is': 2, 'he': 2}) 
0

はあなたが単に別のマップを保つことができる母音と子音の順序のためのカウントを維持する必要があると仮定すると。

あなたはそれはあなたがこれを行うことが子音かどうletterは母音とFalseある場合Trueを返す関数is_vowel(letter)を持っている場合。

vc_counts[is_vowel(letter1)][is_vowel(letter2)][is_vowel(letter3)] = \ 
vc_counts[is_vowel(letter1)][is_vowel(letter2)][is_vowel(letter3)] + 1 
+0

ありがとうございます!これは完璧です。 –

+0

すばらしい@KatieTetzloff。それがあなたの質問を解決した場合、あなたはupvoteして答えを受け入れることができますか?ありがとう! – cjungel

関連する問題