我想創建一個輸入列表,其中有相應的值從全局嵌套字典訪問。下面是代碼,訪問來自嵌套字典的值
import sys
param_values = {
'vowels':{
'aa' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0), (-1,-1)],
'ae' : [(-1,-1), (-1,-1), (0.1,0.8), (-1,-1), (0.1,1.0), (-1,-1)],
'ah' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0), (-1,-1)],
'ao' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.2,1.0), (-1,-1)],
'eh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0), (-1,-1)],
'er' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.15,0.7), (-1,-1)],
'ey' : [(-1,-1), (-1,-1), (0.3,1.0), (-1,-1), (0.1,0.5), (-1,-1)],
'ih' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'iy' : [(-1,-1), (-1,-1), (0.2,1.0), (-1,-1), (0.1,0.8), (-1,-1)],
'uh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0)],
'uw' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0)],
'o' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.4,1.0)]
},
'consonants':{
'b' : [(-1,-1), (0.0,0.0), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'ch' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
'd' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
'dh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.35), (-1,-1)],
'dx' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.35), (-1,-1)],
'f' : [(0.3,1.0), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1)],
'g' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'hh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'jh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'k' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'l' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
'm' : [(-1,-1), (0.0,0.0), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'n' : [(-1,-1), (0.1,1.0), (-1,-1), (0.3,1.0), (0.0,0.0), (-1,-1)],
'ng' : [(-1,-1), (0.1,1.0), (-1,-1), (-1,-1), (0.0,0.0), (-1,-1)],
'p' : [(-1,-1), (0.0,0.0), (-1,-1), (-1,-1), (0.1,0.8), (-1,-1)],
'r' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
's' : [(-1,-1), (0.1,1.0), (-1,-1), (0.3,1.0), (0.0,0.0), (-1,-1)],
'sh' : [(-1,-1), (0.1,1.0), (-1,-1), (0.3,1.0), (0.0,0.0), (-1,-1)],
't' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
'th' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.4), (-1,-1)],
'v' : [(0.3,1.0), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1)],
'w' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,1.0)],
'y' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.6), (-1,-1)],
'z' : [(-1,-1), (0.1,1.0), (-1,-1), (0.3,1.0), (0.0,0.0), (-1,-1)],
'zh' : [(-1,-1), (-1,-1), (-1,-1), (-1,-1), (0.1,0.6), (-1,-1)]
}
}
diphthong = {
'aw' : ['ao' , 'uw'],
'ay' : ['ao' , 'ih'],
'ow' : ['o' , 'aa'],
'oy' : ['o' , 'ih']
}
print "Usage :python co.py phonemeFile"
def coart(phonemeFile):
""" Function for generating parameter values from the global list """
phonemeList = []
with open("syllabifiedPhonemes.txt", "r") as pFile :
for line in pFile :
l = line.split()
for phoneme in l :
next_phoneme = diphthong.get(phoneme)
if next_phoneme is None :
# exploring the dict param_values extracting each nested dict
for group in param_values.keys() : # 'group' refers to vowels or consonants
# iterate over each nested dict
for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group
phonemeList.append((phones, param_values.get(param_values[group][phones])))
else :
for group in param_values.keys() : # 'group' refers to vowels or consonants
for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group
phonemeList.extend([(phones, param_values.get(param_values[group][phones])) for phoneme in next_phoneme])
print "New List"
print '\n'.join(str(l) for l in phonemeList)
輸入文件syllabifiedPhonemes.txt有以下內容:
s aa ' m ih ' k l eh k ' t aa ' n ih t ' g eh l ' v ae ' n ih ' k aa ' p l ay k
的if-else語句是不正確的,據我可以看到。我得到了以下錯誤:
Traceback (most recent call last):
File "co.py", line 189, in <module>
coart("syllabifiedPhonemes.txt")
File "co.py", line 160, in coart
phonemeList.append((phones, param_values.get(param_values[group][phones])))
TypeError: unhashable type: 'list'
我改變了if-else語句下面的地方,擺脫錯誤的。
if next_phoneme is None :
# exploring the dict param_values extracting each nested dict
for group in param_values.keys() : # 'group' refers to vowels or consonants
# iterate over each nested dict
for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group
phonemeList.append((phones, param_values[group][phones]))
else :
for group in param_values.keys() : # 'group' refers to vowels or consonants
for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group
phonemeList.extend([(phones, param_values[group][phones]) for phoneme in next_phoneme])
但現在我得到的輸出作爲一個巨大的名單,我相信通過字典多次程序重複一次又一次打印整個字典,而不是隻顯示給定的輸入值。有人能指出我錯在哪裏嗎?謝謝。
爲什麼你需要在這裏使用嵌套字典。用元音和輔音去除圖層可以簡化這個顯着的問題 –
@james我必須使用嵌套字典,因爲在輸入音素列表後,他們的值主要是比較對方的值(範圍)後我必須做更多的事情。而他們對於音位是輔音或元音有不同的條件/規則。 – zingy
,但通過將它們放入列表中,您將丟失該信息,將其放入單獨的數據結構並稍後檢索它會簡單得多。看到我的回答下面 –