15 The Kentucky Friar: More regular expressions

 

I grew up in the American Deep South where we tend to drop the final “g” of words ending in “ing,” like “cookin’” instead of “cooking.” We also tend to say “y’all” for the second-person plural pronoun, which makes sense because Standard English is missing a distinctive word for this. In this exercise, we’ll write a program called friar.py that will accept some input as a single positional argument and transform the text by replacing the final “g” with an apostrophe (’) for two-syllable words ending in “ing” and changing “you” to “y’all.” Granted, we have no way to know if we’re changing the first- or second-person “you,” but it makes for a fun challenge nonetheless.


Figure 15.1 is a string diagram that will help you see the inputs and outputs. When run with no arguments or with the -h or --help flags, your program should present the following usage statement:

$ ./friar.py -h
usage: friar.py [-h] text

Southern fry text

positional arguments:
 text        Input text or file

optional arguments:
 -h, --help  show this help message and exit
Figure 15.1 Our program will modify the input text to give it a Southern lilt.

15.1 Writing friar.py

15.1.1 Splitting text using regular expressions

15.1.2 Shorthand classes

15.1.3 Negated shorthand classes

15.1.4 Using re.split() with a captured regex

15.1.5 Writing the fry() function

15.1.6 Using the fry() function

15.2 Solution

15.3 Discussion

15.3.1 Writing the fry() function manually

15.3.2 Writing the fry() function with regular expressions

15.4 Going further

Summary