Before we start working with our data, let's have some fun and play some language games with text that we are now able to read from files. A language game is a systematic manipulation of a word that is used to conceal a conversation. Please complete pig_latin(word) and izzle(word).

Pig Latin

Pig Latin is one of the most famous English language games. You can convert an English word into Pig Latin by moving all the consonants up to the first vowel to the end of the word and then appending "ay". E.g. "hello, Chris how are you?" becomes "Ellohay, ischray owhay areay ouyay?" Let's write a function that takes in a single word and return its Pig Latin translation.

Complete the following function after the read_file function you just defined in word_analyzer.py:

def pig_latin(word):
    """ Returns the pig latin translation of a word. """
    return ???

Hint: Make a list of vowels [a, e, i, o, u].

To test if your code works you can add the following line to the end of the file.

print(pig_latin("hello"))

Try running word_analyzer.py, and you should get:

$ python word_analyzer.py
ellohay

You may also test your code by running the following line.

$ python -m doctest word_analyzer.py

-izzle Speak

Mac Dre an American Rapper is accredited for coming up with the next language game we will implement, however Snoop Dogg (D-O-double-g) is responsible for its wide spread use. You can convert an English word into -izzle speak by replacing the last vowel and all its following consonants with "-izzle." E.g. "Merry Christmas" becomes "Mizzle Christmizzle." If a word only has one vowel like 'and' and 'a' or it doesn't contain a cardinal vowel, [a, e, i, o, u], like the word 'my' you can either replace the entire word with 'izzle' or append '-izzle' to the end of the word.

Complete the following function after the pig_latin function you just defined:

def izzle(word):
    """ Returns the izzle translation of a word. """
    return ???

You can test your function using the same methodology we used for Pig Latin.

You may also use the autograder by running the following line.

$ python -m doctest word_analyzer.py

Higher Order Manipulation

Now let's apply our language games to the Gettysburg Address. We could make two separate functions that apply the language games we just defined to the text, but that would mean writing some redundant code. Just like in Snap! you can pass in a function as input in Python. Refer to the example below which takes in a language game and applies it to the word "hello."

HOF

def hello_language_game(languageGame):
    return languageGame("hello")
                    
izzle Game

	hello_language_game(izzle)
                    

Notice that in the example above, the function izzle is passed into hello_language_game without any parenthesis. In Snap! we use call to evaluate the input function, but in Python we do this by using parenthesis: languageGame("hello").

With this knowledge write the following required function:

def apply_language_game(text, language_game):
    """Takes a text and a language_game function as inputs"""
    """ Returns the language game function applied to every word of the text. """
    return ???

To split a sentence into a list of words, use the split function. For example, "hello my name is josh".split() would return ["hello", "my", "name", "is", "josh"].

To convert a list into a string use " ".join(). E.g. " ".join(["hello", "world"]) evaluates to "hello world"

To test if your code works you can add the following line to the end of the file. This will work if your text_processing folder is stored in the same location as word_analyzer.py. If word_analyzer.py is inside of the text_processing folder, change the string to just "gettysburg.txt".

text = read_file("text_processing/gettysburg.txt")
print(apply_language_game(text, izzle))
	 
$ python word_analyzer.py
Foizzle scorizzle izzle sevizzle yeizzle agizzle oizzle fathizzle broizzle ...

You may also run the following line to test your code with the autograder.

$ python -m doctest word_analyzer.py