简体   繁体   中英

printing the corresponding elements of dictionary

Here is the solution to the question I had asked here assigning a string with a combination of two and separate them in a list . The earlier question was an example and below is the actual program.

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']
            }   

def Usage() :
    print "Usage :python co.py phonemeFile"

def coart(phonemeFile):
    """ Function for generating parameter values looking 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 :
                    phonemeList.append((phoneme, param_values.get(phoneme)))

                else : 
                    phonemeList.extend([(phoneme, param_values.get(phoneme)) for phoneme in next_phoneme])

            print "New List"
            print '\n'.join(str(l) for l in phonemeList)

if __name__ == '__main__':
    if len(sys.argv) != 2 : 
        Usage() 

    else :
        phonemeFile = sys.argv[1]

        coart("syllabifiedPhonemes.txt")

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 output I got is the following:

New List
('s', None)
('aa', None)
("'", None)
('m', None)
('ih', None)
("'", None)
('k', None)
('l', None)
('eh', None)
('k', None)
("'", None)
('t', None)
('aa', None)
("'", None)
('n', None)
('ih', None)
('t', None)
("'", None)
('g', None)
('eh', None)
('l', None)
("'", None)
('v', None)
('ae', None)
("'", None)
('n', None)
('ih', None)
("'", None)
('k', None)
('aa', None)
("'", None)
('p', None)
('l', None)
('ao', None)
('ih', None)
('k', None)

Now my question is, how do I display the complete dictionary with the values? I can see that I am not printing it the right way as there is the separation of vowels and consonants. How do I get the output as the following neglecting the vowels and consonants tag?

('s', [(-1, -1), (0.10000000000000001, 1.0), (-1, -1), (0.29999999999999999, 1.0), (0.0, 0.0), (-1, -1)]) 

PS For the apostrophes, None is ok as they are not in the dict.

Thank you.

Replace the section where the phonemes are added with the following:

if next_phoneme is None :
    phonemeList.append((phoneme, param_values.get(phoneme)))
else : 
    phonemeList.extend([(phoneme, param_values.get(phoneme)) for phoneme in next_phoneme])

This adds the full tuples, not just the names.

Instead of

phonemeList.append(phoneme)

You need:

phonemeList.append((phoneme, param_values[phoneme]))

Your print statement will also get simplified and would just need to be:

print phonemeList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM