3
def translate(phrase):
    translation = ""
    verb = "eau"
    print(translation)
    for letter in phrase:
        if letter.lower() in verb:
            if letter.isupper():
                translation = translation + " \u0332"
            else:
                translation = translation + " \u0332"
        else:
            translation = translation + letter

    return translation


print(translate(input("Enter a phrase: ")))

I am trying to make it so if it finds the word "eau" together (not if "a" or "e" is alone) it will underline it but the problem is that it just deletes it and then adds a underline

Enter a phrase: MEAU

M ̲ ̲ ̲

I do not only want it for "eau" i want to do it for others like if i add more word("eim") into the verb.

Thanks for your help

MevR
  • 33
  • 5

3 Answers3

1

you can use a re.sub:

import re 
def translate(phrase, words):
    return re.sub('|'.join(words), lambda x: ''.join(e + '\u0332' for e in x.group()), phrase)

translate(input("Enter a phrase: "), words = ['eau', 'vert'])
# input:  Une eau pure pour que le vert dure!

output:

'Une e̲a̲u̲ pure pour que le v̲e̲r̲t̲ dure!'
kederrac
  • 16,819
  • 6
  • 32
  • 55
1

You can do that using re.sub with a replacement function that inserts the underline before each letter of the matched substring. This allows you to match the substring independently of the case:

import re

def underline(match):
    return '\u0332'.join([''] + list(match.group()))

def underline_substring(substring, sentence):
    return re.sub(substring, underline, sentence, flags=re.I)

print(underline_substring('eau', "Eau, c'est beau"))

Output:

E̲a̲u, c'est b̲e̲a̲u

(It looks better in the terminal than here on the web page, the underlines are really under the characters)

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

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().

JL Peyret
  • 10,917
  • 2
  • 54
  • 73