Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step40/zero/18.attention.dense.weight/fp32.pt +3 -0
- ckpts/universal/global_step40/zero/4.post_attention_layernorm.weight/exp_avg.pt +3 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/book.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/cli.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/collections.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/collocations.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/data.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/downloader.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/featstruct.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/grammar.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/help.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/internals.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/jsontags.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/lazyimport.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/probability.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/text.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/toolbox.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/treeprettyprinter.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/util.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/__pycache__/wsd.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/api.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/decisiontree.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/maxent.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/megam.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/naivebayes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/positivenaivebayes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/rte_classify.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/scikitlearn.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/senna.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/svm.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/tadm.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/textcat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/util.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/classify/__pycache__/weka.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__init__.py +51 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/agreement.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/aline.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/association.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/confusionmatrix.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/distance.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/paice.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/scores.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/segmentation.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/spearman.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/nltk/metrics/agreement.py +465 -0
- venv/lib/python3.10/site-packages/nltk/metrics/aline.py +1354 -0
- venv/lib/python3.10/site-packages/nltk/metrics/association.py +476 -0
ckpts/universal/global_step40/zero/18.attention.dense.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:93482633809008696ac13147754df15af63147730ef48b2e800c1fefc6d00f08
|
3 |
+
size 16778317
|
ckpts/universal/global_step40/zero/4.post_attention_layernorm.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ef05a5b41568f454cb8d51db527fa4cf539c17f2f8392ece5400565f3592d63e
|
3 |
+
size 9372
|
venv/lib/python3.10/site-packages/nltk/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (4.85 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/book.cpython-310.pyc
ADDED
Binary file (3 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/cli.cpython-310.pyc
ADDED
Binary file (1.67 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/collections.cpython-310.pyc
ADDED
Binary file (23.3 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/collocations.cpython-310.pyc
ADDED
Binary file (15 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/data.cpython-310.pyc
ADDED
Binary file (38.7 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/downloader.cpython-310.pyc
ADDED
Binary file (61.4 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/featstruct.cpython-310.pyc
ADDED
Binary file (73.4 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/grammar.cpython-310.pyc
ADDED
Binary file (53.3 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/help.cpython-310.pyc
ADDED
Binary file (1.61 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/internals.cpython-310.pyc
ADDED
Binary file (28.8 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/jsontags.cpython-310.pyc
ADDED
Binary file (2.33 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/lazyimport.cpython-310.pyc
ADDED
Binary file (3.74 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/probability.cpython-310.pyc
ADDED
Binary file (86.2 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/text.cpython-310.pyc
ADDED
Binary file (28.4 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/toolbox.cpython-310.pyc
ADDED
Binary file (15.9 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/treeprettyprinter.cpython-310.pyc
ADDED
Binary file (961 Bytes). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/util.cpython-310.pyc
ADDED
Binary file (32.6 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/__pycache__/wsd.cpython-310.pyc
ADDED
Binary file (1.84 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (4.69 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/api.cpython-310.pyc
ADDED
Binary file (4.94 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/decisiontree.cpython-310.pyc
ADDED
Binary file (9.59 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/maxent.cpython-310.pyc
ADDED
Binary file (46 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/megam.cpython-310.pyc
ADDED
Binary file (5.21 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/naivebayes.cpython-310.pyc
ADDED
Binary file (8 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/positivenaivebayes.cpython-310.pyc
ADDED
Binary file (5.31 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/rte_classify.cpython-310.pyc
ADDED
Binary file (5.55 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/scikitlearn.cpython-310.pyc
ADDED
Binary file (5.94 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/senna.cpython-310.pyc
ADDED
Binary file (5.63 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/svm.cpython-310.pyc
ADDED
Binary file (694 Bytes). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/tadm.cpython-310.pyc
ADDED
Binary file (3.33 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/textcat.cpython-310.pyc
ADDED
Binary file (4.71 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/util.cpython-310.pyc
ADDED
Binary file (10.5 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/classify/__pycache__/weka.cpython-310.pyc
ADDED
Binary file (10.2 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__init__.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Natural Language Toolkit: Metrics
|
2 |
+
#
|
3 |
+
# Copyright (C) 2001-2023 NLTK Project
|
4 |
+
# Author: Steven Bird <[email protected]>
|
5 |
+
# Edward Loper <[email protected]>
|
6 |
+
# URL: <https://www.nltk.org/>
|
7 |
+
# For license information, see LICENSE.TXT
|
8 |
+
#
|
9 |
+
|
10 |
+
"""
|
11 |
+
NLTK Metrics
|
12 |
+
|
13 |
+
Classes and methods for scoring processing modules.
|
14 |
+
"""
|
15 |
+
|
16 |
+
from nltk.metrics.agreement import AnnotationTask
|
17 |
+
from nltk.metrics.aline import align
|
18 |
+
from nltk.metrics.association import (
|
19 |
+
BigramAssocMeasures,
|
20 |
+
ContingencyMeasures,
|
21 |
+
NgramAssocMeasures,
|
22 |
+
QuadgramAssocMeasures,
|
23 |
+
TrigramAssocMeasures,
|
24 |
+
)
|
25 |
+
from nltk.metrics.confusionmatrix import ConfusionMatrix
|
26 |
+
from nltk.metrics.distance import (
|
27 |
+
binary_distance,
|
28 |
+
custom_distance,
|
29 |
+
edit_distance,
|
30 |
+
edit_distance_align,
|
31 |
+
fractional_presence,
|
32 |
+
interval_distance,
|
33 |
+
jaccard_distance,
|
34 |
+
masi_distance,
|
35 |
+
presence,
|
36 |
+
)
|
37 |
+
from nltk.metrics.paice import Paice
|
38 |
+
from nltk.metrics.scores import (
|
39 |
+
accuracy,
|
40 |
+
approxrand,
|
41 |
+
f_measure,
|
42 |
+
log_likelihood,
|
43 |
+
precision,
|
44 |
+
recall,
|
45 |
+
)
|
46 |
+
from nltk.metrics.segmentation import ghd, pk, windowdiff
|
47 |
+
from nltk.metrics.spearman import (
|
48 |
+
ranks_from_scores,
|
49 |
+
ranks_from_sequence,
|
50 |
+
spearman_correlation,
|
51 |
+
)
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.31 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/agreement.cpython-310.pyc
ADDED
Binary file (16.4 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/aline.cpython-310.pyc
ADDED
Binary file (13.6 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/association.cpython-310.pyc
ADDED
Binary file (15.7 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/confusionmatrix.cpython-310.pyc
ADDED
Binary file (12 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/distance.cpython-310.pyc
ADDED
Binary file (14.7 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/paice.cpython-310.pyc
ADDED
Binary file (11.4 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/scores.cpython-310.pyc
ADDED
Binary file (7.59 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/segmentation.cpython-310.pyc
ADDED
Binary file (6.5 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/__pycache__/spearman.cpython-310.pyc
ADDED
Binary file (2.28 kB). View file
|
|
venv/lib/python3.10/site-packages/nltk/metrics/agreement.py
ADDED
@@ -0,0 +1,465 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Natural Language Toolkit: Agreement Metrics
|
2 |
+
#
|
3 |
+
# Copyright (C) 2001-2023 NLTK Project
|
4 |
+
# Author: Tom Lippincott <[email protected]>
|
5 |
+
# URL: <https://www.nltk.org/>
|
6 |
+
# For license information, see LICENSE.TXT
|
7 |
+
#
|
8 |
+
|
9 |
+
"""
|
10 |
+
Implementations of inter-annotator agreement coefficients surveyed by Artstein
|
11 |
+
and Poesio (2007), Inter-Coder Agreement for Computational Linguistics.
|
12 |
+
|
13 |
+
An agreement coefficient calculates the amount that annotators agreed on label
|
14 |
+
assignments beyond what is expected by chance.
|
15 |
+
|
16 |
+
In defining the AnnotationTask class, we use naming conventions similar to the
|
17 |
+
paper's terminology. There are three types of objects in an annotation task:
|
18 |
+
|
19 |
+
the coders (variables "c" and "C")
|
20 |
+
the items to be annotated (variables "i" and "I")
|
21 |
+
the potential categories to be assigned (variables "k" and "K")
|
22 |
+
|
23 |
+
Additionally, it is often the case that we don't want to treat two different
|
24 |
+
labels as complete disagreement, and so the AnnotationTask constructor can also
|
25 |
+
take a distance metric as a final argument. Distance metrics are simply
|
26 |
+
functions that take two arguments, and return a value between 0.0 and 1.0
|
27 |
+
indicating the distance between them. If not supplied, the default is binary
|
28 |
+
comparison between the arguments.
|
29 |
+
|
30 |
+
The simplest way to initialize an AnnotationTask is with a list of triples,
|
31 |
+
each containing a coder's assignment for one object in the task:
|
32 |
+
|
33 |
+
task = AnnotationTask(data=[('c1', '1', 'v1'),('c2', '1', 'v1'),...])
|
34 |
+
|
35 |
+
Note that the data list needs to contain the same number of triples for each
|
36 |
+
individual coder, containing category values for the same set of items.
|
37 |
+
|
38 |
+
Alpha (Krippendorff 1980)
|
39 |
+
Kappa (Cohen 1960)
|
40 |
+
S (Bennet, Albert and Goldstein 1954)
|
41 |
+
Pi (Scott 1955)
|
42 |
+
|
43 |
+
|
44 |
+
TODO: Describe handling of multiple coders and missing data
|
45 |
+
|
46 |
+
Expected results from the Artstein and Poesio survey paper:
|
47 |
+
|
48 |
+
>>> from nltk.metrics.agreement import AnnotationTask
|
49 |
+
>>> import os.path
|
50 |
+
>>> t = AnnotationTask(data=[x.split() for x in open(os.path.join(os.path.dirname(__file__), "artstein_poesio_example.txt"))])
|
51 |
+
>>> t.avg_Ao()
|
52 |
+
0.88
|
53 |
+
>>> round(t.pi(), 5)
|
54 |
+
0.79953
|
55 |
+
>>> round(t.S(), 2)
|
56 |
+
0.82
|
57 |
+
|
58 |
+
This would have returned a wrong value (0.0) in @785fb79 as coders are in
|
59 |
+
the wrong order. Subsequently, all values for pi(), S(), and kappa() would
|
60 |
+
have been wrong as they are computed with avg_Ao().
|
61 |
+
>>> t2 = AnnotationTask(data=[('b','1','stat'),('a','1','stat')])
|
62 |
+
>>> t2.avg_Ao()
|
63 |
+
1.0
|
64 |
+
|
65 |
+
The following, of course, also works.
|
66 |
+
>>> t3 = AnnotationTask(data=[('a','1','othr'),('b','1','othr')])
|
67 |
+
>>> t3.avg_Ao()
|
68 |
+
1.0
|
69 |
+
|
70 |
+
"""
|
71 |
+
|
72 |
+
import logging
|
73 |
+
from itertools import groupby
|
74 |
+
from operator import itemgetter
|
75 |
+
|
76 |
+
from nltk.internals import deprecated
|
77 |
+
from nltk.metrics.distance import binary_distance
|
78 |
+
from nltk.probability import ConditionalFreqDist, FreqDist
|
79 |
+
|
80 |
+
log = logging.getLogger(__name__)
|
81 |
+
|
82 |
+
|
83 |
+
class AnnotationTask:
|
84 |
+
"""Represents an annotation task, i.e. people assign labels to items.
|
85 |
+
|
86 |
+
Notation tries to match notation in Artstein and Poesio (2007).
|
87 |
+
|
88 |
+
In general, coders and items can be represented as any hashable object.
|
89 |
+
Integers, for example, are fine, though strings are more readable.
|
90 |
+
Labels must support the distance functions applied to them, so e.g.
|
91 |
+
a string-edit-distance makes no sense if your labels are integers,
|
92 |
+
whereas interval distance needs numeric values. A notable case of this
|
93 |
+
is the MASI metric, which requires Python sets.
|
94 |
+
"""
|
95 |
+
|
96 |
+
def __init__(self, data=None, distance=binary_distance):
|
97 |
+
"""Initialize an annotation task.
|
98 |
+
|
99 |
+
The data argument can be None (to create an empty annotation task) or a sequence of 3-tuples,
|
100 |
+
each representing a coder's labeling of an item:
|
101 |
+
``(coder,item,label)``
|
102 |
+
|
103 |
+
The distance argument is a function taking two arguments (labels) and producing a numerical distance.
|
104 |
+
The distance from a label to itself should be zero:
|
105 |
+
``distance(l,l) = 0``
|
106 |
+
"""
|
107 |
+
self.distance = distance
|
108 |
+
self.I = set()
|
109 |
+
self.K = set()
|
110 |
+
self.C = set()
|
111 |
+
self.data = []
|
112 |
+
if data is not None:
|
113 |
+
self.load_array(data)
|
114 |
+
|
115 |
+
def __str__(self):
|
116 |
+
return "\r\n".join(
|
117 |
+
map(
|
118 |
+
lambda x: "%s\t%s\t%s"
|
119 |
+
% (x["coder"], x["item"].replace("_", "\t"), ",".join(x["labels"])),
|
120 |
+
self.data,
|
121 |
+
)
|
122 |
+
)
|
123 |
+
|
124 |
+
def load_array(self, array):
|
125 |
+
"""Load an sequence of annotation results, appending to any data already loaded.
|
126 |
+
|
127 |
+
The argument is a sequence of 3-tuples, each representing a coder's labeling of an item:
|
128 |
+
(coder,item,label)
|
129 |
+
"""
|
130 |
+
for coder, item, labels in array:
|
131 |
+
self.C.add(coder)
|
132 |
+
self.K.add(labels)
|
133 |
+
self.I.add(item)
|
134 |
+
self.data.append({"coder": coder, "labels": labels, "item": item})
|
135 |
+
|
136 |
+
def agr(self, cA, cB, i, data=None):
|
137 |
+
"""Agreement between two coders on a given item"""
|
138 |
+
data = data or self.data
|
139 |
+
# cfedermann: we don't know what combination of coder/item will come
|
140 |
+
# first in x; to avoid StopIteration problems due to assuming an order
|
141 |
+
# cA,cB, we allow either for k1 and then look up the missing as k2.
|
142 |
+
k1 = next(x for x in data if x["coder"] in (cA, cB) and x["item"] == i)
|
143 |
+
if k1["coder"] == cA:
|
144 |
+
k2 = next(x for x in data if x["coder"] == cB and x["item"] == i)
|
145 |
+
else:
|
146 |
+
k2 = next(x for x in data if x["coder"] == cA and x["item"] == i)
|
147 |
+
|
148 |
+
ret = 1.0 - float(self.distance(k1["labels"], k2["labels"]))
|
149 |
+
log.debug("Observed agreement between %s and %s on %s: %f", cA, cB, i, ret)
|
150 |
+
log.debug(
|
151 |
+
'Distance between "%r" and "%r": %f', k1["labels"], k2["labels"], 1.0 - ret
|
152 |
+
)
|
153 |
+
return ret
|
154 |
+
|
155 |
+
def Nk(self, k):
|
156 |
+
return float(sum(1 for x in self.data if x["labels"] == k))
|
157 |
+
|
158 |
+
def Nik(self, i, k):
|
159 |
+
return float(sum(1 for x in self.data if x["item"] == i and x["labels"] == k))
|
160 |
+
|
161 |
+
def Nck(self, c, k):
|
162 |
+
return float(sum(1 for x in self.data if x["coder"] == c and x["labels"] == k))
|
163 |
+
|
164 |
+
@deprecated("Use Nk, Nik or Nck instead")
|
165 |
+
def N(self, k=None, i=None, c=None):
|
166 |
+
"""Implements the "n-notation" used in Artstein and Poesio (2007)"""
|
167 |
+
if k is not None and i is None and c is None:
|
168 |
+
ret = self.Nk(k)
|
169 |
+
elif k is not None and i is not None and c is None:
|
170 |
+
ret = self.Nik(i, k)
|
171 |
+
elif k is not None and c is not None and i is None:
|
172 |
+
ret = self.Nck(c, k)
|
173 |
+
else:
|
174 |
+
raise ValueError(
|
175 |
+
f"You must pass either i or c, not both! (k={k!r},i={i!r},c={c!r})"
|
176 |
+
)
|
177 |
+
log.debug("Count on N[%s,%s,%s]: %d", k, i, c, ret)
|
178 |
+
return ret
|
179 |
+
|
180 |
+
def _grouped_data(self, field, data=None):
|
181 |
+
data = data or self.data
|
182 |
+
return groupby(sorted(data, key=itemgetter(field)), itemgetter(field))
|
183 |
+
|
184 |
+
def Ao(self, cA, cB):
|
185 |
+
"""Observed agreement between two coders on all items."""
|
186 |
+
data = self._grouped_data(
|
187 |
+
"item", (x for x in self.data if x["coder"] in (cA, cB))
|
188 |
+
)
|
189 |
+
ret = sum(self.agr(cA, cB, item, item_data) for item, item_data in data) / len(
|
190 |
+
self.I
|
191 |
+
)
|
192 |
+
log.debug("Observed agreement between %s and %s: %f", cA, cB, ret)
|
193 |
+
return ret
|
194 |
+
|
195 |
+
def _pairwise_average(self, function):
|
196 |
+
"""
|
197 |
+
Calculates the average of function results for each coder pair
|
198 |
+
"""
|
199 |
+
total = 0
|
200 |
+
n = 0
|
201 |
+
s = self.C.copy()
|
202 |
+
for cA in self.C:
|
203 |
+
s.remove(cA)
|
204 |
+
for cB in s:
|
205 |
+
total += function(cA, cB)
|
206 |
+
n += 1
|
207 |
+
ret = total / n
|
208 |
+
return ret
|
209 |
+
|
210 |
+
def avg_Ao(self):
|
211 |
+
"""Average observed agreement across all coders and items."""
|
212 |
+
ret = self._pairwise_average(self.Ao)
|
213 |
+
log.debug("Average observed agreement: %f", ret)
|
214 |
+
return ret
|
215 |
+
|
216 |
+
def Do_Kw_pairwise(self, cA, cB, max_distance=1.0):
|
217 |
+
"""The observed disagreement for the weighted kappa coefficient."""
|
218 |
+
total = 0.0
|
219 |
+
data = (x for x in self.data if x["coder"] in (cA, cB))
|
220 |
+
for i, itemdata in self._grouped_data("item", data):
|
221 |
+
# we should have two items; distance doesn't care which comes first
|
222 |
+
total += self.distance(next(itemdata)["labels"], next(itemdata)["labels"])
|
223 |
+
|
224 |
+
ret = total / (len(self.I) * max_distance)
|
225 |
+
log.debug("Observed disagreement between %s and %s: %f", cA, cB, ret)
|
226 |
+
return ret
|
227 |
+
|
228 |
+
def Do_Kw(self, max_distance=1.0):
|
229 |
+
"""Averaged over all labelers"""
|
230 |
+
ret = self._pairwise_average(
|
231 |
+
lambda cA, cB: self.Do_Kw_pairwise(cA, cB, max_distance)
|
232 |
+
)
|
233 |
+
log.debug("Observed disagreement: %f", ret)
|
234 |
+
return ret
|
235 |
+
|
236 |
+
# Agreement Coefficients
|
237 |
+
def S(self):
|
238 |
+
"""Bennett, Albert and Goldstein 1954"""
|
239 |
+
Ae = 1.0 / len(self.K)
|
240 |
+
ret = (self.avg_Ao() - Ae) / (1.0 - Ae)
|
241 |
+
return ret
|
242 |
+
|
243 |
+
def pi(self):
|
244 |
+
"""Scott 1955; here, multi-pi.
|
245 |
+
Equivalent to K from Siegel and Castellan (1988).
|
246 |
+
|
247 |
+
"""
|
248 |
+
total = 0.0
|
249 |
+
label_freqs = FreqDist(x["labels"] for x in self.data)
|
250 |
+
for k, f in label_freqs.items():
|
251 |
+
total += f**2
|
252 |
+
Ae = total / ((len(self.I) * len(self.C)) ** 2)
|
253 |
+
return (self.avg_Ao() - Ae) / (1 - Ae)
|
254 |
+
|
255 |
+
def Ae_kappa(self, cA, cB):
|
256 |
+
Ae = 0.0
|
257 |
+
nitems = float(len(self.I))
|
258 |
+
label_freqs = ConditionalFreqDist((x["labels"], x["coder"]) for x in self.data)
|
259 |
+
for k in label_freqs.conditions():
|
260 |
+
Ae += (label_freqs[k][cA] / nitems) * (label_freqs[k][cB] / nitems)
|
261 |
+
return Ae
|
262 |
+
|
263 |
+
def kappa_pairwise(self, cA, cB):
|
264 |
+
""" """
|
265 |
+
Ae = self.Ae_kappa(cA, cB)
|
266 |
+
ret = (self.Ao(cA, cB) - Ae) / (1.0 - Ae)
|
267 |
+
log.debug("Expected agreement between %s and %s: %f", cA, cB, Ae)
|
268 |
+
return ret
|
269 |
+
|
270 |
+
def kappa(self):
|
271 |
+
"""Cohen 1960
|
272 |
+
Averages naively over kappas for each coder pair.
|
273 |
+
|
274 |
+
"""
|
275 |
+
return self._pairwise_average(self.kappa_pairwise)
|
276 |
+
|
277 |
+
def multi_kappa(self):
|
278 |
+
"""Davies and Fleiss 1982
|
279 |
+
Averages over observed and expected agreements for each coder pair.
|
280 |
+
|
281 |
+
"""
|
282 |
+
Ae = self._pairwise_average(self.Ae_kappa)
|
283 |
+
return (self.avg_Ao() - Ae) / (1.0 - Ae)
|
284 |
+
|
285 |
+
def Disagreement(self, label_freqs):
|
286 |
+
total_labels = sum(label_freqs.values())
|
287 |
+
pairs = 0.0
|
288 |
+
for j, nj in label_freqs.items():
|
289 |
+
for l, nl in label_freqs.items():
|
290 |
+
pairs += float(nj * nl) * self.distance(l, j)
|
291 |
+
return 1.0 * pairs / (total_labels * (total_labels - 1))
|
292 |
+
|
293 |
+
def alpha(self):
|
294 |
+
"""Krippendorff 1980"""
|
295 |
+
# check for degenerate cases
|
296 |
+
if len(self.K) == 0:
|
297 |
+
raise ValueError("Cannot calculate alpha, no data present!")
|
298 |
+
if len(self.K) == 1:
|
299 |
+
log.debug("Only one annotation value, alpha returning 1.")
|
300 |
+
return 1
|
301 |
+
if len(self.C) == 1 and len(self.I) == 1:
|
302 |
+
raise ValueError("Cannot calculate alpha, only one coder and item present!")
|
303 |
+
|
304 |
+
total_disagreement = 0.0
|
305 |
+
total_ratings = 0
|
306 |
+
all_valid_labels_freq = FreqDist([])
|
307 |
+
|
308 |
+
total_do = 0.0 # Total observed disagreement for all items.
|
309 |
+
for i, itemdata in self._grouped_data("item"):
|
310 |
+
label_freqs = FreqDist(x["labels"] for x in itemdata)
|
311 |
+
labels_count = sum(label_freqs.values())
|
312 |
+
if labels_count < 2:
|
313 |
+
# Ignore the item.
|
314 |
+
continue
|
315 |
+
all_valid_labels_freq += label_freqs
|
316 |
+
total_do += self.Disagreement(label_freqs) * labels_count
|
317 |
+
|
318 |
+
do = total_do / sum(all_valid_labels_freq.values())
|
319 |
+
|
320 |
+
de = self.Disagreement(all_valid_labels_freq) # Expected disagreement.
|
321 |
+
k_alpha = 1.0 - do / de
|
322 |
+
|
323 |
+
return k_alpha
|
324 |
+
|
325 |
+
def weighted_kappa_pairwise(self, cA, cB, max_distance=1.0):
|
326 |
+
"""Cohen 1968"""
|
327 |
+
total = 0.0
|
328 |
+
label_freqs = ConditionalFreqDist(
|
329 |
+
(x["coder"], x["labels"]) for x in self.data if x["coder"] in (cA, cB)
|
330 |
+
)
|
331 |
+
for j in self.K:
|
332 |
+
for l in self.K:
|
333 |
+
total += label_freqs[cA][j] * label_freqs[cB][l] * self.distance(j, l)
|
334 |
+
De = total / (max_distance * pow(len(self.I), 2))
|
335 |
+
log.debug("Expected disagreement between %s and %s: %f", cA, cB, De)
|
336 |
+
Do = self.Do_Kw_pairwise(cA, cB)
|
337 |
+
ret = 1.0 - (Do / De)
|
338 |
+
return ret
|
339 |
+
|
340 |
+
def weighted_kappa(self, max_distance=1.0):
|
341 |
+
"""Cohen 1968"""
|
342 |
+
return self._pairwise_average(
|
343 |
+
lambda cA, cB: self.weighted_kappa_pairwise(cA, cB, max_distance)
|
344 |
+
)
|
345 |
+
|
346 |
+
|
347 |
+
if __name__ == "__main__":
|
348 |
+
|
349 |
+
import optparse
|
350 |
+
import re
|
351 |
+
|
352 |
+
from nltk.metrics import distance
|
353 |
+
|
354 |
+
# process command-line arguments
|
355 |
+
parser = optparse.OptionParser()
|
356 |
+
parser.add_option(
|
357 |
+
"-d",
|
358 |
+
"--distance",
|
359 |
+
dest="distance",
|
360 |
+
default="binary_distance",
|
361 |
+
help="distance metric to use",
|
362 |
+
)
|
363 |
+
parser.add_option(
|
364 |
+
"-a",
|
365 |
+
"--agreement",
|
366 |
+
dest="agreement",
|
367 |
+
default="kappa",
|
368 |
+
help="agreement coefficient to calculate",
|
369 |
+
)
|
370 |
+
parser.add_option(
|
371 |
+
"-e",
|
372 |
+
"--exclude",
|
373 |
+
dest="exclude",
|
374 |
+
action="append",
|
375 |
+
default=[],
|
376 |
+
help="coder names to exclude (may be specified multiple times)",
|
377 |
+
)
|
378 |
+
parser.add_option(
|
379 |
+
"-i",
|
380 |
+
"--include",
|
381 |
+
dest="include",
|
382 |
+
action="append",
|
383 |
+
default=[],
|
384 |
+
help="coder names to include, same format as exclude",
|
385 |
+
)
|
386 |
+
parser.add_option(
|
387 |
+
"-f",
|
388 |
+
"--file",
|
389 |
+
dest="file",
|
390 |
+
help="file to read labelings from, each line with three columns: 'labeler item labels'",
|
391 |
+
)
|
392 |
+
parser.add_option(
|
393 |
+
"-v",
|
394 |
+
"--verbose",
|
395 |
+
dest="verbose",
|
396 |
+
default="0",
|
397 |
+
help="how much debugging to print on stderr (0-4)",
|
398 |
+
)
|
399 |
+
parser.add_option(
|
400 |
+
"-c",
|
401 |
+
"--columnsep",
|
402 |
+
dest="columnsep",
|
403 |
+
default="\t",
|
404 |
+
help="char/string that separates the three columns in the file, defaults to tab",
|
405 |
+
)
|
406 |
+
parser.add_option(
|
407 |
+
"-l",
|
408 |
+
"--labelsep",
|
409 |
+
dest="labelsep",
|
410 |
+
default=",",
|
411 |
+
help="char/string that separates labels (if labelers can assign more than one), defaults to comma",
|
412 |
+
)
|
413 |
+
parser.add_option(
|
414 |
+
"-p",
|
415 |
+
"--presence",
|
416 |
+
dest="presence",
|
417 |
+
default=None,
|
418 |
+
help="convert each labeling into 1 or 0, based on presence of LABEL",
|
419 |
+
)
|
420 |
+
parser.add_option(
|
421 |
+
"-T",
|
422 |
+
"--thorough",
|
423 |
+
dest="thorough",
|
424 |
+
default=False,
|
425 |
+
action="store_true",
|
426 |
+
help="calculate agreement for every subset of the annotators",
|
427 |
+
)
|
428 |
+
(options, remainder) = parser.parse_args()
|
429 |
+
|
430 |
+
if not options.file:
|
431 |
+
parser.print_help()
|
432 |
+
exit()
|
433 |
+
|
434 |
+
logging.basicConfig(level=50 - 10 * int(options.verbose))
|
435 |
+
|
436 |
+
# read in data from the specified file
|
437 |
+
data = []
|
438 |
+
with open(options.file) as infile:
|
439 |
+
for l in infile:
|
440 |
+
toks = l.split(options.columnsep)
|
441 |
+
coder, object_, labels = (
|
442 |
+
toks[0],
|
443 |
+
str(toks[1:-1]),
|
444 |
+
frozenset(toks[-1].strip().split(options.labelsep)),
|
445 |
+
)
|
446 |
+
if (
|
447 |
+
(options.include == options.exclude)
|
448 |
+
or (len(options.include) > 0 and coder in options.include)
|
449 |
+
or (len(options.exclude) > 0 and coder not in options.exclude)
|
450 |
+
):
|
451 |
+
data.append((coder, object_, labels))
|
452 |
+
|
453 |
+
if options.presence:
|
454 |
+
task = AnnotationTask(
|
455 |
+
data, getattr(distance, options.distance)(options.presence)
|
456 |
+
)
|
457 |
+
else:
|
458 |
+
task = AnnotationTask(data, getattr(distance, options.distance))
|
459 |
+
|
460 |
+
if options.thorough:
|
461 |
+
pass
|
462 |
+
else:
|
463 |
+
print(getattr(task, options.agreement)())
|
464 |
+
|
465 |
+
logging.shutdown()
|
venv/lib/python3.10/site-packages/nltk/metrics/aline.py
ADDED
@@ -0,0 +1,1354 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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1 |
+
# Natural Language Toolkit: ALINE
|
2 |
+
#
|
3 |
+
# Copyright (C) 2001-2023 NLTK Project
|
4 |
+
# Author: Greg Kondrak <[email protected]>
|
5 |
+
# Geoff Bacon <[email protected]> (Python port)
|
6 |
+
# URL: <https://www.nltk.org/>
|
7 |
+
# For license information, see LICENSE.TXT
|
8 |
+
|
9 |
+
"""
|
10 |
+
ALINE
|
11 |
+
https://webdocs.cs.ualberta.ca/~kondrak/
|
12 |
+
Copyright 2002 by Grzegorz Kondrak.
|
13 |
+
|
14 |
+
ALINE is an algorithm for aligning phonetic sequences, described in [1].
|
15 |
+
This module is a port of Kondrak's (2002) ALINE. It provides functions for
|
16 |
+
phonetic sequence alignment and similarity analysis. These are useful in
|
17 |
+
historical linguistics, sociolinguistics and synchronic phonology.
|
18 |
+
|
19 |
+
ALINE has parameters that can be tuned for desired output. These parameters are:
|
20 |
+
- C_skip, C_sub, C_exp, C_vwl
|
21 |
+
- Salience weights
|
22 |
+
- Segmental features
|
23 |
+
|
24 |
+
In this implementation, some parameters have been changed from their default
|
25 |
+
values as described in [1], in order to replicate published results. All changes
|
26 |
+
are noted in comments.
|
27 |
+
|
28 |
+
Example usage
|
29 |
+
-------------
|
30 |
+
|
31 |
+
# Get optimal alignment of two phonetic sequences
|
32 |
+
|
33 |
+
>>> align('θin', 'tenwis') # doctest: +SKIP
|
34 |
+
[[('θ', 't'), ('i', 'e'), ('n', 'n'), ('-', 'w'), ('-', 'i'), ('-', 's')]]
|
35 |
+
|
36 |
+
[1] G. Kondrak. Algorithms for Language Reconstruction. PhD dissertation,
|
37 |
+
University of Toronto.
|
38 |
+
"""
|
39 |
+
|
40 |
+
try:
|
41 |
+
import numpy as np
|
42 |
+
except ImportError:
|
43 |
+
np = None
|
44 |
+
|
45 |
+
# === Constants ===
|
46 |
+
|
47 |
+
inf = float("inf")
|
48 |
+
|
49 |
+
# Default values for maximum similarity scores (Kondrak 2002: 54)
|
50 |
+
C_skip = -10 # Indels
|
51 |
+
C_sub = 35 # Substitutions
|
52 |
+
C_exp = 45 # Expansions/compressions
|
53 |
+
C_vwl = 5 # Vowel/consonant relative weight (decreased from 10)
|
54 |
+
|
55 |
+
consonants = [
|
56 |
+
"B",
|
57 |
+
"N",
|
58 |
+
"R",
|
59 |
+
"b",
|
60 |
+
"c",
|
61 |
+
"d",
|
62 |
+
"f",
|
63 |
+
"g",
|
64 |
+
"h",
|
65 |
+
"j",
|
66 |
+
"k",
|
67 |
+
"l",
|
68 |
+
"m",
|
69 |
+
"n",
|
70 |
+
"p",
|
71 |
+
"q",
|
72 |
+
"r",
|
73 |
+
"s",
|
74 |
+
"t",
|
75 |
+
"v",
|
76 |
+
"x",
|
77 |
+
"z",
|
78 |
+
"ç",
|
79 |
+
"ð",
|
80 |
+
"ħ",
|
81 |
+
"ŋ",
|
82 |
+
"ɖ",
|
83 |
+
"ɟ",
|
84 |
+
"ɢ",
|
85 |
+
"ɣ",
|
86 |
+
"ɦ",
|
87 |
+
"ɬ",
|
88 |
+
"ɮ",
|
89 |
+
"ɰ",
|
90 |
+
"ɱ",
|
91 |
+
"ɲ",
|
92 |
+
"ɳ",
|
93 |
+
"ɴ",
|
94 |
+
"ɸ",
|
95 |
+
"ɹ",
|
96 |
+
"ɻ",
|
97 |
+
"ɽ",
|
98 |
+
"ɾ",
|
99 |
+
"ʀ",
|
100 |
+
"ʁ",
|
101 |
+
"ʂ",
|
102 |
+
"ʃ",
|
103 |
+
"ʈ",
|
104 |
+
"ʋ",
|
105 |
+
"ʐ ",
|
106 |
+
"ʒ",
|
107 |
+
"ʔ",
|
108 |
+
"ʕ",
|
109 |
+
"ʙ",
|
110 |
+
"ʝ",
|
111 |
+
"β",
|
112 |
+
"θ",
|
113 |
+
"χ",
|
114 |
+
"ʐ",
|
115 |
+
"w",
|
116 |
+
]
|
117 |
+
|
118 |
+
# Relevant features for comparing consonants and vowels
|
119 |
+
R_c = [
|
120 |
+
"aspirated",
|
121 |
+
"lateral",
|
122 |
+
"manner",
|
123 |
+
"nasal",
|
124 |
+
"place",
|
125 |
+
"retroflex",
|
126 |
+
"syllabic",
|
127 |
+
"voice",
|
128 |
+
]
|
129 |
+
# 'high' taken out of R_v because same as manner
|
130 |
+
R_v = [
|
131 |
+
"back",
|
132 |
+
"lateral",
|
133 |
+
"long",
|
134 |
+
"manner",
|
135 |
+
"nasal",
|
136 |
+
"place",
|
137 |
+
"retroflex",
|
138 |
+
"round",
|
139 |
+
"syllabic",
|
140 |
+
"voice",
|
141 |
+
]
|
142 |
+
|
143 |
+
# Flattened feature matrix (Kondrak 2002: 56)
|
144 |
+
similarity_matrix = {
|
145 |
+
# place
|
146 |
+
"bilabial": 1.0,
|
147 |
+
"labiodental": 0.95,
|
148 |
+
"dental": 0.9,
|
149 |
+
"alveolar": 0.85,
|
150 |
+
"retroflex": 0.8,
|
151 |
+
"palato-alveolar": 0.75,
|
152 |
+
"palatal": 0.7,
|
153 |
+
"velar": 0.6,
|
154 |
+
"uvular": 0.5,
|
155 |
+
"pharyngeal": 0.3,
|
156 |
+
"glottal": 0.1,
|
157 |
+
"labiovelar": 1.0,
|
158 |
+
"vowel": -1.0, # added 'vowel'
|
159 |
+
# manner
|
160 |
+
"stop": 1.0,
|
161 |
+
"affricate": 0.9,
|
162 |
+
"fricative": 0.85, # increased fricative from 0.8
|
163 |
+
"trill": 0.7,
|
164 |
+
"tap": 0.65,
|
165 |
+
"approximant": 0.6,
|
166 |
+
"high vowel": 0.4,
|
167 |
+
"mid vowel": 0.2,
|
168 |
+
"low vowel": 0.0,
|
169 |
+
"vowel2": 0.5, # added vowel
|
170 |
+
# high
|
171 |
+
"high": 1.0,
|
172 |
+
"mid": 0.5,
|
173 |
+
"low": 0.0,
|
174 |
+
# back
|
175 |
+
"front": 1.0,
|
176 |
+
"central": 0.5,
|
177 |
+
"back": 0.0,
|
178 |
+
# binary features
|
179 |
+
"plus": 1.0,
|
180 |
+
"minus": 0.0,
|
181 |
+
}
|
182 |
+
|
183 |
+
# Relative weights of phonetic features (Kondrak 2002: 55)
|
184 |
+
salience = {
|
185 |
+
"syllabic": 5,
|
186 |
+
"place": 40,
|
187 |
+
"manner": 50,
|
188 |
+
"voice": 5, # decreased from 10
|
189 |
+
"nasal": 20, # increased from 10
|
190 |
+
"retroflex": 10,
|
191 |
+
"lateral": 10,
|
192 |
+
"aspirated": 5,
|
193 |
+
"long": 0, # decreased from 1
|
194 |
+
"high": 3, # decreased from 5
|
195 |
+
"back": 2, # decreased from 5
|
196 |
+
"round": 2, # decreased from 5
|
197 |
+
}
|
198 |
+
|
199 |
+
# (Kondrak 2002: 59-60)
|
200 |
+
feature_matrix = {
|
201 |
+
# Consonants
|
202 |
+
"p": {
|
203 |
+
"place": "bilabial",
|
204 |
+
"manner": "stop",
|
205 |
+
"syllabic": "minus",
|
206 |
+
"voice": "minus",
|
207 |
+
"nasal": "minus",
|
208 |
+
"retroflex": "minus",
|
209 |
+
"lateral": "minus",
|
210 |
+
"aspirated": "minus",
|
211 |
+
},
|
212 |
+
"b": {
|
213 |
+
"place": "bilabial",
|
214 |
+
"manner": "stop",
|
215 |
+
"syllabic": "minus",
|
216 |
+
"voice": "plus",
|
217 |
+
"nasal": "minus",
|
218 |
+
"retroflex": "minus",
|
219 |
+
"lateral": "minus",
|
220 |
+
"aspirated": "minus",
|
221 |
+
},
|
222 |
+
"t": {
|
223 |
+
"place": "alveolar",
|
224 |
+
"manner": "stop",
|
225 |
+
"syllabic": "minus",
|
226 |
+
"voice": "minus",
|
227 |
+
"nasal": "minus",
|
228 |
+
"retroflex": "minus",
|
229 |
+
"lateral": "minus",
|
230 |
+
"aspirated": "minus",
|
231 |
+
},
|
232 |
+
"d": {
|
233 |
+
"place": "alveolar",
|
234 |
+
"manner": "stop",
|
235 |
+
"syllabic": "minus",
|
236 |
+
"voice": "plus",
|
237 |
+
"nasal": "minus",
|
238 |
+
"retroflex": "minus",
|
239 |
+
"lateral": "minus",
|
240 |
+
"aspirated": "minus",
|
241 |
+
},
|
242 |
+
"ʈ": {
|
243 |
+
"place": "retroflex",
|
244 |
+
"manner": "stop",
|
245 |
+
"syllabic": "minus",
|
246 |
+
"voice": "minus",
|
247 |
+
"nasal": "minus",
|
248 |
+
"retroflex": "plus",
|
249 |
+
"lateral": "minus",
|
250 |
+
"aspirated": "minus",
|
251 |
+
},
|
252 |
+
"ɖ": {
|
253 |
+
"place": "retroflex",
|
254 |
+
"manner": "stop",
|
255 |
+
"syllabic": "minus",
|
256 |
+
"voice": "plus",
|
257 |
+
"nasal": "minus",
|
258 |
+
"retroflex": "plus",
|
259 |
+
"lateral": "minus",
|
260 |
+
"aspirated": "minus",
|
261 |
+
},
|
262 |
+
"c": {
|
263 |
+
"place": "palatal",
|
264 |
+
"manner": "stop",
|
265 |
+
"syllabic": "minus",
|
266 |
+
"voice": "minus",
|
267 |
+
"nasal": "minus",
|
268 |
+
"retroflex": "minus",
|
269 |
+
"lateral": "minus",
|
270 |
+
"aspirated": "minus",
|
271 |
+
},
|
272 |
+
"ɟ": {
|
273 |
+
"place": "palatal",
|
274 |
+
"manner": "stop",
|
275 |
+
"syllabic": "minus",
|
276 |
+
"voice": "plus",
|
277 |
+
"nasal": "minus",
|
278 |
+
"retroflex": "minus",
|
279 |
+
"lateral": "minus",
|
280 |
+
"aspirated": "minus",
|
281 |
+
},
|
282 |
+
"k": {
|
283 |
+
"place": "velar",
|
284 |
+
"manner": "stop",
|
285 |
+
"syllabic": "minus",
|
286 |
+
"voice": "minus",
|
287 |
+
"nasal": "minus",
|
288 |
+
"retroflex": "minus",
|
289 |
+
"lateral": "minus",
|
290 |
+
"aspirated": "minus",
|
291 |
+
},
|
292 |
+
"g": {
|
293 |
+
"place": "velar",
|
294 |
+
"manner": "stop",
|
295 |
+
"syllabic": "minus",
|
296 |
+
"voice": "plus",
|
297 |
+
"nasal": "minus",
|
298 |
+
"retroflex": "minus",
|
299 |
+
"lateral": "minus",
|
300 |
+
"aspirated": "minus",
|
301 |
+
},
|
302 |
+
"q": {
|
303 |
+
"place": "uvular",
|
304 |
+
"manner": "stop",
|
305 |
+
"syllabic": "minus",
|
306 |
+
"voice": "minus",
|
307 |
+
"nasal": "minus",
|
308 |
+
"retroflex": "minus",
|
309 |
+
"lateral": "minus",
|
310 |
+
"aspirated": "minus",
|
311 |
+
},
|
312 |
+
"ɢ": {
|
313 |
+
"place": "uvular",
|
314 |
+
"manner": "stop",
|
315 |
+
"syllabic": "minus",
|
316 |
+
"voice": "plus",
|
317 |
+
"nasal": "minus",
|
318 |
+
"retroflex": "minus",
|
319 |
+
"lateral": "minus",
|
320 |
+
"aspirated": "minus",
|
321 |
+
},
|
322 |
+
"ʔ": {
|
323 |
+
"place": "glottal",
|
324 |
+
"manner": "stop",
|
325 |
+
"syllabic": "minus",
|
326 |
+
"voice": "minus",
|
327 |
+
"nasal": "minus",
|
328 |
+
"retroflex": "minus",
|
329 |
+
"lateral": "minus",
|
330 |
+
"aspirated": "minus",
|
331 |
+
},
|
332 |
+
"m": {
|
333 |
+
"place": "bilabial",
|
334 |
+
"manner": "stop",
|
335 |
+
"syllabic": "minus",
|
336 |
+
"voice": "plus",
|
337 |
+
"nasal": "plus",
|
338 |
+
"retroflex": "minus",
|
339 |
+
"lateral": "minus",
|
340 |
+
"aspirated": "minus",
|
341 |
+
},
|
342 |
+
"ɱ": {
|
343 |
+
"place": "labiodental",
|
344 |
+
"manner": "stop",
|
345 |
+
"syllabic": "minus",
|
346 |
+
"voice": "plus",
|
347 |
+
"nasal": "plus",
|
348 |
+
"retroflex": "minus",
|
349 |
+
"lateral": "minus",
|
350 |
+
"aspirated": "minus",
|
351 |
+
},
|
352 |
+
"n": {
|
353 |
+
"place": "alveolar",
|
354 |
+
"manner": "stop",
|
355 |
+
"syllabic": "minus",
|
356 |
+
"voice": "plus",
|
357 |
+
"nasal": "plus",
|
358 |
+
"retroflex": "minus",
|
359 |
+
"lateral": "minus",
|
360 |
+
"aspirated": "minus",
|
361 |
+
},
|
362 |
+
"ɳ": {
|
363 |
+
"place": "retroflex",
|
364 |
+
"manner": "stop",
|
365 |
+
"syllabic": "minus",
|
366 |
+
"voice": "plus",
|
367 |
+
"nasal": "plus",
|
368 |
+
"retroflex": "plus",
|
369 |
+
"lateral": "minus",
|
370 |
+
"aspirated": "minus",
|
371 |
+
},
|
372 |
+
"ɲ": {
|
373 |
+
"place": "palatal",
|
374 |
+
"manner": "stop",
|
375 |
+
"syllabic": "minus",
|
376 |
+
"voice": "plus",
|
377 |
+
"nasal": "plus",
|
378 |
+
"retroflex": "minus",
|
379 |
+
"lateral": "minus",
|
380 |
+
"aspirated": "minus",
|
381 |
+
},
|
382 |
+
"ŋ": {
|
383 |
+
"place": "velar",
|
384 |
+
"manner": "stop",
|
385 |
+
"syllabic": "minus",
|
386 |
+
"voice": "plus",
|
387 |
+
"nasal": "plus",
|
388 |
+
"retroflex": "minus",
|
389 |
+
"lateral": "minus",
|
390 |
+
"aspirated": "minus",
|
391 |
+
},
|
392 |
+
"ɴ": {
|
393 |
+
"place": "uvular",
|
394 |
+
"manner": "stop",
|
395 |
+
"syllabic": "minus",
|
396 |
+
"voice": "plus",
|
397 |
+
"nasal": "plus",
|
398 |
+
"retroflex": "minus",
|
399 |
+
"lateral": "minus",
|
400 |
+
"aspirated": "minus",
|
401 |
+
},
|
402 |
+
"N": {
|
403 |
+
"place": "uvular",
|
404 |
+
"manner": "stop",
|
405 |
+
"syllabic": "minus",
|
406 |
+
"voice": "plus",
|
407 |
+
"nasal": "plus",
|
408 |
+
"retroflex": "minus",
|
409 |
+
"lateral": "minus",
|
410 |
+
"aspirated": "minus",
|
411 |
+
},
|
412 |
+
"ʙ": {
|
413 |
+
"place": "bilabial",
|
414 |
+
"manner": "trill",
|
415 |
+
"syllabic": "minus",
|
416 |
+
"voice": "plus",
|
417 |
+
"nasal": "minus",
|
418 |
+
"retroflex": "minus",
|
419 |
+
"lateral": "minus",
|
420 |
+
"aspirated": "minus",
|
421 |
+
},
|
422 |
+
"B": {
|
423 |
+
"place": "bilabial",
|
424 |
+
"manner": "trill",
|
425 |
+
"syllabic": "minus",
|
426 |
+
"voice": "plus",
|
427 |
+
"nasal": "minus",
|
428 |
+
"retroflex": "minus",
|
429 |
+
"lateral": "minus",
|
430 |
+
"aspirated": "minus",
|
431 |
+
},
|
432 |
+
"r": {
|
433 |
+
"place": "alveolar",
|
434 |
+
"manner": "trill",
|
435 |
+
"syllabic": "minus",
|
436 |
+
"voice": "plus",
|
437 |
+
"nasal": "minus",
|
438 |
+
"retroflex": "plus",
|
439 |
+
"lateral": "minus",
|
440 |
+
"aspirated": "minus",
|
441 |
+
},
|
442 |
+
"ʀ": {
|
443 |
+
"place": "uvular",
|
444 |
+
"manner": "trill",
|
445 |
+
"syllabic": "minus",
|
446 |
+
"voice": "plus",
|
447 |
+
"nasal": "minus",
|
448 |
+
"retroflex": "minus",
|
449 |
+
"lateral": "minus",
|
450 |
+
"aspirated": "minus",
|
451 |
+
},
|
452 |
+
"R": {
|
453 |
+
"place": "uvular",
|
454 |
+
"manner": "trill",
|
455 |
+
"syllabic": "minus",
|
456 |
+
"voice": "plus",
|
457 |
+
"nasal": "minus",
|
458 |
+
"retroflex": "minus",
|
459 |
+
"lateral": "minus",
|
460 |
+
"aspirated": "minus",
|
461 |
+
},
|
462 |
+
"ɾ": {
|
463 |
+
"place": "alveolar",
|
464 |
+
"manner": "tap",
|
465 |
+
"syllabic": "minus",
|
466 |
+
"voice": "plus",
|
467 |
+
"nasal": "minus",
|
468 |
+
"retroflex": "minus",
|
469 |
+
"lateral": "minus",
|
470 |
+
"aspirated": "minus",
|
471 |
+
},
|
472 |
+
"ɽ": {
|
473 |
+
"place": "retroflex",
|
474 |
+
"manner": "tap",
|
475 |
+
"syllabic": "minus",
|
476 |
+
"voice": "plus",
|
477 |
+
"nasal": "minus",
|
478 |
+
"retroflex": "plus",
|
479 |
+
"lateral": "minus",
|
480 |
+
"aspirated": "minus",
|
481 |
+
},
|
482 |
+
"ɸ": {
|
483 |
+
"place": "bilabial",
|
484 |
+
"manner": "fricative",
|
485 |
+
"syllabic": "minus",
|
486 |
+
"voice": "minus",
|
487 |
+
"nasal": "minus",
|
488 |
+
"retroflex": "minus",
|
489 |
+
"lateral": "minus",
|
490 |
+
"aspirated": "minus",
|
491 |
+
},
|
492 |
+
"β": {
|
493 |
+
"place": "bilabial",
|
494 |
+
"manner": "fricative",
|
495 |
+
"syllabic": "minus",
|
496 |
+
"voice": "plus",
|
497 |
+
"nasal": "minus",
|
498 |
+
"retroflex": "minus",
|
499 |
+
"lateral": "minus",
|
500 |
+
"aspirated": "minus",
|
501 |
+
},
|
502 |
+
"f": {
|
503 |
+
"place": "labiodental",
|
504 |
+
"manner": "fricative",
|
505 |
+
"syllabic": "minus",
|
506 |
+
"voice": "minus",
|
507 |
+
"nasal": "minus",
|
508 |
+
"retroflex": "minus",
|
509 |
+
"lateral": "minus",
|
510 |
+
"aspirated": "minus",
|
511 |
+
},
|
512 |
+
"v": {
|
513 |
+
"place": "labiodental",
|
514 |
+
"manner": "fricative",
|
515 |
+
"syllabic": "minus",
|
516 |
+
"voice": "plus",
|
517 |
+
"nasal": "minus",
|
518 |
+
"retroflex": "minus",
|
519 |
+
"lateral": "minus",
|
520 |
+
"aspirated": "minus",
|
521 |
+
},
|
522 |
+
"θ": {
|
523 |
+
"place": "dental",
|
524 |
+
"manner": "fricative",
|
525 |
+
"syllabic": "minus",
|
526 |
+
"voice": "minus",
|
527 |
+
"nasal": "minus",
|
528 |
+
"retroflex": "minus",
|
529 |
+
"lateral": "minus",
|
530 |
+
"aspirated": "minus",
|
531 |
+
},
|
532 |
+
"ð": {
|
533 |
+
"place": "dental",
|
534 |
+
"manner": "fricative",
|
535 |
+
"syllabic": "minus",
|
536 |
+
"voice": "plus",
|
537 |
+
"nasal": "minus",
|
538 |
+
"retroflex": "minus",
|
539 |
+
"lateral": "minus",
|
540 |
+
"aspirated": "minus",
|
541 |
+
},
|
542 |
+
"s": {
|
543 |
+
"place": "alveolar",
|
544 |
+
"manner": "fricative",
|
545 |
+
"syllabic": "minus",
|
546 |
+
"voice": "minus",
|
547 |
+
"nasal": "minus",
|
548 |
+
"retroflex": "minus",
|
549 |
+
"lateral": "minus",
|
550 |
+
"aspirated": "minus",
|
551 |
+
},
|
552 |
+
"z": {
|
553 |
+
"place": "alveolar",
|
554 |
+
"manner": "fricative",
|
555 |
+
"syllabic": "minus",
|
556 |
+
"voice": "plus",
|
557 |
+
"nasal": "minus",
|
558 |
+
"retroflex": "minus",
|
559 |
+
"lateral": "minus",
|
560 |
+
"aspirated": "minus",
|
561 |
+
},
|
562 |
+
"ʃ": {
|
563 |
+
"place": "palato-alveolar",
|
564 |
+
"manner": "fricative",
|
565 |
+
"syllabic": "minus",
|
566 |
+
"voice": "minus",
|
567 |
+
"nasal": "minus",
|
568 |
+
"retroflex": "minus",
|
569 |
+
"lateral": "minus",
|
570 |
+
"aspirated": "minus",
|
571 |
+
},
|
572 |
+
"ʒ": {
|
573 |
+
"place": "palato-alveolar",
|
574 |
+
"manner": "fricative",
|
575 |
+
"syllabic": "minus",
|
576 |
+
"voice": "plus",
|
577 |
+
"nasal": "minus",
|
578 |
+
"retroflex": "minus",
|
579 |
+
"lateral": "minus",
|
580 |
+
"aspirated": "minus",
|
581 |
+
},
|
582 |
+
"ʂ": {
|
583 |
+
"place": "retroflex",
|
584 |
+
"manner": "fricative",
|
585 |
+
"syllabic": "minus",
|
586 |
+
"voice": "minus",
|
587 |
+
"nasal": "minus",
|
588 |
+
"retroflex": "plus",
|
589 |
+
"lateral": "minus",
|
590 |
+
"aspirated": "minus",
|
591 |
+
},
|
592 |
+
"ʐ": {
|
593 |
+
"place": "retroflex",
|
594 |
+
"manner": "fricative",
|
595 |
+
"syllabic": "minus",
|
596 |
+
"voice": "plus",
|
597 |
+
"nasal": "minus",
|
598 |
+
"retroflex": "plus",
|
599 |
+
"lateral": "minus",
|
600 |
+
"aspirated": "minus",
|
601 |
+
},
|
602 |
+
"ç": {
|
603 |
+
"place": "palatal",
|
604 |
+
"manner": "fricative",
|
605 |
+
"syllabic": "minus",
|
606 |
+
"voice": "minus",
|
607 |
+
"nasal": "minus",
|
608 |
+
"retroflex": "minus",
|
609 |
+
"lateral": "minus",
|
610 |
+
"aspirated": "minus",
|
611 |
+
},
|
612 |
+
"ʝ": {
|
613 |
+
"place": "palatal",
|
614 |
+
"manner": "fricative",
|
615 |
+
"syllabic": "minus",
|
616 |
+
"voice": "plus",
|
617 |
+
"nasal": "minus",
|
618 |
+
"retroflex": "minus",
|
619 |
+
"lateral": "minus",
|
620 |
+
"aspirated": "minus",
|
621 |
+
},
|
622 |
+
"x": {
|
623 |
+
"place": "velar",
|
624 |
+
"manner": "fricative",
|
625 |
+
"syllabic": "minus",
|
626 |
+
"voice": "minus",
|
627 |
+
"nasal": "minus",
|
628 |
+
"retroflex": "minus",
|
629 |
+
"lateral": "minus",
|
630 |
+
"aspirated": "minus",
|
631 |
+
},
|
632 |
+
"ɣ": {
|
633 |
+
"place": "velar",
|
634 |
+
"manner": "fricative",
|
635 |
+
"syllabic": "minus",
|
636 |
+
"voice": "plus",
|
637 |
+
"nasal": "minus",
|
638 |
+
"retroflex": "minus",
|
639 |
+
"lateral": "minus",
|
640 |
+
"aspirated": "minus",
|
641 |
+
},
|
642 |
+
"χ": {
|
643 |
+
"place": "uvular",
|
644 |
+
"manner": "fricative",
|
645 |
+
"syllabic": "minus",
|
646 |
+
"voice": "minus",
|
647 |
+
"nasal": "minus",
|
648 |
+
"retroflex": "minus",
|
649 |
+
"lateral": "minus",
|
650 |
+
"aspirated": "minus",
|
651 |
+
},
|
652 |
+
"ʁ": {
|
653 |
+
"place": "uvular",
|
654 |
+
"manner": "fricative",
|
655 |
+
"syllabic": "minus",
|
656 |
+
"voice": "plus",
|
657 |
+
"nasal": "minus",
|
658 |
+
"retroflex": "minus",
|
659 |
+
"lateral": "minus",
|
660 |
+
"aspirated": "minus",
|
661 |
+
},
|
662 |
+
"ħ": {
|
663 |
+
"place": "pharyngeal",
|
664 |
+
"manner": "fricative",
|
665 |
+
"syllabic": "minus",
|
666 |
+
"voice": "minus",
|
667 |
+
"nasal": "minus",
|
668 |
+
"retroflex": "minus",
|
669 |
+
"lateral": "minus",
|
670 |
+
"aspirated": "minus",
|
671 |
+
},
|
672 |
+
"ʕ": {
|
673 |
+
"place": "pharyngeal",
|
674 |
+
"manner": "fricative",
|
675 |
+
"syllabic": "minus",
|
676 |
+
"voice": "plus",
|
677 |
+
"nasal": "minus",
|
678 |
+
"retroflex": "minus",
|
679 |
+
"lateral": "minus",
|
680 |
+
"aspirated": "minus",
|
681 |
+
},
|
682 |
+
"h": {
|
683 |
+
"place": "glottal",
|
684 |
+
"manner": "fricative",
|
685 |
+
"syllabic": "minus",
|
686 |
+
"voice": "minus",
|
687 |
+
"nasal": "minus",
|
688 |
+
"retroflex": "minus",
|
689 |
+
"lateral": "minus",
|
690 |
+
"aspirated": "minus",
|
691 |
+
},
|
692 |
+
"ɦ": {
|
693 |
+
"place": "glottal",
|
694 |
+
"manner": "fricative",
|
695 |
+
"syllabic": "minus",
|
696 |
+
"voice": "plus",
|
697 |
+
"nasal": "minus",
|
698 |
+
"retroflex": "minus",
|
699 |
+
"lateral": "minus",
|
700 |
+
"aspirated": "minus",
|
701 |
+
},
|
702 |
+
"ɬ": {
|
703 |
+
"place": "alveolar",
|
704 |
+
"manner": "fricative",
|
705 |
+
"syllabic": "minus",
|
706 |
+
"voice": "minus",
|
707 |
+
"nasal": "minus",
|
708 |
+
"retroflex": "minus",
|
709 |
+
"lateral": "plus",
|
710 |
+
"aspirated": "minus",
|
711 |
+
},
|
712 |
+
"ɮ": {
|
713 |
+
"place": "alveolar",
|
714 |
+
"manner": "fricative",
|
715 |
+
"syllabic": "minus",
|
716 |
+
"voice": "plus",
|
717 |
+
"nasal": "minus",
|
718 |
+
"retroflex": "minus",
|
719 |
+
"lateral": "plus",
|
720 |
+
"aspirated": "minus",
|
721 |
+
},
|
722 |
+
"ʋ": {
|
723 |
+
"place": "labiodental",
|
724 |
+
"manner": "approximant",
|
725 |
+
"syllabic": "minus",
|
726 |
+
"voice": "plus",
|
727 |
+
"nasal": "minus",
|
728 |
+
"retroflex": "minus",
|
729 |
+
"lateral": "minus",
|
730 |
+
"aspirated": "minus",
|
731 |
+
},
|
732 |
+
"ɹ": {
|
733 |
+
"place": "alveolar",
|
734 |
+
"manner": "approximant",
|
735 |
+
"syllabic": "minus",
|
736 |
+
"voice": "plus",
|
737 |
+
"nasal": "minus",
|
738 |
+
"retroflex": "minus",
|
739 |
+
"lateral": "minus",
|
740 |
+
"aspirated": "minus",
|
741 |
+
},
|
742 |
+
"ɻ": {
|
743 |
+
"place": "retroflex",
|
744 |
+
"manner": "approximant",
|
745 |
+
"syllabic": "minus",
|
746 |
+
"voice": "plus",
|
747 |
+
"nasal": "minus",
|
748 |
+
"retroflex": "plus",
|
749 |
+
"lateral": "minus",
|
750 |
+
"aspirated": "minus",
|
751 |
+
},
|
752 |
+
"j": {
|
753 |
+
"place": "palatal",
|
754 |
+
"manner": "approximant",
|
755 |
+
"syllabic": "minus",
|
756 |
+
"voice": "plus",
|
757 |
+
"nasal": "minus",
|
758 |
+
"retroflex": "minus",
|
759 |
+
"lateral": "minus",
|
760 |
+
"aspirated": "minus",
|
761 |
+
},
|
762 |
+
"ɰ": {
|
763 |
+
"place": "velar",
|
764 |
+
"manner": "approximant",
|
765 |
+
"syllabic": "minus",
|
766 |
+
"voice": "plus",
|
767 |
+
"nasal": "minus",
|
768 |
+
"retroflex": "minus",
|
769 |
+
"lateral": "minus",
|
770 |
+
"aspirated": "minus",
|
771 |
+
},
|
772 |
+
"l": {
|
773 |
+
"place": "alveolar",
|
774 |
+
"manner": "approximant",
|
775 |
+
"syllabic": "minus",
|
776 |
+
"voice": "plus",
|
777 |
+
"nasal": "minus",
|
778 |
+
"retroflex": "minus",
|
779 |
+
"lateral": "plus",
|
780 |
+
"aspirated": "minus",
|
781 |
+
},
|
782 |
+
"w": {
|
783 |
+
"place": "labiovelar",
|
784 |
+
"manner": "approximant",
|
785 |
+
"syllabic": "minus",
|
786 |
+
"voice": "plus",
|
787 |
+
"nasal": "minus",
|
788 |
+
"retroflex": "minus",
|
789 |
+
"lateral": "minus",
|
790 |
+
"aspirated": "minus",
|
791 |
+
},
|
792 |
+
# Vowels
|
793 |
+
"i": {
|
794 |
+
"place": "vowel",
|
795 |
+
"manner": "vowel2",
|
796 |
+
"syllabic": "plus",
|
797 |
+
"voice": "plus",
|
798 |
+
"nasal": "minus",
|
799 |
+
"retroflex": "minus",
|
800 |
+
"lateral": "minus",
|
801 |
+
"high": "high",
|
802 |
+
"back": "front",
|
803 |
+
"round": "minus",
|
804 |
+
"long": "minus",
|
805 |
+
"aspirated": "minus",
|
806 |
+
},
|
807 |
+
"y": {
|
808 |
+
"place": "vowel",
|
809 |
+
"manner": "vowel2",
|
810 |
+
"syllabic": "plus",
|
811 |
+
"voice": "plus",
|
812 |
+
"nasal": "minus",
|
813 |
+
"retroflex": "minus",
|
814 |
+
"lateral": "minus",
|
815 |
+
"high": "high",
|
816 |
+
"back": "front",
|
817 |
+
"round": "plus",
|
818 |
+
"long": "minus",
|
819 |
+
"aspirated": "minus",
|
820 |
+
},
|
821 |
+
"e": {
|
822 |
+
"place": "vowel",
|
823 |
+
"manner": "vowel2",
|
824 |
+
"syllabic": "plus",
|
825 |
+
"voice": "plus",
|
826 |
+
"nasal": "minus",
|
827 |
+
"retroflex": "minus",
|
828 |
+
"lateral": "minus",
|
829 |
+
"high": "mid",
|
830 |
+
"back": "front",
|
831 |
+
"round": "minus",
|
832 |
+
"long": "minus",
|
833 |
+
"aspirated": "minus",
|
834 |
+
},
|
835 |
+
"E": {
|
836 |
+
"place": "vowel",
|
837 |
+
"manner": "vowel2",
|
838 |
+
"syllabic": "plus",
|
839 |
+
"voice": "plus",
|
840 |
+
"nasal": "minus",
|
841 |
+
"retroflex": "minus",
|
842 |
+
"lateral": "minus",
|
843 |
+
"high": "mid",
|
844 |
+
"back": "front",
|
845 |
+
"round": "minus",
|
846 |
+
"long": "plus",
|
847 |
+
"aspirated": "minus",
|
848 |
+
},
|
849 |
+
"ø": {
|
850 |
+
"place": "vowel",
|
851 |
+
"manner": "vowel2",
|
852 |
+
"syllabic": "plus",
|
853 |
+
"voice": "plus",
|
854 |
+
"nasal": "minus",
|
855 |
+
"retroflex": "minus",
|
856 |
+
"lateral": "minus",
|
857 |
+
"high": "mid",
|
858 |
+
"back": "front",
|
859 |
+
"round": "plus",
|
860 |
+
"long": "minus",
|
861 |
+
"aspirated": "minus",
|
862 |
+
},
|
863 |
+
"ɛ": {
|
864 |
+
"place": "vowel",
|
865 |
+
"manner": "vowel2",
|
866 |
+
"syllabic": "plus",
|
867 |
+
"voice": "plus",
|
868 |
+
"nasal": "minus",
|
869 |
+
"retroflex": "minus",
|
870 |
+
"lateral": "minus",
|
871 |
+
"high": "mid",
|
872 |
+
"back": "front",
|
873 |
+
"round": "minus",
|
874 |
+
"long": "minus",
|
875 |
+
"aspirated": "minus",
|
876 |
+
},
|
877 |
+
"œ": {
|
878 |
+
"place": "vowel",
|
879 |
+
"manner": "vowel2",
|
880 |
+
"syllabic": "plus",
|
881 |
+
"voice": "plus",
|
882 |
+
"nasal": "minus",
|
883 |
+
"retroflex": "minus",
|
884 |
+
"lateral": "minus",
|
885 |
+
"high": "mid",
|
886 |
+
"back": "front",
|
887 |
+
"round": "plus",
|
888 |
+
"long": "minus",
|
889 |
+
"aspirated": "minus",
|
890 |
+
},
|
891 |
+
"æ": {
|
892 |
+
"place": "vowel",
|
893 |
+
"manner": "vowel2",
|
894 |
+
"syllabic": "plus",
|
895 |
+
"voice": "plus",
|
896 |
+
"nasal": "minus",
|
897 |
+
"retroflex": "minus",
|
898 |
+
"lateral": "minus",
|
899 |
+
"high": "low",
|
900 |
+
"back": "front",
|
901 |
+
"round": "minus",
|
902 |
+
"long": "minus",
|
903 |
+
"aspirated": "minus",
|
904 |
+
},
|
905 |
+
"a": {
|
906 |
+
"place": "vowel",
|
907 |
+
"manner": "vowel2",
|
908 |
+
"syllabic": "plus",
|
909 |
+
"voice": "plus",
|
910 |
+
"nasal": "minus",
|
911 |
+
"retroflex": "minus",
|
912 |
+
"lateral": "minus",
|
913 |
+
"high": "low",
|
914 |
+
"back": "front",
|
915 |
+
"round": "minus",
|
916 |
+
"long": "minus",
|
917 |
+
"aspirated": "minus",
|
918 |
+
},
|
919 |
+
"A": {
|
920 |
+
"place": "vowel",
|
921 |
+
"manner": "vowel2",
|
922 |
+
"syllabic": "plus",
|
923 |
+
"voice": "plus",
|
924 |
+
"nasal": "minus",
|
925 |
+
"retroflex": "minus",
|
926 |
+
"lateral": "minus",
|
927 |
+
"high": "low",
|
928 |
+
"back": "front",
|
929 |
+
"round": "minus",
|
930 |
+
"long": "plus",
|
931 |
+
"aspirated": "minus",
|
932 |
+
},
|
933 |
+
"ɨ": {
|
934 |
+
"place": "vowel",
|
935 |
+
"manner": "vowel2",
|
936 |
+
"syllabic": "plus",
|
937 |
+
"voice": "plus",
|
938 |
+
"nasal": "minus",
|
939 |
+
"retroflex": "minus",
|
940 |
+
"lateral": "minus",
|
941 |
+
"high": "high",
|
942 |
+
"back": "central",
|
943 |
+
"round": "minus",
|
944 |
+
"long": "minus",
|
945 |
+
"aspirated": "minus",
|
946 |
+
},
|
947 |
+
"ʉ": {
|
948 |
+
"place": "vowel",
|
949 |
+
"manner": "vowel2",
|
950 |
+
"syllabic": "plus",
|
951 |
+
"voice": "plus",
|
952 |
+
"nasal": "minus",
|
953 |
+
"retroflex": "minus",
|
954 |
+
"lateral": "minus",
|
955 |
+
"high": "high",
|
956 |
+
"back": "central",
|
957 |
+
"round": "plus",
|
958 |
+
"long": "minus",
|
959 |
+
"aspirated": "minus",
|
960 |
+
},
|
961 |
+
"ə": {
|
962 |
+
"place": "vowel",
|
963 |
+
"manner": "vowel2",
|
964 |
+
"syllabic": "plus",
|
965 |
+
"voice": "plus",
|
966 |
+
"nasal": "minus",
|
967 |
+
"retroflex": "minus",
|
968 |
+
"lateral": "minus",
|
969 |
+
"high": "mid",
|
970 |
+
"back": "central",
|
971 |
+
"round": "minus",
|
972 |
+
"long": "minus",
|
973 |
+
"aspirated": "minus",
|
974 |
+
},
|
975 |
+
"u": {
|
976 |
+
"place": "vowel",
|
977 |
+
"manner": "vowel2",
|
978 |
+
"syllabic": "plus",
|
979 |
+
"voice": "plus",
|
980 |
+
"nasal": "minus",
|
981 |
+
"retroflex": "minus",
|
982 |
+
"lateral": "minus",
|
983 |
+
"high": "high",
|
984 |
+
"back": "back",
|
985 |
+
"round": "plus",
|
986 |
+
"long": "minus",
|
987 |
+
"aspirated": "minus",
|
988 |
+
},
|
989 |
+
"U": {
|
990 |
+
"place": "vowel",
|
991 |
+
"manner": "vowel2",
|
992 |
+
"syllabic": "plus",
|
993 |
+
"voice": "plus",
|
994 |
+
"nasal": "minus",
|
995 |
+
"retroflex": "minus",
|
996 |
+
"lateral": "minus",
|
997 |
+
"high": "high",
|
998 |
+
"back": "back",
|
999 |
+
"round": "plus",
|
1000 |
+
"long": "plus",
|
1001 |
+
"aspirated": "minus",
|
1002 |
+
},
|
1003 |
+
"o": {
|
1004 |
+
"place": "vowel",
|
1005 |
+
"manner": "vowel2",
|
1006 |
+
"syllabic": "plus",
|
1007 |
+
"voice": "plus",
|
1008 |
+
"nasal": "minus",
|
1009 |
+
"retroflex": "minus",
|
1010 |
+
"lateral": "minus",
|
1011 |
+
"high": "mid",
|
1012 |
+
"back": "back",
|
1013 |
+
"round": "plus",
|
1014 |
+
"long": "minus",
|
1015 |
+
"aspirated": "minus",
|
1016 |
+
},
|
1017 |
+
"O": {
|
1018 |
+
"place": "vowel",
|
1019 |
+
"manner": "vowel2",
|
1020 |
+
"syllabic": "plus",
|
1021 |
+
"voice": "plus",
|
1022 |
+
"nasal": "minus",
|
1023 |
+
"retroflex": "minus",
|
1024 |
+
"lateral": "minus",
|
1025 |
+
"high": "mid",
|
1026 |
+
"back": "back",
|
1027 |
+
"round": "plus",
|
1028 |
+
"long": "plus",
|
1029 |
+
"aspirated": "minus",
|
1030 |
+
},
|
1031 |
+
"ɔ": {
|
1032 |
+
"place": "vowel",
|
1033 |
+
"manner": "vowel2",
|
1034 |
+
"syllabic": "plus",
|
1035 |
+
"voice": "plus",
|
1036 |
+
"nasal": "minus",
|
1037 |
+
"retroflex": "minus",
|
1038 |
+
"lateral": "minus",
|
1039 |
+
"high": "mid",
|
1040 |
+
"back": "back",
|
1041 |
+
"round": "plus",
|
1042 |
+
"long": "minus",
|
1043 |
+
"aspirated": "minus",
|
1044 |
+
},
|
1045 |
+
"ɒ": {
|
1046 |
+
"place": "vowel",
|
1047 |
+
"manner": "vowel2",
|
1048 |
+
"syllabic": "plus",
|
1049 |
+
"voice": "plus",
|
1050 |
+
"nasal": "minus",
|
1051 |
+
"retroflex": "minus",
|
1052 |
+
"lateral": "minus",
|
1053 |
+
"high": "low",
|
1054 |
+
"back": "back",
|
1055 |
+
"round": "minus",
|
1056 |
+
"long": "minus",
|
1057 |
+
"aspirated": "minus",
|
1058 |
+
},
|
1059 |
+
"I": {
|
1060 |
+
"place": "vowel",
|
1061 |
+
"manner": "vowel2",
|
1062 |
+
"syllabic": "plus",
|
1063 |
+
"voice": "plus",
|
1064 |
+
"nasal": "minus",
|
1065 |
+
"retroflex": "minus",
|
1066 |
+
"lateral": "minus",
|
1067 |
+
"high": "high",
|
1068 |
+
"back": "front",
|
1069 |
+
"round": "minus",
|
1070 |
+
"long": "plus",
|
1071 |
+
"aspirated": "minus",
|
1072 |
+
},
|
1073 |
+
}
|
1074 |
+
|
1075 |
+
# === Algorithm ===
|
1076 |
+
|
1077 |
+
|
1078 |
+
def align(str1, str2, epsilon=0):
|
1079 |
+
"""
|
1080 |
+
Compute the alignment of two phonetic strings.
|
1081 |
+
|
1082 |
+
:param str str1: First string to be aligned
|
1083 |
+
:param str str2: Second string to be aligned
|
1084 |
+
|
1085 |
+
:type epsilon: float (0.0 to 1.0)
|
1086 |
+
:param epsilon: Adjusts threshold similarity score for near-optimal alignments
|
1087 |
+
|
1088 |
+
:rtype: list(list(tuple(str, str)))
|
1089 |
+
:return: Alignment(s) of str1 and str2
|
1090 |
+
|
1091 |
+
(Kondrak 2002: 51)
|
1092 |
+
"""
|
1093 |
+
if np is None:
|
1094 |
+
raise ImportError("You need numpy in order to use the align function")
|
1095 |
+
|
1096 |
+
assert 0.0 <= epsilon <= 1.0, "Epsilon must be between 0.0 and 1.0."
|
1097 |
+
m = len(str1)
|
1098 |
+
n = len(str2)
|
1099 |
+
# This includes Kondrak's initialization of row 0 and column 0 to all 0s.
|
1100 |
+
S = np.zeros((m + 1, n + 1), dtype=float)
|
1101 |
+
|
1102 |
+
# If i <= 1 or j <= 1, don't allow expansions as it doesn't make sense,
|
1103 |
+
# and breaks array and string indices. Make sure they never get chosen
|
1104 |
+
# by setting them to -inf.
|
1105 |
+
for i in range(1, m + 1):
|
1106 |
+
for j in range(1, n + 1):
|
1107 |
+
edit1 = S[i - 1, j] + sigma_skip(str1[i - 1])
|
1108 |
+
edit2 = S[i, j - 1] + sigma_skip(str2[j - 1])
|
1109 |
+
edit3 = S[i - 1, j - 1] + sigma_sub(str1[i - 1], str2[j - 1])
|
1110 |
+
if i > 1:
|
1111 |
+
edit4 = S[i - 2, j - 1] + sigma_exp(str2[j - 1], str1[i - 2 : i])
|
1112 |
+
else:
|
1113 |
+
edit4 = -inf
|
1114 |
+
if j > 1:
|
1115 |
+
edit5 = S[i - 1, j - 2] + sigma_exp(str1[i - 1], str2[j - 2 : j])
|
1116 |
+
else:
|
1117 |
+
edit5 = -inf
|
1118 |
+
S[i, j] = max(edit1, edit2, edit3, edit4, edit5, 0)
|
1119 |
+
|
1120 |
+
T = (1 - epsilon) * np.amax(S) # Threshold score for near-optimal alignments
|
1121 |
+
|
1122 |
+
alignments = []
|
1123 |
+
for i in range(1, m + 1):
|
1124 |
+
for j in range(1, n + 1):
|
1125 |
+
if S[i, j] >= T:
|
1126 |
+
alignments.append(_retrieve(i, j, 0, S, T, str1, str2, []))
|
1127 |
+
return alignments
|
1128 |
+
|
1129 |
+
|
1130 |
+
def _retrieve(i, j, s, S, T, str1, str2, out):
|
1131 |
+
"""
|
1132 |
+
Retrieve the path through the similarity matrix S starting at (i, j).
|
1133 |
+
|
1134 |
+
:rtype: list(tuple(str, str))
|
1135 |
+
:return: Alignment of str1 and str2
|
1136 |
+
"""
|
1137 |
+
if S[i, j] == 0:
|
1138 |
+
return out
|
1139 |
+
else:
|
1140 |
+
if j > 1 and S[i - 1, j - 2] + sigma_exp(str1[i - 1], str2[j - 2 : j]) + s >= T:
|
1141 |
+
out.insert(0, (str1[i - 1], str2[j - 2 : j]))
|
1142 |
+
_retrieve(
|
1143 |
+
i - 1,
|
1144 |
+
j - 2,
|
1145 |
+
s + sigma_exp(str1[i - 1], str2[j - 2 : j]),
|
1146 |
+
S,
|
1147 |
+
T,
|
1148 |
+
str1,
|
1149 |
+
str2,
|
1150 |
+
out,
|
1151 |
+
)
|
1152 |
+
elif (
|
1153 |
+
i > 1 and S[i - 2, j - 1] + sigma_exp(str2[j - 1], str1[i - 2 : i]) + s >= T
|
1154 |
+
):
|
1155 |
+
out.insert(0, (str1[i - 2 : i], str2[j - 1]))
|
1156 |
+
_retrieve(
|
1157 |
+
i - 2,
|
1158 |
+
j - 1,
|
1159 |
+
s + sigma_exp(str2[j - 1], str1[i - 2 : i]),
|
1160 |
+
S,
|
1161 |
+
T,
|
1162 |
+
str1,
|
1163 |
+
str2,
|
1164 |
+
out,
|
1165 |
+
)
|
1166 |
+
elif S[i, j - 1] + sigma_skip(str2[j - 1]) + s >= T:
|
1167 |
+
out.insert(0, ("-", str2[j - 1]))
|
1168 |
+
_retrieve(i, j - 1, s + sigma_skip(str2[j - 1]), S, T, str1, str2, out)
|
1169 |
+
elif S[i - 1, j] + sigma_skip(str1[i - 1]) + s >= T:
|
1170 |
+
out.insert(0, (str1[i - 1], "-"))
|
1171 |
+
_retrieve(i - 1, j, s + sigma_skip(str1[i - 1]), S, T, str1, str2, out)
|
1172 |
+
elif S[i - 1, j - 1] + sigma_sub(str1[i - 1], str2[j - 1]) + s >= T:
|
1173 |
+
out.insert(0, (str1[i - 1], str2[j - 1]))
|
1174 |
+
_retrieve(
|
1175 |
+
i - 1,
|
1176 |
+
j - 1,
|
1177 |
+
s + sigma_sub(str1[i - 1], str2[j - 1]),
|
1178 |
+
S,
|
1179 |
+
T,
|
1180 |
+
str1,
|
1181 |
+
str2,
|
1182 |
+
out,
|
1183 |
+
)
|
1184 |
+
return out
|
1185 |
+
|
1186 |
+
|
1187 |
+
def sigma_skip(p):
|
1188 |
+
"""
|
1189 |
+
Returns score of an indel of P.
|
1190 |
+
|
1191 |
+
(Kondrak 2002: 54)
|
1192 |
+
"""
|
1193 |
+
return C_skip
|
1194 |
+
|
1195 |
+
|
1196 |
+
def sigma_sub(p, q):
|
1197 |
+
"""
|
1198 |
+
Returns score of a substitution of P with Q.
|
1199 |
+
|
1200 |
+
(Kondrak 2002: 54)
|
1201 |
+
"""
|
1202 |
+
return C_sub - delta(p, q) - V(p) - V(q)
|
1203 |
+
|
1204 |
+
|
1205 |
+
def sigma_exp(p, q):
|
1206 |
+
"""
|
1207 |
+
Returns score of an expansion/compression.
|
1208 |
+
|
1209 |
+
(Kondrak 2002: 54)
|
1210 |
+
"""
|
1211 |
+
q1 = q[0]
|
1212 |
+
q2 = q[1]
|
1213 |
+
return C_exp - delta(p, q1) - delta(p, q2) - V(p) - max(V(q1), V(q2))
|
1214 |
+
|
1215 |
+
|
1216 |
+
def delta(p, q):
|
1217 |
+
"""
|
1218 |
+
Return weighted sum of difference between P and Q.
|
1219 |
+
|
1220 |
+
(Kondrak 2002: 54)
|
1221 |
+
"""
|
1222 |
+
features = R(p, q)
|
1223 |
+
total = 0
|
1224 |
+
for f in features:
|
1225 |
+
total += diff(p, q, f) * salience[f]
|
1226 |
+
return total
|
1227 |
+
|
1228 |
+
|
1229 |
+
def diff(p, q, f):
|
1230 |
+
"""
|
1231 |
+
Returns difference between phonetic segments P and Q for feature F.
|
1232 |
+
|
1233 |
+
(Kondrak 2002: 52, 54)
|
1234 |
+
"""
|
1235 |
+
p_features, q_features = feature_matrix[p], feature_matrix[q]
|
1236 |
+
return abs(similarity_matrix[p_features[f]] - similarity_matrix[q_features[f]])
|
1237 |
+
|
1238 |
+
|
1239 |
+
def R(p, q):
|
1240 |
+
"""
|
1241 |
+
Return relevant features for segment comparison.
|
1242 |
+
|
1243 |
+
(Kondrak 2002: 54)
|
1244 |
+
"""
|
1245 |
+
if p in consonants or q in consonants:
|
1246 |
+
return R_c
|
1247 |
+
return R_v
|
1248 |
+
|
1249 |
+
|
1250 |
+
def V(p):
|
1251 |
+
"""
|
1252 |
+
Return vowel weight if P is vowel.
|
1253 |
+
|
1254 |
+
(Kondrak 2002: 54)
|
1255 |
+
"""
|
1256 |
+
if p in consonants:
|
1257 |
+
return 0
|
1258 |
+
return C_vwl
|
1259 |
+
|
1260 |
+
|
1261 |
+
# === Test ===
|
1262 |
+
|
1263 |
+
|
1264 |
+
def demo():
|
1265 |
+
"""
|
1266 |
+
A demonstration of the result of aligning phonetic sequences
|
1267 |
+
used in Kondrak's (2002) dissertation.
|
1268 |
+
"""
|
1269 |
+
data = [pair.split(",") for pair in cognate_data.split("\n")]
|
1270 |
+
for pair in data:
|
1271 |
+
alignment = align(pair[0], pair[1])[0]
|
1272 |
+
alignment = [f"({a[0]}, {a[1]})" for a in alignment]
|
1273 |
+
alignment = " ".join(alignment)
|
1274 |
+
print(f"{pair[0]} ~ {pair[1]} : {alignment}")
|
1275 |
+
|
1276 |
+
|
1277 |
+
cognate_data = """jo,ʒə
|
1278 |
+
tu,ty
|
1279 |
+
nosotros,nu
|
1280 |
+
kjen,ki
|
1281 |
+
ke,kwa
|
1282 |
+
todos,tu
|
1283 |
+
una,ən
|
1284 |
+
dos,dø
|
1285 |
+
tres,trwa
|
1286 |
+
ombre,om
|
1287 |
+
arbol,arbrə
|
1288 |
+
pluma,plym
|
1289 |
+
kabeθa,kap
|
1290 |
+
boka,buʃ
|
1291 |
+
pje,pje
|
1292 |
+
koraθon,kœr
|
1293 |
+
ber,vwar
|
1294 |
+
benir,vənir
|
1295 |
+
deθir,dir
|
1296 |
+
pobre,povrə
|
1297 |
+
ðis,dIzes
|
1298 |
+
ðæt,das
|
1299 |
+
wat,vas
|
1300 |
+
nat,nixt
|
1301 |
+
loŋ,laŋ
|
1302 |
+
mæn,man
|
1303 |
+
fleʃ,flajʃ
|
1304 |
+
bləd,blyt
|
1305 |
+
feðər,fEdər
|
1306 |
+
hær,hAr
|
1307 |
+
ir,Or
|
1308 |
+
aj,awgə
|
1309 |
+
nowz,nAzə
|
1310 |
+
mawθ,munt
|
1311 |
+
təŋ,tsuŋə
|
1312 |
+
fut,fys
|
1313 |
+
nij,knI
|
1314 |
+
hænd,hant
|
1315 |
+
hart,herts
|
1316 |
+
livər,lEbər
|
1317 |
+
ænd,ante
|
1318 |
+
æt,ad
|
1319 |
+
blow,flAre
|
1320 |
+
ir,awris
|
1321 |
+
ijt,edere
|
1322 |
+
fiʃ,piʃkis
|
1323 |
+
flow,fluere
|
1324 |
+
staɾ,stella
|
1325 |
+
ful,plenus
|
1326 |
+
græs,gramen
|
1327 |
+
hart,kordis
|
1328 |
+
horn,korny
|
1329 |
+
aj,ego
|
1330 |
+
nij,genU
|
1331 |
+
məðər,mAter
|
1332 |
+
mawntən,mons
|
1333 |
+
nejm,nomen
|
1334 |
+
njuw,nowus
|
1335 |
+
wən,unus
|
1336 |
+
rawnd,rotundus
|
1337 |
+
sow,suere
|
1338 |
+
sit,sedere
|
1339 |
+
θrij,tres
|
1340 |
+
tuwθ,dentis
|
1341 |
+
θin,tenwis
|
1342 |
+
kinwawa,kenuaʔ
|
1343 |
+
nina,nenah
|
1344 |
+
napewa,napɛw
|
1345 |
+
wapimini,wapemen
|
1346 |
+
namesa,namɛʔs
|
1347 |
+
okimawa,okemaw
|
1348 |
+
ʃiʃipa,seʔsep
|
1349 |
+
ahkohkwa,ahkɛh
|
1350 |
+
pematesiweni,pematesewen
|
1351 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ures
|
2 |
+
#
|
3 |
+
# Copyright (C) 2001-2023 NLTK Project
|
4 |
+
# Author: Joel Nothman <[email protected]>
|
5 |
+
# URL: <https://www.nltk.org/>
|
6 |
+
# For license information, see LICENSE.TXT
|
7 |
+
|
8 |
+
"""
|
9 |
+
Provides scoring functions for a number of association measures through a
|
10 |
+
generic, abstract implementation in ``NgramAssocMeasures``, and n-specific
|
11 |
+
``BigramAssocMeasures`` and ``TrigramAssocMeasures``.
|
12 |
+
"""
|
13 |
+
|
14 |
+
import math as _math
|
15 |
+
from abc import ABCMeta, abstractmethod
|
16 |
+
from functools import reduce
|
17 |
+
|
18 |
+
_log2 = lambda x: _math.log2(x)
|
19 |
+
_ln = _math.log
|
20 |
+
|
21 |
+
_product = lambda s: reduce(lambda x, y: x * y, s)
|
22 |
+
|
23 |
+
_SMALL = 1e-20
|
24 |
+
|
25 |
+
try:
|
26 |
+
from scipy.stats import fisher_exact
|
27 |
+
except ImportError:
|
28 |
+
|
29 |
+
def fisher_exact(*_args, **_kwargs):
|
30 |
+
raise NotImplementedError
|
31 |
+
|
32 |
+
|
33 |
+
### Indices to marginals arguments:
|
34 |
+
|
35 |
+
NGRAM = 0
|
36 |
+
"""Marginals index for the ngram count"""
|
37 |
+
|
38 |
+
UNIGRAMS = -2
|
39 |
+
"""Marginals index for a tuple of each unigram count"""
|
40 |
+
|
41 |
+
TOTAL = -1
|
42 |
+
"""Marginals index for the number of words in the data"""
|
43 |
+
|
44 |
+
|
45 |
+
class NgramAssocMeasures(metaclass=ABCMeta):
|
46 |
+
"""
|
47 |
+
An abstract class defining a collection of generic association measures.
|
48 |
+
Each public method returns a score, taking the following arguments::
|
49 |
+
|
50 |
+
score_fn(count_of_ngram,
|
51 |
+
(count_of_n-1gram_1, ..., count_of_n-1gram_j),
|
52 |
+
(count_of_n-2gram_1, ..., count_of_n-2gram_k),
|
53 |
+
...,
|
54 |
+
(count_of_1gram_1, ..., count_of_1gram_n),
|
55 |
+
count_of_total_words)
|
56 |
+
|
57 |
+
See ``BigramAssocMeasures`` and ``TrigramAssocMeasures``
|
58 |
+
|
59 |
+
Inheriting classes should define a property _n, and a method _contingency
|
60 |
+
which calculates contingency values from marginals in order for all
|
61 |
+
association measures defined here to be usable.
|
62 |
+
"""
|
63 |
+
|
64 |
+
_n = 0
|
65 |
+
|
66 |
+
@staticmethod
|
67 |
+
@abstractmethod
|
68 |
+
def _contingency(*marginals):
|
69 |
+
"""Calculates values of a contingency table from marginal values."""
|
70 |
+
raise NotImplementedError(
|
71 |
+
"The contingency table is not available" "in the general ngram case"
|
72 |
+
)
|
73 |
+
|
74 |
+
@staticmethod
|
75 |
+
@abstractmethod
|
76 |
+
def _marginals(*contingency):
|
77 |
+
"""Calculates values of contingency table marginals from its values."""
|
78 |
+
raise NotImplementedError(
|
79 |
+
"The contingency table is not available" "in the general ngram case"
|
80 |
+
)
|
81 |
+
|
82 |
+
@classmethod
|
83 |
+
def _expected_values(cls, cont):
|
84 |
+
"""Calculates expected values for a contingency table."""
|
85 |
+
n_all = sum(cont)
|
86 |
+
bits = [1 << i for i in range(cls._n)]
|
87 |
+
|
88 |
+
# For each contingency table cell
|
89 |
+
for i in range(len(cont)):
|
90 |
+
# Yield the expected value
|
91 |
+
yield (
|
92 |
+
_product(
|
93 |
+
sum(cont[x] for x in range(2**cls._n) if (x & j) == (i & j))
|
94 |
+
for j in bits
|
95 |
+
)
|
96 |
+
/ (n_all ** (cls._n - 1))
|
97 |
+
)
|
98 |
+
|
99 |
+
@staticmethod
|
100 |
+
def raw_freq(*marginals):
|
101 |
+
"""Scores ngrams by their frequency"""
|
102 |
+
return marginals[NGRAM] / marginals[TOTAL]
|
103 |
+
|
104 |
+
@classmethod
|
105 |
+
def student_t(cls, *marginals):
|
106 |
+
"""Scores ngrams using Student's t test with independence hypothesis
|
107 |
+
for unigrams, as in Manning and Schutze 5.3.1.
|
108 |
+
"""
|
109 |
+
return (
|
110 |
+
marginals[NGRAM]
|
111 |
+
- _product(marginals[UNIGRAMS]) / (marginals[TOTAL] ** (cls._n - 1))
|
112 |
+
) / (marginals[NGRAM] + _SMALL) ** 0.5
|
113 |
+
|
114 |
+
@classmethod
|
115 |
+
def chi_sq(cls, *marginals):
|
116 |
+
"""Scores ngrams using Pearson's chi-square as in Manning and Schutze
|
117 |
+
5.3.3.
|
118 |
+
"""
|
119 |
+
cont = cls._contingency(*marginals)
|
120 |
+
exps = cls._expected_values(cont)
|
121 |
+
return sum((obs - exp) ** 2 / (exp + _SMALL) for obs, exp in zip(cont, exps))
|
122 |
+
|
123 |
+
@staticmethod
|
124 |
+
def mi_like(*marginals, **kwargs):
|
125 |
+
"""Scores ngrams using a variant of mutual information. The keyword
|
126 |
+
argument power sets an exponent (default 3) for the numerator. No
|
127 |
+
logarithm of the result is calculated.
|
128 |
+
"""
|
129 |
+
return marginals[NGRAM] ** kwargs.get("power", 3) / _product(
|
130 |
+
marginals[UNIGRAMS]
|
131 |
+
)
|
132 |
+
|
133 |
+
@classmethod
|
134 |
+
def pmi(cls, *marginals):
|
135 |
+
"""Scores ngrams by pointwise mutual information, as in Manning and
|
136 |
+
Schutze 5.4.
|
137 |
+
"""
|
138 |
+
return _log2(marginals[NGRAM] * marginals[TOTAL] ** (cls._n - 1)) - _log2(
|
139 |
+
_product(marginals[UNIGRAMS])
|
140 |
+
)
|
141 |
+
|
142 |
+
@classmethod
|
143 |
+
def likelihood_ratio(cls, *marginals):
|
144 |
+
"""Scores ngrams using likelihood ratios as in Manning and Schutze 5.3.4."""
|
145 |
+
cont = cls._contingency(*marginals)
|
146 |
+
return 2 * sum(
|
147 |
+
obs * _ln(obs / (exp + _SMALL) + _SMALL)
|
148 |
+
for obs, exp in zip(cont, cls._expected_values(cont))
|
149 |
+
)
|
150 |
+
|
151 |
+
@classmethod
|
152 |
+
def poisson_stirling(cls, *marginals):
|
153 |
+
"""Scores ngrams using the Poisson-Stirling measure."""
|
154 |
+
exp = _product(marginals[UNIGRAMS]) / (marginals[TOTAL] ** (cls._n - 1))
|
155 |
+
return marginals[NGRAM] * (_log2(marginals[NGRAM] / exp) - 1)
|
156 |
+
|
157 |
+
@classmethod
|
158 |
+
def jaccard(cls, *marginals):
|
159 |
+
"""Scores ngrams using the Jaccard index."""
|
160 |
+
cont = cls._contingency(*marginals)
|
161 |
+
return cont[0] / sum(cont[:-1])
|
162 |
+
|
163 |
+
|
164 |
+
class BigramAssocMeasures(NgramAssocMeasures):
|
165 |
+
"""
|
166 |
+
A collection of bigram association measures. Each association measure
|
167 |
+
is provided as a function with three arguments::
|
168 |
+
|
169 |
+
bigram_score_fn(n_ii, (n_ix, n_xi), n_xx)
|
170 |
+
|
171 |
+
The arguments constitute the marginals of a contingency table, counting
|
172 |
+
the occurrences of particular events in a corpus. The letter i in the
|
173 |
+
suffix refers to the appearance of the word in question, while x indicates
|
174 |
+
the appearance of any word. Thus, for example:
|
175 |
+
|
176 |
+
- n_ii counts ``(w1, w2)``, i.e. the bigram being scored
|
177 |
+
- n_ix counts ``(w1, *)``
|
178 |
+
- n_xi counts ``(*, w2)``
|
179 |
+
- n_xx counts ``(*, *)``, i.e. any bigram
|
180 |
+
|
181 |
+
This may be shown with respect to a contingency table::
|
182 |
+
|
183 |
+
w1 ~w1
|
184 |
+
------ ------
|
185 |
+
w2 | n_ii | n_oi | = n_xi
|
186 |
+
------ ------
|
187 |
+
~w2 | n_io | n_oo |
|
188 |
+
------ ------
|
189 |
+
= n_ix TOTAL = n_xx
|
190 |
+
"""
|
191 |
+
|
192 |
+
_n = 2
|
193 |
+
|
194 |
+
@staticmethod
|
195 |
+
def _contingency(n_ii, n_ix_xi_tuple, n_xx):
|
196 |
+
"""Calculates values of a bigram contingency table from marginal values."""
|
197 |
+
(n_ix, n_xi) = n_ix_xi_tuple
|
198 |
+
n_oi = n_xi - n_ii
|
199 |
+
n_io = n_ix - n_ii
|
200 |
+
return (n_ii, n_oi, n_io, n_xx - n_ii - n_oi - n_io)
|
201 |
+
|
202 |
+
@staticmethod
|
203 |
+
def _marginals(n_ii, n_oi, n_io, n_oo):
|
204 |
+
"""Calculates values of contingency table marginals from its values."""
|
205 |
+
return (n_ii, (n_oi + n_ii, n_io + n_ii), n_oo + n_oi + n_io + n_ii)
|
206 |
+
|
207 |
+
@staticmethod
|
208 |
+
def _expected_values(cont):
|
209 |
+
"""Calculates expected values for a contingency table."""
|
210 |
+
n_xx = sum(cont)
|
211 |
+
# For each contingency table cell
|
212 |
+
for i in range(4):
|
213 |
+
yield (cont[i] + cont[i ^ 1]) * (cont[i] + cont[i ^ 2]) / n_xx
|
214 |
+
|
215 |
+
@classmethod
|
216 |
+
def phi_sq(cls, *marginals):
|
217 |
+
"""Scores bigrams using phi-square, the square of the Pearson correlation
|
218 |
+
coefficient.
|
219 |
+
"""
|
220 |
+
n_ii, n_io, n_oi, n_oo = cls._contingency(*marginals)
|
221 |
+
|
222 |
+
return (n_ii * n_oo - n_io * n_oi) ** 2 / (
|
223 |
+
(n_ii + n_io) * (n_ii + n_oi) * (n_io + n_oo) * (n_oi + n_oo)
|
224 |
+
)
|
225 |
+
|
226 |
+
@classmethod
|
227 |
+
def chi_sq(cls, n_ii, n_ix_xi_tuple, n_xx):
|
228 |
+
"""Scores bigrams using chi-square, i.e. phi-sq multiplied by the number
|
229 |
+
of bigrams, as in Manning and Schutze 5.3.3.
|
230 |
+
"""
|
231 |
+
(n_ix, n_xi) = n_ix_xi_tuple
|
232 |
+
return n_xx * cls.phi_sq(n_ii, (n_ix, n_xi), n_xx)
|
233 |
+
|
234 |
+
@classmethod
|
235 |
+
def fisher(cls, *marginals):
|
236 |
+
"""Scores bigrams using Fisher's Exact Test (Pedersen 1996). Less
|
237 |
+
sensitive to small counts than PMI or Chi Sq, but also more expensive
|
238 |
+
to compute. Requires scipy.
|
239 |
+
"""
|
240 |
+
|
241 |
+
n_ii, n_io, n_oi, n_oo = cls._contingency(*marginals)
|
242 |
+
|
243 |
+
(odds, pvalue) = fisher_exact([[n_ii, n_io], [n_oi, n_oo]], alternative="less")
|
244 |
+
return pvalue
|
245 |
+
|
246 |
+
@staticmethod
|
247 |
+
def dice(n_ii, n_ix_xi_tuple, n_xx):
|
248 |
+
"""Scores bigrams using Dice's coefficient."""
|
249 |
+
(n_ix, n_xi) = n_ix_xi_tuple
|
250 |
+
return 2 * n_ii / (n_ix + n_xi)
|
251 |
+
|
252 |
+
|
253 |
+
class TrigramAssocMeasures(NgramAssocMeasures):
|
254 |
+
"""
|
255 |
+
A collection of trigram association measures. Each association measure
|
256 |
+
is provided as a function with four arguments::
|
257 |
+
|
258 |
+
trigram_score_fn(n_iii,
|
259 |
+
(n_iix, n_ixi, n_xii),
|
260 |
+
(n_ixx, n_xix, n_xxi),
|
261 |
+
n_xxx)
|
262 |
+
|
263 |
+
The arguments constitute the marginals of a contingency table, counting
|
264 |
+
the occurrences of particular events in a corpus. The letter i in the
|
265 |
+
suffix refers to the appearance of the word in question, while x indicates
|
266 |
+
the appearance of any word. Thus, for example:
|
267 |
+
|
268 |
+
- n_iii counts ``(w1, w2, w3)``, i.e. the trigram being scored
|
269 |
+
- n_ixx counts ``(w1, *, *)``
|
270 |
+
- n_xxx counts ``(*, *, *)``, i.e. any trigram
|
271 |
+
"""
|
272 |
+
|
273 |
+
_n = 3
|
274 |
+
|
275 |
+
@staticmethod
|
276 |
+
def _contingency(n_iii, n_iix_tuple, n_ixx_tuple, n_xxx):
|
277 |
+
"""Calculates values of a trigram contingency table (or cube) from
|
278 |
+
marginal values.
|
279 |
+
>>> TrigramAssocMeasures._contingency(1, (1, 1, 1), (1, 73, 1), 2000)
|
280 |
+
(1, 0, 0, 0, 0, 72, 0, 1927)
|
281 |
+
"""
|
282 |
+
(n_iix, n_ixi, n_xii) = n_iix_tuple
|
283 |
+
(n_ixx, n_xix, n_xxi) = n_ixx_tuple
|
284 |
+
n_oii = n_xii - n_iii
|
285 |
+
n_ioi = n_ixi - n_iii
|
286 |
+
n_iio = n_iix - n_iii
|
287 |
+
n_ooi = n_xxi - n_iii - n_oii - n_ioi
|
288 |
+
n_oio = n_xix - n_iii - n_oii - n_iio
|
289 |
+
n_ioo = n_ixx - n_iii - n_ioi - n_iio
|
290 |
+
n_ooo = n_xxx - n_iii - n_oii - n_ioi - n_iio - n_ooi - n_oio - n_ioo
|
291 |
+
|
292 |
+
return (n_iii, n_oii, n_ioi, n_ooi, n_iio, n_oio, n_ioo, n_ooo)
|
293 |
+
|
294 |
+
@staticmethod
|
295 |
+
def _marginals(*contingency):
|
296 |
+
"""Calculates values of contingency table marginals from its values.
|
297 |
+
>>> TrigramAssocMeasures._marginals(1, 0, 0, 0, 0, 72, 0, 1927)
|
298 |
+
(1, (1, 1, 1), (1, 73, 1), 2000)
|
299 |
+
"""
|
300 |
+
n_iii, n_oii, n_ioi, n_ooi, n_iio, n_oio, n_ioo, n_ooo = contingency
|
301 |
+
return (
|
302 |
+
n_iii,
|
303 |
+
(n_iii + n_iio, n_iii + n_ioi, n_iii + n_oii),
|
304 |
+
(
|
305 |
+
n_iii + n_ioi + n_iio + n_ioo,
|
306 |
+
n_iii + n_oii + n_iio + n_oio,
|
307 |
+
n_iii + n_oii + n_ioi + n_ooi,
|
308 |
+
),
|
309 |
+
sum(contingency),
|
310 |
+
)
|
311 |
+
|
312 |
+
|
313 |
+
class QuadgramAssocMeasures(NgramAssocMeasures):
|
314 |
+
"""
|
315 |
+
A collection of quadgram association measures. Each association measure
|
316 |
+
is provided as a function with five arguments::
|
317 |
+
|
318 |
+
trigram_score_fn(n_iiii,
|
319 |
+
(n_iiix, n_iixi, n_ixii, n_xiii),
|
320 |
+
(n_iixx, n_ixix, n_ixxi, n_xixi, n_xxii, n_xiix),
|
321 |
+
(n_ixxx, n_xixx, n_xxix, n_xxxi),
|
322 |
+
n_all)
|
323 |
+
|
324 |
+
The arguments constitute the marginals of a contingency table, counting
|
325 |
+
the occurrences of particular events in a corpus. The letter i in the
|
326 |
+
suffix refers to the appearance of the word in question, while x indicates
|
327 |
+
the appearance of any word. Thus, for example:
|
328 |
+
|
329 |
+
- n_iiii counts ``(w1, w2, w3, w4)``, i.e. the quadgram being scored
|
330 |
+
- n_ixxi counts ``(w1, *, *, w4)``
|
331 |
+
- n_xxxx counts ``(*, *, *, *)``, i.e. any quadgram
|
332 |
+
"""
|
333 |
+
|
334 |
+
_n = 4
|
335 |
+
|
336 |
+
@staticmethod
|
337 |
+
def _contingency(n_iiii, n_iiix_tuple, n_iixx_tuple, n_ixxx_tuple, n_xxxx):
|
338 |
+
"""Calculates values of a quadgram contingency table from
|
339 |
+
marginal values.
|
340 |
+
"""
|
341 |
+
(n_iiix, n_iixi, n_ixii, n_xiii) = n_iiix_tuple
|
342 |
+
(n_iixx, n_ixix, n_ixxi, n_xixi, n_xxii, n_xiix) = n_iixx_tuple
|
343 |
+
(n_ixxx, n_xixx, n_xxix, n_xxxi) = n_ixxx_tuple
|
344 |
+
n_oiii = n_xiii - n_iiii
|
345 |
+
n_ioii = n_ixii - n_iiii
|
346 |
+
n_iioi = n_iixi - n_iiii
|
347 |
+
n_ooii = n_xxii - n_iiii - n_oiii - n_ioii
|
348 |
+
n_oioi = n_xixi - n_iiii - n_oiii - n_iioi
|
349 |
+
n_iooi = n_ixxi - n_iiii - n_ioii - n_iioi
|
350 |
+
n_oooi = n_xxxi - n_iiii - n_oiii - n_ioii - n_iioi - n_ooii - n_iooi - n_oioi
|
351 |
+
n_iiio = n_iiix - n_iiii
|
352 |
+
n_oiio = n_xiix - n_iiii - n_oiii - n_iiio
|
353 |
+
n_ioio = n_ixix - n_iiii - n_ioii - n_iiio
|
354 |
+
n_ooio = n_xxix - n_iiii - n_oiii - n_ioii - n_iiio - n_ooii - n_ioio - n_oiio
|
355 |
+
n_iioo = n_iixx - n_iiii - n_iioi - n_iiio
|
356 |
+
n_oioo = n_xixx - n_iiii - n_oiii - n_iioi - n_iiio - n_oioi - n_oiio - n_iioo
|
357 |
+
n_iooo = n_ixxx - n_iiii - n_ioii - n_iioi - n_iiio - n_iooi - n_iioo - n_ioio
|
358 |
+
n_oooo = (
|
359 |
+
n_xxxx
|
360 |
+
- n_iiii
|
361 |
+
- n_oiii
|
362 |
+
- n_ioii
|
363 |
+
- n_iioi
|
364 |
+
- n_ooii
|
365 |
+
- n_oioi
|
366 |
+
- n_iooi
|
367 |
+
- n_oooi
|
368 |
+
- n_iiio
|
369 |
+
- n_oiio
|
370 |
+
- n_ioio
|
371 |
+
- n_ooio
|
372 |
+
- n_iioo
|
373 |
+
- n_oioo
|
374 |
+
- n_iooo
|
375 |
+
)
|
376 |
+
|
377 |
+
return (
|
378 |
+
n_iiii,
|
379 |
+
n_oiii,
|
380 |
+
n_ioii,
|
381 |
+
n_ooii,
|
382 |
+
n_iioi,
|
383 |
+
n_oioi,
|
384 |
+
n_iooi,
|
385 |
+
n_oooi,
|
386 |
+
n_iiio,
|
387 |
+
n_oiio,
|
388 |
+
n_ioio,
|
389 |
+
n_ooio,
|
390 |
+
n_iioo,
|
391 |
+
n_oioo,
|
392 |
+
n_iooo,
|
393 |
+
n_oooo,
|
394 |
+
)
|
395 |
+
|
396 |
+
@staticmethod
|
397 |
+
def _marginals(*contingency):
|
398 |
+
"""Calculates values of contingency table marginals from its values.
|
399 |
+
QuadgramAssocMeasures._marginals(1, 0, 2, 46, 552, 825, 2577, 34967, 1, 0, 2, 48, 7250, 9031, 28585, 356653)
|
400 |
+
(1, (2, 553, 3, 1), (7804, 6, 3132, 1378, 49, 2), (38970, 17660, 100, 38970), 440540)
|
401 |
+
"""
|
402 |
+
(
|
403 |
+
n_iiii,
|
404 |
+
n_oiii,
|
405 |
+
n_ioii,
|
406 |
+
n_ooii,
|
407 |
+
n_iioi,
|
408 |
+
n_oioi,
|
409 |
+
n_iooi,
|
410 |
+
n_oooi,
|
411 |
+
n_iiio,
|
412 |
+
n_oiio,
|
413 |
+
n_ioio,
|
414 |
+
n_ooio,
|
415 |
+
n_iioo,
|
416 |
+
n_oioo,
|
417 |
+
n_iooo,
|
418 |
+
n_oooo,
|
419 |
+
) = contingency
|
420 |
+
|
421 |
+
n_iiix = n_iiii + n_iiio
|
422 |
+
n_iixi = n_iiii + n_iioi
|
423 |
+
n_ixii = n_iiii + n_ioii
|
424 |
+
n_xiii = n_iiii + n_oiii
|
425 |
+
|
426 |
+
n_iixx = n_iiii + n_iioi + n_iiio + n_iioo
|
427 |
+
n_ixix = n_iiii + n_ioii + n_iiio + n_ioio
|
428 |
+
n_ixxi = n_iiii + n_ioii + n_iioi + n_iooi
|
429 |
+
n_xixi = n_iiii + n_oiii + n_iioi + n_oioi
|
430 |
+
n_xxii = n_iiii + n_oiii + n_ioii + n_ooii
|
431 |
+
n_xiix = n_iiii + n_oiii + n_iiio + n_oiio
|
432 |
+
|
433 |
+
n_ixxx = n_iiii + n_ioii + n_iioi + n_iiio + n_iooi + n_iioo + n_ioio + n_iooo
|
434 |
+
n_xixx = n_iiii + n_oiii + n_iioi + n_iiio + n_oioi + n_oiio + n_iioo + n_oioo
|
435 |
+
n_xxix = n_iiii + n_oiii + n_ioii + n_iiio + n_ooii + n_ioio + n_oiio + n_ooio
|
436 |
+
n_xxxi = n_iiii + n_oiii + n_ioii + n_iioi + n_ooii + n_iooi + n_oioi + n_oooi
|
437 |
+
|
438 |
+
n_all = sum(contingency)
|
439 |
+
|
440 |
+
return (
|
441 |
+
n_iiii,
|
442 |
+
(n_iiix, n_iixi, n_ixii, n_xiii),
|
443 |
+
(n_iixx, n_ixix, n_ixxi, n_xixi, n_xxii, n_xiix),
|
444 |
+
(n_ixxx, n_xixx, n_xxix, n_xxxi),
|
445 |
+
n_all,
|
446 |
+
)
|
447 |
+
|
448 |
+
|
449 |
+
class ContingencyMeasures:
|
450 |
+
"""Wraps NgramAssocMeasures classes such that the arguments of association
|
451 |
+
measures are contingency table values rather than marginals.
|
452 |
+
"""
|
453 |
+
|
454 |
+
def __init__(self, measures):
|
455 |
+
"""Constructs a ContingencyMeasures given a NgramAssocMeasures class"""
|
456 |
+
self.__class__.__name__ = "Contingency" + measures.__class__.__name__
|
457 |
+
for k in dir(measures):
|
458 |
+
if k.startswith("__"):
|
459 |
+
continue
|
460 |
+
v = getattr(measures, k)
|
461 |
+
if not k.startswith("_"):
|
462 |
+
v = self._make_contingency_fn(measures, v)
|
463 |
+
setattr(self, k, v)
|
464 |
+
|
465 |
+
@staticmethod
|
466 |
+
def _make_contingency_fn(measures, old_fn):
|
467 |
+
"""From an association measure function, produces a new function which
|
468 |
+
accepts contingency table values as its arguments.
|
469 |
+
"""
|
470 |
+
|
471 |
+
def res(*contingency):
|
472 |
+
return old_fn(*measures._marginals(*contingency))
|
473 |
+
|
474 |
+
res.__doc__ = old_fn.__doc__
|
475 |
+
res.__name__ = old_fn.__name__
|
476 |
+
return res
|