nltk.metrics.agreement module

Implementations of inter-annotator agreement coefficients surveyed by Artstein and Poesio (2007), Inter-Coder Agreement for Computational Linguistics.

An agreement coefficient calculates the amount that annotators agreed on label assignments beyond what is expected by chance.

In defining the AnnotationTask class, we use naming conventions similar to the paper’s terminology. There are three types of objects in an annotation task:

the coders (variables “c” and “C”) the items to be annotated (variables “i” and “I”) the potential categories to be assigned (variables “k” and “K”)

Additionally, it is often the case that we don’t want to treat two different labels as complete disagreement, and so the AnnotationTask constructor can also take a distance metric as a final argument. Distance metrics are simply functions that take two arguments, and return a value between 0.0 and 1.0 indicating the distance between them. If not supplied, the default is binary comparison between the arguments.

The simplest way to initialize an AnnotationTask is with a list of triples, each containing a coder’s assignment for one object in the task:

task = AnnotationTask(data=[(‘c1’, ‘1’, ‘v1’),(‘c2’, ‘1’, ‘v1’),…])

Note that the data list needs to contain the same number of triples for each individual coder, containing category values for the same set of items.

Alpha (Krippendorff 1980) Kappa (Cohen 1960) S (Bennet, Albert and Goldstein 1954) Pi (Scott 1955)

TODO: Describe handling of multiple coders and missing data

Expected results from the Artstein and Poesio survey paper:

>>> from nltk.metrics.agreement import AnnotationTask
>>> import os.path
>>> t = AnnotationTask(data=[x.split() for x in open(os.path.join(os.path.dirname(__file__), "artstein_poesio_example.txt"))])
>>> t.avg_Ao()
0.88
>>> round(t.pi(), 5)
0.79953
>>> round(t.S(), 2)
0.82

This would have returned a wrong value (0.0) in @785fb79 as coders are in the wrong order. Subsequently, all values for pi(), S(), and kappa() would have been wrong as they are computed with avg_Ao(). >>> t2 = AnnotationTask(data=[(‘b’,’1’,’stat’),(‘a’,’1’,’stat’)]) >>> t2.avg_Ao() 1.0

The following, of course, also works. >>> t3 = AnnotationTask(data=[(‘a’,’1’,’othr’),(‘b’,’1’,’othr’)]) >>> t3.avg_Ao() 1.0

class nltk.metrics.agreement.AnnotationTask[source]

Bases: object

Represents an annotation task, i.e. people assign labels to items.

Notation tries to match notation in Artstein and Poesio (2007).

In general, coders and items can be represented as any hashable object. Integers, for example, are fine, though strings are more readable. Labels must support the distance functions applied to them, so e.g. a string-edit-distance makes no sense if your labels are integers, whereas interval distance needs numeric values. A notable case of this is the MASI metric, which requires Python sets.

Ae_kappa(cA, cB)[source]
Ao(cA, cB)[source]

Observed agreement between two coders on all items.

Disagreement(label_freqs)[source]
Do_Kw(max_distance=1.0)[source]

Averaged over all labelers

Do_Kw_pairwise(cA, cB, max_distance=1.0)[source]

The observed disagreement for the weighted kappa coefficient.

N(**kwargs)

Implements the “n-notation” used in Artstein and Poesio (2007)

@deprecated: Use Nk, Nik or Nck instead

Nck(c, k)[source]
Nik(i, k)[source]
Nk(k)[source]
S()[source]

Bennett, Albert and Goldstein 1954

__init__(data=None, distance=<function binary_distance>)[source]

Initialize an annotation task.

The data argument can be None (to create an empty annotation task) or a sequence of 3-tuples, each representing a coder’s labeling of an item: (coder,item,label)

The distance argument is a function taking two arguments (labels) and producing a numerical distance. The distance from a label to itself should be zero: distance(l,l) = 0

agr(cA, cB, i, data=None)[source]

Agreement between two coders on a given item

alpha()[source]

Krippendorff 1980

avg_Ao()[source]

Average observed agreement across all coders and items.

kappa()[source]

Cohen 1960 Averages naively over kappas for each coder pair.

kappa_pairwise(cA, cB)[source]
load_array(array)[source]

Load an sequence of annotation results, appending to any data already loaded.

The argument is a sequence of 3-tuples, each representing a coder’s labeling of an item:

(coder,item,label)

multi_kappa()[source]

Davies and Fleiss 1982 Averages over observed and expected agreements for each coder pair.

pi()[source]

Scott 1955; here, multi-pi. Equivalent to K from Siegel and Castellan (1988).

weighted_kappa(max_distance=1.0)[source]

Cohen 1968

weighted_kappa_pairwise(cA, cB, max_distance=1.0)[source]

Cohen 1968