Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nltk
- arpabet = nltk.corpus.cmudict.dict()
- def guess_phonemes(self, word):
- '''
- Guesses a word's pronunciation if it cannot be found in the pronunciation dictionary
- Inputs:
- word ------------- the word to be pronounced
- Outputs:
- phones ----------- the word spelled phonetically
- '''
- basicPronunciations = {'a': ['AE'], 'b': ['B'], 'c': ['K'],
- 'd': ['D'],
- 'e': ['EH'], 'f': ['F'], 'g': ['G'],
- 'h': ['HH'],
- 'i': ['IH'],
- 'j': ['JH'], 'k': ['K'], 'l': ['L'],
- 'm': ['M'],
- 'n': ['N'], 'o': ['OW'], 'p': ['P'],
- 'qu': ['K', 'W'], 'r': ['R'],
- 's': ['S'], 't': ['T'], 'u': ['AH'],
- 'v': ['V'],
- 'w': ['W'], 'x': ['K', 'S'], 'y': ['Y'],
- 'z': ['Z'], 'ch': ['CH'],
- 'sh': ['SH'], 'th': ['TH'], 'dg': ['JH'],
- 'dge': ['JH'], 'psy': ['S', 'AY'],
- 'oi': ['OY'],
- 'ee': ['IY'],
- 'ao': ['AW'], 'ck': ['K'], 'tt': ['T'],
- 'nn': ['N'], 'ai': ['EY'], 'eu': ['Y', 'UW'],
- 'ue': ['UW'],
- 'ie': ['IY'], 'ei': ['IY'], 'ea': ['IY'],
- 'ght': ['T'], 'ph': ['F'], 'gn': ['N'],
- 'kn': ['N'], 'wh': ['W'],
- 'wr': ['R'], 'gg': ['G'], 'ff': ['F'],
- 'tt': ['T'],
- 'oo': ['UW'], 'ua': ['W', 'AO'], 'ng': ['NG'],
- 'bb': ['B'],
- 'tch': ['CH'], 'rr': ['R'], 'dd': ['D'],
- 'cc': ['K', 'S'], 'wr': ['R'], 'oe': ['OW'],
- 'igh': ['AY'], 'eigh': 'EY'}
- phones = []
- progress = len(word) - 1
- while progress >= 0:
- if word[0:3] in basicPronunciations.keys():
- for phone in basicPronunciations[word[0:3]]:
- phones.append(phone)
- word = word[3:]
- progress -= 3
- elif word[0:2] in basicPronunciations.keys():
- for phone in basicPronunciations[word[0:2]]:
- phones.append(phone)
- word = word[2:]
- progress -= 2
- elif word[0] in basicPronunciations.keys():
- for phone in basicPronunciations[word[0]]:
- phones.append(phone)
- word = word[1:]
- progress -= 1
- else:
- return None
- return phones
- def handle_new_name(self, message):
- name = message.data.get("Name")
- phonemes = None
- if " " in name:
- total_phonemes = []
- names = name.split(" ")
- for name in names:
- if name not in arpabet.keys():
- guess = self.guess_phonemes(name)
- if guess is None:
- total_phonemes = None
- break
- else:
- total_phonemes.extend(guess).extend(".")
- else:
- total_phonemes.extend(arpabet[name][0]).extend(".")
- phonemes = " ".join(total_phonemes[:-1])
- elif name in arpabet.keys():
- phonemes = arpabet[name][0]
- else:
- guess = self.guess_phonemes(name)
- if guess is not None:
- phonemes = guess
- self.log.debug("new name phonemes: " + phonemes)
- if phonemes:
- listener_config = self.config_core.get("listener")
- listener_config["wake_word"] = name
- hotwords_config = self.config_core.get("hotwords")
- hotwords_config[name] = {
- "module": "pocketsphinx",
- "phonemes": phonemes,
- "phoneme_duration": 120,
- "threshold": 1e-90,
- "active": True}
- config = {"listener": listener_config,
- "hotwords": hotwords_config}
- self.update_user_config(config)
- self.speak_dialog("new.name", {"name": name})
- else:
- self.speak_dialog("invalid.name", {"name": name})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement