所以我应该做一个程序,将输入的夏威夷词输入并输出发音。
规则:
我要做什么: 因此,我已经能够识别元音和分组的元音并将其放入转换后的单词中,但是它完全跳过了不是元音或分组元音的字母。我很难找到我的错误。例如,如果我输入“ aloha”,则应输出“ ah-loh-hah”;如果输入“ mahalo”,则应输出“ mah-hah-loh”,但它会完全跳过一些字符。用我的代码,aloha只会打印出“ ah-ah”,这是错误的。
下面是我的代码:
consonants = ('p','k','h','l','m','n')
def check(valid, word):
for x in word:
x = x.lower()
if valid.count(x) == 0:
print(x, "is not a valid hawaiian word")
return False
return True
def convert(word):
convert = ""
x = 0
while x < len(word)-1:
word = word.lower()
if word[x] == "a":
after_x = word[x+1]
if after_x == "i" or after_x == "e":
convert = convert + "eye-"
x = x+1
elif after_x == 'o' or after_x == "u":
convert = convert + "ow-"
x=x+1
else:
convert = convert + "ah-"
elif word[x] == "e":
after_x = word[x+1]
if after_x == "i":
convert = convert + "ay-"
x = x+1
elif after_x == 'u':
convert = convert + "eh-oo-"
x= x+1
elif after_x == 'w': #
convert = convert + "v"
x=x+1
else:
convert = convert + "eh-"
elif word[x] == "i":
after_x = word[x+1]
if after_x == "u":
convert = convert + "ew-"
x = x+1
else:
convert = convert + "ee-"
elif word[x] == "o":
after_x = word[x+1]
if after_x == "i":
convert = convert + "oy-"
x = x+1
elif after_x == "u":
convert = convert + "ow-"
x = x+1
elif after_x == 'w': #
convert = convert + "v"
x = x+1
else:
convert = convert + "oh-"
elif word[x] == "u":
after_x = word[x+1]
if after_x == "i":
convert = convert + "ooey-"
x = x+1
else:
convert = convert + "oo-"
elif word[x] == consonants:
convert = convert + consonants
elif word[x] == " " and convert[len(convert)-1] == "-":
convert = convert[0:len(convert)-1] + " "
elif word[x] == "\'" and convert[len(convert)-1] == "-":
convert = convert[0:len(convert)-1] + "'"
else:
convert = convert + word[x]
x = x +1
if x < len(word):
m = word[len(word)-1]
m = m.lower()
if m == "a" or m == "e" or m == "o":
convert = convert + m + "h"
elif m == "i":
convert = convert + "ee"
elif m == "u":
convert = convert + "oo"
else:
convert = convert + m
if convert[len(convert)-1] == '-':
convert = convert[0:len(convert)-1]
convert = convert.upper()
return convert
def main():
valid = ['p','k','h','l','m','n','w','a','e','i','o','u',' ', '\'']
while True:
word = input("Enter a Hawaiian Word to Pronounce")
word = word.strip()
if(check(valid,word)):
converted_word = convert(word)
converted_word = converted_word.upper()
print(word + " is pronounced" , converted_word)
repeat = input("Would you like to enter another word Y/YES/N/NO")
repeat = repeat.upper()
if repeat == 'N' or repeat == 'NO':
break
else:
main()
main()
谢谢。
答案 0 :(得分:2)
我认为在您的情况下,最好使用字典:
VOWELS = {
'a': 'ah',
'e': 'eh',
'i': 'ee',
'o': 'oh',
'u': 'oo'
}
VOWEL_PAIRS = {
'ai': 'eye',
'ae': 'eye',
'ao': 'ow',
'au': 'ow',
'ei': 'ay',
'eu': 'eh-oo',
'iu': 'ew',
'oi': 'oyo',
'ou': 'ow',
'ui': 'ooey',
'iw': 'v',
'ew': 'v'
}
这是一个将给定单词转换为转录的函数:
def pronounce(word):
chars = word.lower()
i = 0
result = []
while i < len(chars):
char = chars[i]
if i < len(chars) - 1:
pair = char + chars[i + 1]
tr = VOWEL_PAIRS.get(pair)
if tr is None:
tr = VOWELS.get(char)
else:
i = i + 1
else:
tr = VOWELS.get(char)
if tr is not None and i < len(chars) - 1:
tr = tr + '-'
result.append(tr or char)
i = i + 1
return ''.join(result)
结果如下:
>>> pronounce('aloha')
'ah-loh-hah'
>>> pronounce('mahalo')
'mah-hah-loh'