Sequences/Sequence Labeling: POS and NER

Lesson 4.11,895 words

Sequence Labeling: POS and NER

Sequence labeling assigns one tag to every token in a sentence. This first part sets up the task through its two canonical cases — part-of-speech tagging over the Penn Treebank tagset, and named-entity recognition reframed as token labeling with the BIO scheme — then builds the hidden Markov model, the classic probabilistic tagger.

╌╌╌╌

Many language problems have the same shape: read a sentence left to right and attach a label to each word. Deciding that book is a verb in book that flight but a noun in hand me that book is one such problem; finding that United Airlines names an organization is another. Both are instances of sequence labeling — map an input sequence of tokens to an output sequence of tags of the same length, one tag per token.1

The dependence between labels is the difficulty. If the tags were independent we could classify each token on its own with the logistic regression of an earlier lesson. They are not: a determiner is far more likely to be followed by a noun than by a verb, so a good tagger scores the sequence of tags jointly. This lesson develops two models that do exactly that — the hidden Markov model and the conditional random field — and the single decoding algorithm, Viterbi, that both share.

Parts of speech

Part-of-speech tagging is the process of assigning a part-of-speech label to each word in a sequence.2 Parts of speech (also called POS, word classes, or syntactic categories) tell us something about how a word behaves and about its neighbors: nouns tend to follow determiners and adjectives, verbs take nouns as arguments. Knowing a word's part of speech is a useful early step for parsing, coreference, and named-entity recognition.

Word classes divide into two families. Closed classes have a fixed, small membership that rarely admits new words: prepositions (of, in, by), determiners (a, the, this), pronouns (I, you, she), conjunctions (and, but, that), auxiliaries (be, have, can, must), and particles. These are the function words — short, frequent, and grammatical rather than contentful. Open classes accept new members freely and carry the content: nouns, verbs, adjectives, and adverbs. English coins new nouns and verbs constantly (to google, a selfie); it does not coin new prepositions.

To tag a corpus you need a tagset: an agreed inventory of labels. Tagsets for English run from about 40 to 200 tags; the most widely used is the 45-tag Penn Treebank tagset,3 which has labelled many syntactically annotated corpora. It splits the open classes finely — verbs alone get six tags (VB base, VBD past, VBG gerund, VBN past participle, VBP non-third-singular present, VBZ third-singular present) — and gives short codes to the closed classes.

TagMeaningExampleTagMeaningExample
NNnoun, singularllamaDTdeterminera, the
NNSnoun, pluralllamasINpreposition/subord.of, in, by
NNPproper noun, sing.IBMPRPpersonal pronounI, you, he
JJadjectiveyellowMDmodalcan, should
RBadverbquicklyCCcoordinating conj.and, but
VBverb, baseeatVBZverb, 3sg presenteats
VBDverb, pastateVBPverb, non-3sg pres.eat

POS tagging is a disambiguation task: most word types have a single tag (Janet is always NNP, hesitantly always RB), but the ambiguous types — only 14–15% of the vocabulary — are the common words, so 55–67% of word tokens in running text are ambiguous.4 The word back alone can be JJ (the back seat), NN (in the back), VBP (back the bill), VB (back toward the door), RP (buy back), or RB (back then). The tagger's job is to pick the right one from context.

The most-frequent-class baseline assigns each token the tag it carried most often in the training corpus. That trivial rule already reaches about 92% accuracy on English, and good taggers — HMM, CRF, or neural, it barely matters which — reach about 97%, close to the human ceiling.5 Always compare a classifier against a baseline at least this strong.

Named entities and BIO tagging

A named entity is anything that can be referred to with a proper name: a person, a location, an organization. The task of named-entity recognition (NER) finds spans of text that constitute proper names and labels each with its type — most commonly PER (person), LOC (location), ORG (organization), or GPE (geo-political entity), though the term stretches to cover dates, times, and prices.6 Unlike POS tagging, where every token gets exactly one tag, NER must find spans: United Airlines Holding is one three-word organization, and a sentence may have named-entity spans of any length interleaved with ordinary words that belong to no entity.

Spans do not fit the one-tag-per-token mold directly. The standard fix, BIO tagging, encodes the span structure into per-token labels so NER becomes an ordinary sequence-labeling problem.7 Each token gets one of three kinds of tag: B-type marks the token that begins a span of that type, I-type marks a token inside (continuing) a span, and a single O marks any token outside every span. With entity types the scheme uses tags. The BIO labels capture exactly the same information as bracketed spans, but as a flat tag sequence a tagger can produce token by token.

BIO tagging turns span recognition into per-token labeling. Each word of the sentence "Jane Villanueva of United Airlines Holding discussed the Chicago route" gets one tag: B-TYPE begins a span, I-TYPE continues it, and O sits outside every span.

Two variant schemes trade detail for tag count. IO tagging drops the B tag, using only I-type and O; it is smaller but cannot separate two same-type entities that abut. BIOES adds an E-type end tag and an S-type tag for single-token spans, encoding more structure at the cost of more tags. BIO is the common middle ground.

Because NER is span recognition, its errors and its evaluation differ from POS tagging. The unit of response is the entity, not the word, so a system that labels Jane but misses Jane Villanueva commits two errors at once — a false positive and a false negative. NER is scored by precision, recall, and their harmonic mean, the measure, rather than by plain token accuracy.

For example, suppose a gold document contains entity spans, and a system predicts spans, of which exactly match a gold span (right boundaries, right type). Then precision is (of what it proposed, was right), recall is (of what existed, it found ), and

The harmonic mean sits below the arithmetic mean of , and much closer to the lower of the two scores — a system that is lopsided (high precision, low recall, or the reverse) is punished. Because the span is the unit, labeling Jane when the gold span is Jane Villanueva is a boundary error that costs both a false positive (the wrong span Jane) and a false negative (the missed span Jane Villanueva), which is why NER scores run well below the per-token accuracies of POS tagging.

The hidden Markov model

The hidden Markov model (HMM) is the classic probabilistic sequence tagger, and it introduces machinery — states, transitions, emissions, and Viterbi decoding — that reappears in every model after it.8 It is a generative model: it models how a tagged sentence is produced, then inverts that model to recover the tags.

The HMM builds on the Markov chain, a model of sequences of states that makes one strong assumption: to predict the next state, only the current state matters — the states before it have no further influence. Formally, for a sequence of states ,

A Markov chain assigns probabilities to sequences of observable events. But the events we care about in tagging — the parts of speech — are hidden: we see the words, not their tags, and must infer the tags from the words. A hidden Markov model lets us reason about both a sequence of hidden states (the tags) and a sequence of observations they generate (the words).

An HMM is specified by five components. The states are the tags . The transition probability matrix holds , the probability of tag following tag . The observations are the words , drawn from a vocabulary. The emission probabilities (or observation likelihoods) hold , the probability that tag emits word . Finally an initial distribution gives the probability of each tag starting the sequence.

A first-order HMM tagger separates two kinds of probability. Solid arrows are transitions between hidden tag-states (three shown: NNP, MD, VB); dashed arrows are emissions , each state generating an observed word with some likelihood.

A first-order HMM makes two simplifying assumptions, both visible in the figure. The first is the Markov assumption on transitions: a tag depends only on the previous tag. The second is output independence: a word depends only on its own tag, not on neighboring words or tags,

Both probability tables come from a labelled corpus by maximum-likelihood counting. The transition estimate is one tag-bigram count over one tag-unigram count, and the emission estimate is a tag-word count over the same tag count:

In the WSJ corpus, for instance, MD (modal) occurs 13124 times and is followed by VB 10471 of them, so ; the same MD state emits the word will 4046 times, so . Note the counterintuitive direction of the emission: it is the probability of the word will given that an MD is being generated — the likelihood, not the posterior .

The two tables of an HMM tagger, illustrated for three states. The A transition probabilities connect tag-states; the B observation likelihoods give each state a distribution over words. The full tagger has one state per tag.

HMM tagging as decoding

With and in hand, tagging is decoding: given the observations, find the most probable hidden state sequence.9 For words the goal is the tag sequence maximizing the posterior,

The generative model does not give this posterior directly, so we apply Bayes' rule, drop the constant denominator , and impose the two HMM assumptions. Output independence factors the likelihood into per-word emissions; the bigram assumption factors the prior into per-tag transitions. What remains is a product of exactly the two tables we estimated:

The two factors correspond neatly to the emission and transition probabilities. The only remaining problem is the : the number of tag sequences is , exponential in sentence length, so we cannot enumerate them.

The argmax ranges over tag sequences, so brute force is infeasible — but the score factors over adjacent tags, and that local structure is what a dynamic program exploits. The next lesson builds that decoder, the Viterbi algorithm, works a full numeric trace through it, and then shows how the conditional random field keeps the same decoder while relaxing the HMM's rigid tables.

This continues in Viterbi Decoding, CRFs, and Neural Taggers.

Footnotes

  1. Jurafsky & Martin, Speech and Language Processing (3rd ed.), Ch. 8 — Sequence Labeling for Parts of Speech and Named Entities: the sequence-labeling task maps an input sequence of tokens to an equally long output sequence of labels, one label per token.
  2. Jurafsky & Martin, §8.1–§8.2 — English Word Classes and Part-of-Speech Tagging: parts of speech as open and closed classes, and tagging as assigning a POS label to each word in a sequence.
  3. Jurafsky & Martin, §8.2 — the 45-tag Penn Treebank tagset (Marcus et al., 1993), used to label many syntactically annotated corpora, distinguishing tense and participles on verbs.
  4. Jurafsky & Martin, §8.2 — tag ambiguity in the Brown and WSJ corpora: most word types are unambiguous, but the ambiguous types are common, so a majority of word tokens in running text are ambiguous.
  5. Jurafsky & Martin, §8.2 — the most-frequent-class baseline reaches about 92% accuracy on English, against a 97% state-of-the-art and human ceiling; always compare against at least this baseline.
  6. Jurafsky & Martin, §8.3 — Named Entities and Named Entity Tagging: named entities as things referable by a proper name, the PER/LOC/ORG/GPE types, and evaluation by recall, precision, and .
  7. Jurafsky & Martin, §8.3 — BIO tagging (Ramshaw and Marcus, 1995): B/I/O labels turn span recognition into per-token sequence labeling with tags, with IO and BIOES as variants.
  8. Jurafsky & Martin, §8.4 — HMM Part-of-Speech Tagging: Markov chains and the Markov assumption, the hidden Markov model's five components, and its transition and emission probabilities estimated by maximum-likelihood counting.
  9. Jurafsky & Martin, §8.4.4 — HMM tagging as decoding: Bayes' rule plus the output-independence and bigram assumptions reduce the argmax to a product of emission and transition probabilities.

╌╌ END ╌╌