Haigy Paigy is as a children's invented language which sounds exactly like English, except that "aig" is inserted before the vowel sound of each syllable. E.g., the English word "hello" becomes "haigellaigo" in Haigy Paigy. Write a set of regular expressions that will automatically translate sentences from English into Haigy Paigy. The input of your program will be a string containing one English sentence. Your regular expressions should translate this sentence into Haigy Paigy.

Respuesta :

Answer:

def haigyPaigy(dict, text):

 # Create a regular expression  from the dictionary keys

 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))

 # For each match, look-up corresponding value in dictionary

 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)  

text = input("Enter text: ")

dict = {

   "a" : "aiga",

   "e" : "aige",

   "i" : "aigi",

   "o" : "aigo",

   "u" : "aigu",

   

 }  

print(haigyPaigy(dict, text))

Explanation:

Step 1:

define the haigyPaigy function: this function takes its argument from a dictionary and the text you you want to convert.

def haigyPaigy(dict, text):

In the function, there are two regular expressions:

  • regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))

this is a regular expression for the dictionary keys, it looks through the text for a match, it acts like a scanner, simply put, it looks for [a,e,i,o,u].

re.compile(pattern, repl, string): is used to combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it.

  • return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)  

For each match, it looks up the key in the dictionary and replaces it with a corresponding value.

Step 2:

prompt the user to input the text that needs conversion to Haigy Paigy

text = input("Enter text: ")

Step 3:

Create the dictionary by inputting the key and the corresponding value.

NOTE: the key is what you want to replace in the text and the value is what you're replacing it with.

dict = {

   "a" : "aiga",

   "e" : "aige",

   "i" : "aigi",

   "o" : "aigo",

   "u" : "aigu",

   

 }  

Step 4:

Call the function and print the translated text to your screen.

print(haigyPaigy(dict, text))