Answer:
#section 1
import re
thesaurus = {
"happy" : "glad",
"sad" : "bleak",
}
text =input('Enter text: ').lower()
#section 2
def synomReplace(thesaurus, text):
<em> # Create a regular expression from the dictionary keys
</em>
regex = re.compile("(%s)" % "|".join(map(re.escape, thesaurus.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda x: thesaurus[x.string[x.start():x.end()]].upper(), text)
print(synomReplace(thesaurus, text))
Explanation:
#section 1
In this section, the regular expression module is imported to carry out special string operations. The thesaurus is initialized as a dictionary. The program then prompts the user to enter a text.
#section 2
In the section, we create a regular expression that will search for all the keys and another one that will substitute the keys with their value and also convert the values to uppercase using the .upper() method.
I have attached a picture for you to see the result of the code.