Python夏威夷语发音程序连字符位置错误修复求助
修复夏威夷语发音程序的连字符位置错误
问题描述
正在开发一款可接收夏威夷语单词(含短语)并输出其发音的Python程序。当前程序能正确转换字母发音,但连字符的放置位置不符合预期,需要修正以匹配目标输出格式。
发音规则
- p、k、h、l、m、n的发音与英语一致;
- 当W位于词首或字母a、u、o之后时发音为w,位于i或e之后时发音为v;
- 元音发音:a读作"ah",e读作"eh",i读作"ee",o读作"oh",u读作"oo";
- 元音组合发音:ai读作"eye",ae读作"eye",ao读作"ow",au读作"ow",ei读作"ay",eu读作"eh-oo",iu读作"ew",oi读作"oyo",ou读作"ow",ui读作"ooey"。
现有代码
vowels = {"a":"ah","e":"eh", "i":"ee","o":"oh","u":"oo"} doubleVowels = {"ai":"eye","ae":"eye","ao":"ow","eu":"eh-oo","ei":"ay","au":"ow","iu":"ew","oi":"oyo","ou":"ow","ui":"ooey"} cons = "wphkmln" def validate(validLetters, word): for letter in word: letter = letter.lower() if validLetters.count(letter) == 0: print (letter, "is a invalid hawaiian character") return False return True def pronounce(word): output = "" vowels_str = "aeiou" i = 0 while i < len(word): if i > 0 and ((word[i] in vowels_str and word[i - 1] in vowels_str + cons) or (word[i] in cons and word[i - 1] in vowels_str)): output += "-" if i < len(word) - 1 and word[i:i + 2] in doubleVowels: output += doubleVowels[word[i:i + 2]] i += 2 elif i < len(word) - 2 and word[i:i + 3] in doubleVowels: output += doubleVowels[word[i:i + 3]] i += 3 elif word[i] in vowels: output += vowels[word[i]] i += 1 elif word[i] in cons: if word[i] == 'w': if i == 0 or word[i - 1] in 'au': output += 'w' elif word[i - 1] in 'ie': output += 'v' else: output += word[i] i += 1 elif word[i] == "'": output += "'" i += 1 elif word[i] == " ": output += " " i += 1 return output.capitalize() def main(): valid_hawaiian_characters = "aeiouwphkmln' " done = False while not done: gaveValidWord = False while not gaveValidWord: word = input("give word: ").lower() gaveValidWord = validate(valid_hawaiian_characters, word)
当前输出
give word: aloha Pronunciation: Ah-l-oh-h-ah go again? yes give word: keikikane Pronunciation: K-ay-k-ee-k-ah-n-eh go again? yes give word: humuhumunukunukuapua'a Pronunciation: H-oo-m-oo-h-oo-m-oo-n-oo-k-oo-n-oo-k-oo-ah-p-oo-ah'ah go again? no
预期输出
Input: aloha -> Output: Ah-loh-hah Input: keikikane -> Output: Kay-kee-kah-neh Input: humuhumunukunukuapua'a -> Output: Hoo-moo-hoo-moo-noo-koonoo-koo-ah-poo-ah'ah Input: e komo mai -> Output: Eh koh-moh meye
解决方案
核心问题在于原代码的连字符判断逻辑过于零散,没有按音节单元处理发音。以下是修复后的完整代码,关键修改点附在代码后:
vowels = {"a":"ah","e":"eh", "i":"ee","o":"oh","u":"oo"} doubleVowels = {"ai":"eye","ae":"eye","ao":"ow","eu":"eh-oo","ei":"ay","au":"ow","iu":"ew","oi":"oyo","ou":"ow","ui":"ooey"} cons = "wphkmln" def validate(validLetters, word): for letter in word: letter = letter.lower() if validLetters.count(letter) == 0: print(f"{letter} is an invalid hawaiian character") return False return True def pronounce(word): syllables = [] current_syllable = "" vowels_str = "aeiou" i = 0 word_len = len(word) while i < word_len: char = word[i] # 处理空格:保存当前音节,添加空格分隔符,重置当前音节 if char == " ": if current_syllable: syllables.append(current_syllable) current_syllable = "" syllables.append(" ") i += 1 continue # 处理okina:直接加入当前音节,不拆分 if char == "'": current_syllable += char i += 1 continue # 优先处理双元音组合 if i < word_len - 1 and word[i:i+2] in doubleVowels: current_syllable += doubleVowels[word[i:i+2]] i += 2 # 双元音是完整音节,加入音节列表并重置 syllables.append(current_syllable) current_syllable = "" continue # 处理单个元音 if char in vowels: current_syllable += vowels[char] syllables.append(current_syllable) current_syllable = "" i += 1 continue # 处理辅音 if char in cons: # 处理w的特殊发音 if char == "w": # 词首的w发w音 if i == 0: current_syllable += "w" else: prev_char = word[i-1] if prev_char in "auo": current_syllable += "w" elif prev_char in "ie": current_syllable += "v" else: current_syllable += char i += 1 # 辅音后必须接元音,所以继续处理下一个字符 continue # 处理最后一个未加入的音节 if current_syllable: syllables.append(current_syllable) # 拼接音节:空格直接保留,其他音节用连字符连接 output = "" for idx, syl in enumerate(syllables): if syl == " ": output += " " else: if idx > 0 and syllables[idx-1] != " ": output += "-" output += syl # 首字母大写,其余保持原转换后的大小写 return output.capitalize() def main(): valid_hawaiian_characters = "aeiouwphkmln' " done = False while not done: gaveValidWord = False while not gaveValidWord: word = input("give word: ").lower() gaveValidWord = validate(valid_hawaiian_characters, word) pronunciation = pronounce(word) print(f"Input: {word} -> Output: {pronunciation}") # 询问是否继续 again = input("go again? ").lower() if again not in ["yes", "y"]: done = True
关键修改说明
- 按音节单元处理:不再逐个字符判断连字符,而是先收集每个完整的音节(双元音、辅音+元音、单个元音),最后统一拼接时添加连字符,避免零散添加错误。
- 修复w的发音判断:补充了o之后发w的规则(原规则第2条包含o,原代码遗漏)。
- 处理空格与okina:空格作为短语分隔符,直接保留;okina与后续字符连为一个音节,不拆分。
- 完善main函数:添加了发音输出和继续询问的逻辑,匹配示例中的交互流程。
- 修正连字符拼接逻辑:仅在非空格分隔的音节之间添加连字符,避免空格前后出现多余连字符。
内容的提问来源于stack exchange,提问作者kq2002
相关产品推荐
相关产品推荐

