2016-11-25 431 views
0

我想将英语词典的单词转换为使用python的简单音素。我使用的是python 3.5,而所有的例子都是针对python 2 +的。使用python创建音素

例如在以下文件test.txt的文本:

what a joke 
is your name 
this fall summer singer 
well what do I call this thing mister 

在这里首先我想提取的每个单词,然后将它们转换成音素。这是我想要的结果

what WH AT 
a  AE 
joke JOH K 
is  ES 

....and so on 

这是我的python代码,但它太早,太少。能否请你建议我更多的转换什么WH AT我需要先寻找是否有字母WH然后用更换WH

with open ('test.txt',mode='r',encoding='utf8')as f: 
     for line in f: 
     for word in line.split(): 
      phenome = word.replace('what', word + ' WH AT') 
      print (phenome) 

回答

-1

1,建立一个字典中表型图。然后通过查字典替换单词

# added full phenome mapping to dict below 
dict1 = {'what':'WH AT', 'a':'AE', 'joke':'JOH K', 'is':'ES'} 

with open ('test.txt', encoding='utf8') as f: 
    for line in f: 
     phenome = ' '.join([dict1.get(word, word) for word in line.split()]) 
     print (phenome) 
+0

我正在为整本词典工作。有10万字。你有什么建议可行,但不适用于大量的文字。 – choman

+0

我能想到的是使用pickle保存字典,然后从pickle文件加载,但它仍然需要unpickle并加载到内存,不知道它是否有助于内存和效率 – Skycc