Python 夏威夷发音者
所以我应该制作一个程序,接收输入的夏威夷单词并输出发音 规则:Python 夏威夷发音者,python,python-3.x,Python,Python 3.x,所以我应该制作一个程序,接收输入的夏威夷单词并输出发音 规则: p、 k、h、l、m、n的发音与英语中的发音相同 W作为单词的开头或字母a、u或o后的字母,发音为W。但如果它在字母i或e之后,则发音为v 元音:a读作“啊”,e读作“呃”,i读作“ee”,o读作“哦”,u读作“oo” 对于组元音:ai发音为“eye”,ae发音为“eye”,ao发音为“ow”,au发音为“ow”,ei发音为“ay”,eu发音为“eh-oo”,iu发音为“ew”,oi发音为“oyo”,ou发音为“ow”,ui发音为
- p、 k、h、l、m、n的发音与英语中的发音相同
- W作为单词的开头或字母a、u或o后的字母,发音为W。但如果它在字母i或e之后,则发音为v
- 元音:a读作“啊”,e读作“呃”,i读作“ee”,o读作“哦”,u读作“oo”
- 对于组元音:ai发音为“eye”,ae发音为“eye”,ao发音为“ow”,au发音为“ow”,ei发音为“ay”,eu发音为“eh-oo”,iu发音为“ew”,oi发音为“oyo”,ou发音为“ow”,ui发音为“ooey”
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()
辅音=('p','k','h','l','m','n')
def检查(有效,word):
对于word中的x:
x=x.下()
如果有效。计数(x)=0:
打印(x,“不是有效的夏威夷单词”)
返回错误
返回真值
def转换(word):
convert=“”
x=0
而x
谢谢。我认为你最好使用字典:
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)
您的代码中存在一些缩进错误,需要在运行之前进行修复。很好的尝试,但您的元音对词典将永远不会被查询。当您有一个元音对时,元音dict将为
tr
返回一个非None
值,因此您永远不会输入if条件来检查元音对的可能性。
>>> pronounce('aloha')
'ah-loh-hah'
>>> pronounce('mahalo')
'mah-hah-loh'