Book Image

Python Text Processing with NLTK 2.0 Cookbook

By : Jacob Perkins
Book Image

Python Text Processing with NLTK 2.0 Cookbook

By: Jacob Perkins

Overview of this book

<p>Natural Language Processing is used everywhere – in search engines, spell checkers, mobile phones, computer games – even your washing machine. Python's Natural Language Toolkit (NLTK) suite of libraries has rapidly emerged as one of the most efficient tools for Natural Language Processing. You want to employ nothing less than the best techniques in Natural Language Processing – and this book is your answer.<br /><br /><em>Python Text Processing with NLTK 2.0 Cookbook</em> is your handy and illustrative guide, which will walk you through all the Natural Language Processing techniques in a step–by-step manner. It will demystify the advanced features of text analysis and text mining using the comprehensive NLTK suite.<br /><br />This book cuts short the preamble and you dive right into the science of text processing with a practical hands-on approach.<br /><br />Get started off with learning tokenization of text. Get an overview of WordNet and how to use it. Learn the basics as well as advanced features of Stemming and Lemmatization. Discover various ways to replace words with simpler and more common (read: more searched) variants. Create your own corpora and learn to create custom corpus readers for JSON files as well as for data stored in MongoDB. Use and manipulate POS taggers. Transform and normalize parsed chunks to produce a canonical form without changing their meaning. Dig into feature extraction and text classification. Learn how to easily handle huge amounts of data without any loss in efficiency or speed.<br /><br />This book will teach you all that and beyond, in a hands-on learn-by-doing manner. Make yourself an expert in using the NLTK for Natural Language Processing with this handy companion.</p>
Table of Contents (16 chapters)
Python Text Processing with NLTK 2.0 Cookbook
Credits
About the Author
About the Reviewers
Preface
Penn Treebank Part-of-Speech Tags
Index

Combining taggers with backoff tagging


Backoff tagging is one of the core features of SequentialBackoffTagger. It allows you to chain taggers together so that if one tagger doesn't know how to tag a word, it can pass the word on to the next backoff tagger. If that one can't do it, it can pass the word on to the next backoff tagger, and so on until there are no backoff taggers left to check.

How to do it...

Every subclass of SequentialBackoffTagger can take a backoff keyword argument whose value is another instance of a SequentialBackoffTagger. So we will use the DefaultTagger from the Default tagging recipe as the backoff to the UnigramTagger from the Training a unigram part-of-speech tagger recipe. Refer to both recipes for details on train_sents and test_sents.

>>> tagger1 = DefaultTagger('NN')
>>> tagger2 = UnigramTagger(train_sents, backoff=tagger1)
>>> tagger2.evaluate(test_sents)
0.87459529462551266

By using a default tag of NN whenever the UnigramTagger is...