Reputation: 53
I am having trouble figuring out why my code is not printing out anything after I input a value for word. I can input a word, but it does not output anything after evaluating through the while loop. What are your thoughts?
ai = "eye-"
ae = "eye-"
ao = "ow-"
au = "ow-"
ei = "ay-"
eu = "eh-oo-"
iu = "ew-"
oi = "oy-"
ou = "ow-"
ui = "ooey-"
a = "ah-"
e = "eh-"
i = "ee-"
o = "oh-"
u = "oo-"
p = "p"
k = "k"
h = "h"
l = "l"
m = "m"
n = "n"
word = input("Please enter a Hawaiian word you want pronounced")
character_count1 = int(0)
character_count2 = int(1)
pronunciation = ""
while character_count1 < len(word):
if word[character_count1:character_count2] == ai or word[character_count1:character_count2] == "ae"or word[character_count1:character_count2] == "ao"or word[character_count1:character_count2] == "ei"or word[character_count1:character_count2] == "eu"or word[character_count1:character_count2] == "iu"or word[character_count1:character_count2] == "oi"or word[character_count1:character_count2] == "ou":
print("your word",pronunciation + word[character_count1:character_count2])
character_count1 + 2 and character_count2 + 2
elif word[character_count1:character_count1] == a or word[character_count1:character_count1] == e or word[character_count1:character_count1] == i or word[character_count1:character_count1] == o or word[character_count1:character_count1] == p or word[character_count1:character_count1] == k or word[character_count1:character_count1] == h or word[character_count1:character_count1] == l or word[character_count1:character_count1] == m or word[character_count1:character_count1] == n:
print("your word",pronunciation + word[character_count1:character_count1] )
character_count1 + 1 and character_count2 + 1
Upvotes: 1
Views: 2693
Reputation: 6994
The folks in the answers and comments who have said "use a dictionary" are right, but you can't just loop through the input a character at a time because you have overlapping matches. Without looking at the next character, you can't tell if "a" is part of "ai" or "an", and those cases are handled differently. Here is a complete solution with annotation that handles the subtlety and provides an informative error message when you encounter an illegal string.
hawaiian_pronunciation = {
"ai": "eye-",
"ae": "eye-",
"ao": "ow-",
"au": "ow-",
"ei": "ay-",
"iu": "ew-",
"oi": "oy-",
"ui": "ooey-",
"a": "ah-",
"e": "eh-",
"i": "ee-",
"o": "oh-",
"u": "oo-",
"p": "p",
"k": "k",
"h": "h",
"l": "l",
"m": "m",
"n": "n"
}
def segment(word, start):
# we have to consider the longest possible segment first
# so that hai gets tokenized h-ai instead of h-a-i
for length in (2,1):
# check if the length of the segment puts us past the end of the word
# the upper bound can be equal to the length of the word since the
# string[upper_bound] is not actually included in the range
if start+length > len(word):
continue
# the input segment we are considering
input = word[start:start+length]
# is it in our dictionary?
if input in hawaiian_pronunciation:
# if it is get the corresponding pronunciation
output = hawaiian_pronunciation[input]
# return the output and length for a successful match
return output, length
# if no candidate matches, raise an exception describing where you were
# when you failed to find a match
raise Exception("cannot match word {} at position {}, bad segment {}",
word, start, word[start:])
def pronounce(word):
"generate pronunciation from word"
# build a list of strings
out = []
# we have to use a while loop and an explicit index
# because we can jump by one or two spaces depending on the length of the match
start = 0
while start < len(word):
# when we get a match, append the new sound and
# advance the appropriate number of spaces
new_sound, length = segment(word, start)
out.append(new_sound)
start += length
return "".join(out)
def main():
print pronounce("hai")
main()
Upvotes: 0
Reputation: 11144
What are you trying to achieve is pretty easy, if you use a data structure called dictionary, a very basic data structure in python. Change the data structure like that:
dic={"ai" :"eye-","ae" :"eye-","ao": "ow-","au" :"ow-"......}
Now you can access the values (Pronunciation) with the keys (words).
like this,
dic["ai"]
You will get:
eye-
So, now let's try to get the solution:
Define a dictionary.
dic={"ai" :"eye-","ae" :"eye-","ao": "ow-","au" :"ow-"......}
Take the input, better use raw_input if you are not using python3
word = raw_input("Please enter a Hawaiian word you want pronounced")
Split the input by white spaces and form a list.
lst=word.split()
Use the elements of lst
as dictionary key
to find the value
. Iterate through the list and check if the input matches to any key of dic
for i in lst:
print dic.get(i)
None
will be printed if the key
doesn't exist.
As, your requirement isn't quite clear to me, i have included all the things needed to solve the problem.
So, use them where needed and solve the problem.
Happy coding.
Upvotes: 3