2011-09-14 48 views
0

下面是我在這裏詢問的問題的解決方案assigning a string with a combination of two and separate them in a list。前面的問題是一個例子,下面是實際的程序。打印字典的相應元素

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") 

輸入文件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 

我得到的輸出如下:

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) 

現在的問題是,我該如何顯示完整的字典與值?由於有元音和輔音的分離,我可以看到我沒有以正確的方式打印它。我如何得到輸出如下忽略元音和輔音標記?

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

P.S.對於撇號來說,沒有關係,因爲他們不在字典中。

謝謝。

+0

沒有看到輸入文件很難告訴爲什麼代碼的行爲不當。 –

+0

對不起,我忘了寫輸入文件,我會編輯這個問題。 – zingy

回答

2

更換其中音素添加以下部分:

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]) 

這增加了完整的元組,而不僅僅是名字。

+0

你也想顯示'l'。 – GeneralBecos

+0

修正了這個問題,就像你說的那樣。 –

+0

這些撇號是音節的邊界,他們應該顯示,但他們應該是'沒有',因爲它不是在字典中。我嘗試了上面的建議,但它給出了語法錯誤。 – zingy

1

而不是

phonemeList.append(phoneme) 

您需要:

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

你的打印語句也將得到簡化,將只需要爲:

print phonemeList 
+0

您還需要解釋dipthong情況,如果您在那一刻執行此操作。也就是說,我同意這可能是最好的時機。 –

+0

它不考慮撇號。 – zingy