I am trying to create a list of the input with their corresponding values accessing from the global nested dictionary. Here is the code,
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开发者_JS百科][phones])) for phoneme in next_phoneme])
print "New List"
print '\n'.join(str(l) for l in phonemeList)
The input file syllabifiedPhonemes.txt has the following contents:
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
The if-else statement is not right as far as I can see. I got the following error:
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'
I changed the if-else statement to the following and got rid of the error.
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])
But now I got the output as a huge list and I presume the program iterates through the dict many times and prints the whole dict again and again instead of just displaying the values of the given input. Could someone point out where I am wrong? Thank you.
Try something like this. It is much simpler and much more efficient (I think)
import sys
param_values = {
'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)] ,
'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']
}
phonemeList = []
with open("syllabifiedPhonemes.txt", "r") as pFile :
for line in pFile :
l = line.split()
for phoneme in l :
if phoneme != "'":
next_phoneme = diphthong.get(phoneme)
if next_phoneme is None :
# exploring the dict param_values extracting each nested dict
phonemeList.append((phoneme, param_values[phoneme]))
else :
phonemeList.extend([(phoneme, param_values[phoneme]) for phoneme in next_phoneme])
print "New List"
print '\n'.join(str(l) for l in phonemeList)
If you need the consonant/vowel information it is better storing it somewhere else. Storing it here complicates the code too much.
NOTE: I took out the function for testing purposes. Obviously you should put this back in your code ;)
param_values.get
should not be used there, since you already used param_values[...]
to read the value.
i.e. instead of:
param_values.get(param_values[group][phones])
Use:
param_values[group][phones]
You can use get
instead of (not in addition to) [...]
if you want a default value in case the key doesn't exist, but in this case that isn't necessary.
This is in reply to your current new code version snippet below:
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])
If I understand correctly the goal, I think what you want can be written as:
next_phonemes = diphthong.get(phoneme, [phoneme])
for next_phoneme in next_phonemes:
# exploring the dict param_values extracting each nested dict
for group_val in param_values.itervalues(): # 'group_val' refers to values associated to vowels or consonants
if next_phoneme in group_val:
phonemeList.append((next_phoneme, group_val[next_phoneme])
break # Once we found the next phoneme, no need to look further
Here's what you're doing:
param_values.get(param_values['vowels']['aa'])
But the problem is that
param_values['vowels']['aa']
returns a list and you're trying to get a value in the map using a list, which is unhashable and you can't use it as key in the map.
I believe that what you trying to do is just get what
param_values['vowels']['aa']
returns
Try that in the python console and you will find out the error.
Check this out:
What can be a dictionary key?
Strings can be dictionary keys:
mydict = {"dog":"Hund", "rhinoceros":"Nashorn"}
Integers can be dictionary keys. The following dictionary (somewhat redundantly)
maps prime numbers to their rank, e.g. 2 is the first prime number.
prime_nums = {2:1, 3:2, 5:3, 7:4, 11:5}
Floating point numbers can be dictionary keys as well:
>>> mydict = {3.1415 : "pi", 2.71828 : "e" }
>>> mydict[3.1415]
'pi’
Lists cannot be dictionary keys
>>> mydict = {[1,2] : "wont_work"}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
More info
Ok, first of all, try iterating over the dict using the following code
for k, v in dict.items():
# k is the key
# v is the value
pass
It will make your code cleaner and maybe it will solve your problem.
精彩评论