Singularizing plural nouns
As we saw in the previous recipe, the transformation process can result in phrases such as recipes book
. This is a NNS
followed by a NN
, when a more proper version of the phrase would be recipe book
, which is a NN
followed by another NN
. We can do another transform to correct these improper plural nouns.
How to do it...
The transforms.py
script defines a function called singularize_plural_noun()
which will depluralize a plural noun (tagged with NNS
) that is followed by another noun:
def singularize_plural_noun(chunk): nnsidx = first_chunk_index(chunk, tag_equals('NNS')) if nnsidx is not None and nnsidx+1 < len(chunk) and chunk[nnsidx+1][1][:2] == 'NN': noun, nnstag = chunk[nnsidx] chunk[nnsidx] = (noun.rstrip('s'), nnstag.rstrip('S')) ...
Get Python 3 Text Processing with NLTK 3 Cookbook 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.