Swapping infinitive phrases

An infinitive phrase has the form A of B, such as book of recipes. These can often be transformed into a new form while retaining the same meaning, such as recipes book.

How to do it...

An infinitive phrase can be found by looking for a word tagged with IN. The swap_infinitive_phrase() function, defined in transforms.py, will return a chunk that swaps the portion of the phrase after the IN word with the portion before the IN word:

def swap_infinitive_phrase(chunk): def inpred(wt): word, tag = wt return tag == 'IN' and word != 'like' inidx = first_chunk_index(chunk, inpred) if inidx is None: return chunk nnidx = first_chunk_index(chunk, tag_startswith('NN'), start=inidx, step=-1) or 0 return chunk[:nnidx] + chunk[inidx+1:] ...

Get Natural Language Processing: Python and NLTK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.