What Is a Conditional Random Field (CRF)? Tag Sequences with Context
How CRFs predict label sequences, when they help beyond token classifiers, and how to evaluate sequence quality.
A conditional random field (CRF) is a discriminative model of p(y|x): it scores complete label sequences given an input sequence. In NLP, a linear-chain CRF can label tokens with parts of speech or entity tags while considering neighboring labels and input features such as capitalization, suffixes, and nearby words. Unlike independent token classification, it compares whole sequences; it does not model the input distribution p(x).
Training requires correctly annotated sequences. With sklearn-crfsuite, represent each sentence as a list of feature dictionaries and its labels as a list of strings, then call CRF.fit(X_train, y_train) and predict(X_test). Add token and neighbor features suited to the language and task. The tutorial’s example settings are not universal optima; this code pattern was not run here.
Split data by sentence or document to prevent leakage. For named entities, evaluate exact entity spans with precision, recall, and F1, not token accuracy alone; compare against a token-classifier baseline and inspect boundary errors. Results depend on annotation consistency, feature design, and representative data. Hand-built features take effort, and a simple chain may miss complex long-distance context. Compare with neural alternatives on the same data.