5.4 Automatic Tagging

In the rest of this chapter we will explore various ways to automatically add part-of
speech tags to text. We will see that the tag of a word depends on the word and its
context within a sentence. For this reason, we will be working with data at the level of
(tagged) sentences rather than words. We’ll begin by loading the data we will be using.

from nltk.corpus import brown
brown_tagged_sents = brown.tagged_sents(categories=’news’)
brown_sents = brown.sents(categories=’news’)

The Default Tagger
The simplest possible tagger assigns the same tag to each token. This may seem to be
a rather banal step, but it establishes an important baseline for tagger performance. In
order to get the best result, we tag each word with the most likely tag. Let’s find out
which tag is most likely (now using the unsimplified tagset):

tags = [tag for (word, tag) in brown.tagged_words(categories=’news’)]
nltk.FreqDist(tags).max()
‘NN’

Now we can create a tagger that tags everything as NN.

raw = ‘I do not like green eggs and ham, I do not like them Sam I am!’
tokens = nltk.word_tokenize(raw)
default_tagger = nltk.DefaultTagger(‘NN’)
default_tagger.tag(tokens)
[(‘I’, ‘NN’), (‘do’, ‘NN’), (‘not’, ‘NN’), (‘like’, ‘NN’), (‘green’, ‘NN’),
(‘eggs’, ‘NN’), (‘and’, ‘NN’), (‘ham’, ‘NN’), (‘,’, ‘NN’), (‘I’, ‘NN’), (‘do’, ‘NN’), (‘not’, ‘NN’), (‘like’, ‘NN’), (‘them’, ‘NN’), (‘Sam’, ‘NN’),
(‘I’, ‘NN’), (‘am’, ‘NN’), (‘!’, ‘NN’)]

Unsurprisingly, this method performs rather poorly. On a typical corpus, it will tag
only about an eighth of the tokens correctly, as we see here:

default_tagger.evaluate(brown_tagged_sents)
0.13089484257215028

Default taggers assign their tag to every single word, even words that have never been
encountered before. As it happens, once we have processed several thousand words of
English text, most new words will be nouns. As we will see, this means that default
taggers can help to improve the robustness of a language processing system. We will
return to them shortly.

Leave a Reply