I am not sure exactly why you'd test letters within your verb. You want the whole word underlined, but only that word.
What I am doing here instead is to test all words for a match to your target and translate only that.
def translate_word(verb):
translation = ""
for letter in verb:
translation = translation + letter + "\u0332"
return translation
def translate(phrase, verb):
if isinstance(verb, str):
verb = verb.split()
elif isinstance(verb, (list, set, tuple)):
pass
else:
raise TypeError(f"unknown type for verb:{type(verb)}")
#convert the verb into a set, casefold for upper/lower/accented chars
verbs = {v.casefold() for v in verb }
li = []
for word in phrase.split():
#upper/lowercase and accented chars
if word.casefold() in verbs:
li.append(translate_word(word))
else:
li.append(word)
return " ".join(li)
output:
the second test had ("eau", "si")
as input verbs.
Une e̲a̲u̲ pure pour que le vert dure, c'est si beau!
Une E̲a̲u̲ pure pour que le vert dure, c'est s̲i̲ beau!
No regexes were hurt in this program.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Finally, phrase.split()
is an approximate strategy. If you have a phrase with 2 spaces, like sometimes used after colons, you'd be losing one space this way. Or if you had une eau, si douce
then you'd be missing that verb match.
See How to keep the delimiters of Regex.Split? for a more comprehensive approach, appropriately based on regexes. Something based on a regex like \s+,.
would be your word separator of choice instead of str.split()
.