concept poem in category data

appears as: poem, poems, poems, poem
Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.

A new poem has taken global culture by storm, but nobody can definitively identify the mysterious author. Two poets are claiming the poem and have provided you with terabytes of their unpublished poems so you can validate which poet is more likely to be the true author of the poem.

We can see how that would play out in figure 4.6, where we transform a poem into our desired data structure. We start with the raw poem text, as the poet intended it, but after it’s cleaned, the text is ready for us to analyze.

Figure 4.6. We can use a class containing a regular expression to transform a poem into a list of words.
Listing 4.7. A poem cleaner class
class PoemCleaner:
    def __init__(self):
        self.r = re.compile(r'[.,;:?!-]')           #1

    def clean_poem(self, fp):
        with open(fp) as poem:
            no_punc = self.r.sub("",poem.read())    #2
            return no_punc.lower().split()          #3

A straightforward step like this is also something that’s good to get out of the way first. To read in the poems by each author, let’s define two iterables using iglob: one for each author. This is a quick two-liner, as we can see in the following listing.