Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. import nltk
  2.  
  3. arpabet = nltk.corpus.cmudict.dict()
  4.  
  5.     def guess_phonemes(self, word):
  6.         '''
  7.        Guesses a word's pronunciation if it cannot be found in the pronunciation dictionary
  8.          Inputs:
  9.            word ------------- the word to be pronounced
  10.          Outputs:
  11.            phones ----------- the word spelled phonetically
  12.        '''
  13.         basicPronunciations = {'a': ['AE'], 'b': ['B'], 'c': ['K'],
  14.                                'd': ['D'],
  15.                                'e': ['EH'], 'f': ['F'], 'g': ['G'],
  16.                                'h': ['HH'],
  17.                                'i': ['IH'],
  18.                                'j': ['JH'], 'k': ['K'], 'l': ['L'],
  19.                                'm': ['M'],
  20.                                'n': ['N'], 'o': ['OW'], 'p': ['P'],
  21.                                'qu': ['K', 'W'], 'r': ['R'],
  22.                                's': ['S'], 't': ['T'], 'u': ['AH'],
  23.                                'v': ['V'],
  24.                                'w': ['W'], 'x': ['K', 'S'], 'y': ['Y'],
  25.                                'z': ['Z'], 'ch': ['CH'],
  26.                                'sh': ['SH'], 'th': ['TH'], 'dg': ['JH'],
  27.                                'dge': ['JH'], 'psy': ['S', 'AY'],
  28.                                'oi': ['OY'],
  29.                                'ee': ['IY'],
  30.                                'ao': ['AW'], 'ck': ['K'], 'tt': ['T'],
  31.                                'nn': ['N'], 'ai': ['EY'], 'eu': ['Y', 'UW'],
  32.                                'ue': ['UW'],
  33.                                'ie': ['IY'], 'ei': ['IY'], 'ea': ['IY'],
  34.                                'ght': ['T'], 'ph': ['F'], 'gn': ['N'],
  35.                                'kn': ['N'], 'wh': ['W'],
  36.                                'wr': ['R'], 'gg': ['G'], 'ff': ['F'],
  37.                                'tt': ['T'],
  38.                                'oo': ['UW'], 'ua': ['W', 'AO'], 'ng': ['NG'],
  39.                                'bb': ['B'],
  40.                                'tch': ['CH'], 'rr': ['R'], 'dd': ['D'],
  41.                                'cc': ['K', 'S'], 'wr': ['R'], 'oe': ['OW'],
  42.                                'igh': ['AY'], 'eigh': 'EY'}
  43.         phones = []
  44.  
  45.         progress = len(word) - 1
  46.         while progress >= 0:
  47.             if word[0:3] in basicPronunciations.keys():
  48.                 for phone in basicPronunciations[word[0:3]]:
  49.                     phones.append(phone)
  50.                 word = word[3:]
  51.                 progress -= 3
  52.             elif word[0:2] in basicPronunciations.keys():
  53.                 for phone in basicPronunciations[word[0:2]]:
  54.                     phones.append(phone)
  55.                 word = word[2:]
  56.                 progress -= 2
  57.             elif word[0] in basicPronunciations.keys():
  58.                 for phone in basicPronunciations[word[0]]:
  59.                     phones.append(phone)
  60.                 word = word[1:]
  61.                 progress -= 1
  62.             else:
  63.                 return None
  64.         return phones
  65.  
  66.     def handle_new_name(self, message):
  67.         name = message.data.get("Name")
  68.         phonemes = None
  69.         if " " in name:
  70.             total_phonemes = []
  71.             names = name.split(" ")
  72.             for name in names:
  73.                 if name not in arpabet.keys():
  74.                     guess = self.guess_phonemes(name)
  75.                     if guess is None:
  76.                         total_phonemes = None
  77.                         break
  78.                     else:
  79.                         total_phonemes.extend(guess).extend(".")
  80.                 else:
  81.                     total_phonemes.extend(arpabet[name][0]).extend(".")
  82.             phonemes = " ".join(total_phonemes[:-1])
  83.         elif name in arpabet.keys():
  84.             phonemes = arpabet[name][0]
  85.         else:
  86.             guess = self.guess_phonemes(name)
  87.             if guess is not None:
  88.                 phonemes = guess
  89.  
  90.         self.log.debug("new name phonemes: " + phonemes)
  91.         if phonemes:
  92.             listener_config = self.config_core.get("listener")
  93.             listener_config["wake_word"] = name
  94.             hotwords_config = self.config_core.get("hotwords")
  95.             hotwords_config[name] = {
  96.                 "module": "pocketsphinx",
  97.                 "phonemes": phonemes,
  98.                 "phoneme_duration": 120,
  99.                 "threshold": 1e-90,
  100.                 "active": True}
  101.             config = {"listener": listener_config,
  102.                       "hotwords": hotwords_config}
  103.             self.update_user_config(config)
  104.             self.speak_dialog("new.name", {"name": name})
  105.         else:
  106.             self.speak_dialog("invalid.name", {"name": name})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement