2017-04-15 13:05:47 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-04-15 13:13:34 +03:00
|
|
|
import ujson
|
2015-08-24 06:25:55 +03:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
from cymem.cymem cimport Pool
|
2017-03-21 23:08:54 +03:00
|
|
|
from thinc.typedefs cimport atom_t
|
2016-01-29 05:58:55 +03:00
|
|
|
from thinc.extra.eg cimport Example
|
|
|
|
from thinc.structs cimport ExampleC
|
|
|
|
from thinc.linear.avgtron cimport AveragedPerceptron
|
|
|
|
from thinc.linalg cimport VecVec
|
2015-08-24 06:25:55 +03:00
|
|
|
|
|
|
|
from .tokens.doc cimport Doc
|
|
|
|
from .attrs cimport TAG
|
2016-10-17 01:55:15 +03:00
|
|
|
from .gold cimport GoldParse
|
2015-08-26 20:19:21 +03:00
|
|
|
from .attrs cimport *
|
2017-04-15 13:11:16 +03:00
|
|
|
from . import util
|
2015-08-26 20:19:21 +03:00
|
|
|
|
2017-02-27 00:27:11 +03:00
|
|
|
|
2015-08-26 20:19:21 +03:00
|
|
|
cpdef enum:
|
|
|
|
P2_orth
|
|
|
|
P2_cluster
|
|
|
|
P2_shape
|
|
|
|
P2_prefix
|
|
|
|
P2_suffix
|
|
|
|
P2_pos
|
|
|
|
P2_lemma
|
|
|
|
P2_flags
|
|
|
|
|
|
|
|
P1_orth
|
|
|
|
P1_cluster
|
|
|
|
P1_shape
|
|
|
|
P1_prefix
|
|
|
|
P1_suffix
|
|
|
|
P1_pos
|
|
|
|
P1_lemma
|
|
|
|
P1_flags
|
|
|
|
|
|
|
|
W_orth
|
|
|
|
W_cluster
|
|
|
|
W_shape
|
|
|
|
W_prefix
|
|
|
|
W_suffix
|
|
|
|
W_pos
|
|
|
|
W_lemma
|
|
|
|
W_flags
|
|
|
|
|
|
|
|
N1_orth
|
|
|
|
N1_cluster
|
|
|
|
N1_shape
|
|
|
|
N1_prefix
|
|
|
|
N1_suffix
|
|
|
|
N1_pos
|
|
|
|
N1_lemma
|
|
|
|
N1_flags
|
|
|
|
|
|
|
|
N2_orth
|
|
|
|
N2_cluster
|
|
|
|
N2_shape
|
|
|
|
N2_prefix
|
|
|
|
N2_suffix
|
|
|
|
N2_pos
|
|
|
|
N2_lemma
|
|
|
|
N2_flags
|
|
|
|
|
|
|
|
N_CONTEXT_FIELDS
|
2015-08-24 06:25:55 +03:00
|
|
|
|
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
cdef class TaggerModel(AveragedPerceptron):
|
2017-03-11 15:59:13 +03:00
|
|
|
def update(self, Example eg):
|
|
|
|
self.time += 1
|
|
|
|
guess = eg.guess
|
|
|
|
best = VecVec.arg_max_if_zero(eg.c.scores, eg.c.costs, eg.c.nr_class)
|
|
|
|
if guess != best:
|
|
|
|
for feat in eg.c.features[:eg.c.nr_feat]:
|
2017-03-16 19:58:20 +03:00
|
|
|
self.update_weight(feat.key, best, -feat.value)
|
|
|
|
self.update_weight(feat.key, guess, feat.value)
|
2017-02-27 00:27:11 +03:00
|
|
|
|
2017-03-11 15:59:13 +03:00
|
|
|
cdef void set_featuresC(self, ExampleC* eg, const TokenC* tokens, int i) except *:
|
2015-11-06 19:24:30 +03:00
|
|
|
_fill_from_token(&eg.atoms[P2_orth], &tokens[i-2])
|
|
|
|
_fill_from_token(&eg.atoms[P1_orth], &tokens[i-1])
|
|
|
|
_fill_from_token(&eg.atoms[W_orth], &tokens[i])
|
|
|
|
_fill_from_token(&eg.atoms[N1_orth], &tokens[i+1])
|
|
|
|
_fill_from_token(&eg.atoms[N2_orth], &tokens[i+2])
|
|
|
|
|
|
|
|
eg.nr_feat = self.extracter.set_features(eg.features, eg.atoms)
|
|
|
|
|
|
|
|
|
|
|
|
cdef inline void _fill_from_token(atom_t* context, const TokenC* t) nogil:
|
|
|
|
context[0] = t.lex.lower
|
|
|
|
context[1] = t.lex.cluster
|
|
|
|
context[2] = t.lex.shape
|
|
|
|
context[3] = t.lex.prefix
|
|
|
|
context[4] = t.lex.suffix
|
|
|
|
context[5] = t.tag
|
|
|
|
context[6] = t.lemma
|
|
|
|
if t.lex.flags & (1 << IS_ALPHA):
|
|
|
|
context[7] = 1
|
|
|
|
elif t.lex.flags & (1 << IS_PUNCT):
|
|
|
|
context[7] = 2
|
|
|
|
elif t.lex.flags & (1 << LIKE_URL):
|
|
|
|
context[7] = 3
|
|
|
|
elif t.lex.flags & (1 << LIKE_NUM):
|
|
|
|
context[7] = 4
|
|
|
|
else:
|
|
|
|
context[7] = 0
|
|
|
|
|
|
|
|
|
2015-08-24 06:25:55 +03:00
|
|
|
cdef class Tagger:
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Annotate part-of-speech tags on Doc objects.
|
|
|
|
"""
|
2015-08-26 20:19:21 +03:00
|
|
|
@classmethod
|
2016-10-16 22:34:57 +03:00
|
|
|
def load(cls, path, vocab, require=False):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Load the statistical model from the supplied path.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
path (Path):
|
|
|
|
The path to load from.
|
|
|
|
vocab (Vocab):
|
|
|
|
The vocabulary. Must be shared by the documents to be processed.
|
|
|
|
require (bool):
|
|
|
|
Whether to raise an error if the files are not found.
|
|
|
|
Returns (Tagger):
|
|
|
|
The newly created object.
|
|
|
|
"""
|
2016-10-16 22:34:57 +03:00
|
|
|
# TODO: Change this to expect config.json when we don't have to
|
|
|
|
# support old data.
|
2017-04-15 13:11:16 +03:00
|
|
|
path = util.ensure_path(path)
|
2016-09-24 21:26:17 +03:00
|
|
|
if (path / 'templates.json').exists():
|
2016-10-21 02:44:10 +03:00
|
|
|
with (path / 'templates.json').open('r', encoding='utf8') as file_:
|
2017-04-15 13:13:34 +03:00
|
|
|
templates = ujson.load(file_)
|
2016-10-16 22:34:57 +03:00
|
|
|
elif require:
|
|
|
|
raise IOError(
|
|
|
|
"Required file %s/templates.json not found when loading Tagger" % str(path))
|
2016-09-24 16:42:01 +03:00
|
|
|
else:
|
2016-10-16 22:34:57 +03:00
|
|
|
templates = cls.feature_templates
|
|
|
|
self = cls(vocab, model=None, feature_templates=templates)
|
2015-12-07 08:01:28 +03:00
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
if (path / 'model').exists():
|
2016-10-16 22:34:57 +03:00
|
|
|
self.model.load(str(path / 'model'))
|
|
|
|
elif require:
|
|
|
|
raise IOError(
|
|
|
|
"Required file %s/model not found when loading Tagger" % str(path))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __init__(self, Vocab vocab, TaggerModel model=None, **cfg):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Create a Tagger.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
vocab (Vocab):
|
|
|
|
The vocabulary object. Must be shared with documents to be processed.
|
|
|
|
model (thinc.linear.AveragedPerceptron):
|
|
|
|
The statistical model.
|
|
|
|
Returns (Tagger):
|
|
|
|
The newly constructed object.
|
|
|
|
"""
|
2016-10-16 22:34:57 +03:00
|
|
|
if model is None:
|
2017-03-11 15:59:13 +03:00
|
|
|
model = TaggerModel(cfg.get('features', self.feature_templates),
|
|
|
|
L1=0.0)
|
2015-08-26 20:19:21 +03:00
|
|
|
self.vocab = vocab
|
2015-08-27 10:16:11 +03:00
|
|
|
self.model = model
|
2017-03-10 03:43:47 +03:00
|
|
|
self.model.l1_penalty = 0.0
|
2015-08-27 10:16:11 +03:00
|
|
|
# TODO: Move this to tag map
|
2015-08-24 06:25:55 +03:00
|
|
|
self.freqs = {TAG: defaultdict(int)}
|
|
|
|
for tag in self.tag_names:
|
2015-08-26 20:19:21 +03:00
|
|
|
self.freqs[TAG][self.vocab.strings[tag]] = 1
|
2015-08-24 06:25:55 +03:00
|
|
|
self.freqs[TAG][0] = 1
|
2016-10-17 01:55:15 +03:00
|
|
|
self.cfg = cfg
|
2015-08-24 06:25:55 +03:00
|
|
|
|
2015-08-26 20:19:21 +03:00
|
|
|
@property
|
|
|
|
def tag_names(self):
|
2015-08-28 04:44:54 +03:00
|
|
|
return self.vocab.morphology.tag_names
|
2015-08-26 20:19:21 +03:00
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
def __reduce__(self):
|
|
|
|
return (self.__class__, (self.vocab, self.model), None, None)
|
|
|
|
|
|
|
|
def tag_from_strings(self, Doc tokens, object tag_strs):
|
|
|
|
cdef int i
|
|
|
|
for i in range(tokens.length):
|
|
|
|
self.vocab.morphology.assign_tag(&tokens.c[i], tag_strs[i])
|
|
|
|
tokens.is_tagged = True
|
|
|
|
tokens._py_tokens = [None] * tokens.length
|
|
|
|
|
2015-08-24 06:25:55 +03:00
|
|
|
def __call__(self, Doc tokens):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Apply the tagger, setting the POS tags onto the Doc object.
|
2015-08-24 06:25:55 +03:00
|
|
|
|
2016-11-01 14:25:36 +03:00
|
|
|
Arguments:
|
|
|
|
doc (Doc): The tokens to be tagged.
|
|
|
|
Returns:
|
|
|
|
None
|
2015-08-24 06:25:55 +03:00
|
|
|
"""
|
|
|
|
if tokens.length == 0:
|
|
|
|
return 0
|
2015-11-05 16:25:59 +03:00
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
cdef Pool mem = Pool()
|
2015-10-12 11:33:11 +03:00
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
cdef int i, tag
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef Example eg = Example(nr_atom=N_CONTEXT_FIELDS,
|
|
|
|
nr_class=self.vocab.morphology.n_tags,
|
|
|
|
nr_feat=self.model.nr_feat)
|
2015-08-24 06:25:55 +03:00
|
|
|
for i in range(tokens.length):
|
2017-02-27 00:27:11 +03:00
|
|
|
if tokens.c[i].pos == 0:
|
2017-01-09 19:08:34 +03:00
|
|
|
self.model.set_featuresC(&eg.c, tokens.c, i)
|
2016-01-29 05:58:55 +03:00
|
|
|
self.model.set_scoresC(eg.c.scores,
|
2016-09-21 13:26:14 +03:00
|
|
|
eg.c.features, eg.c.nr_feat)
|
2016-01-29 05:58:55 +03:00
|
|
|
guess = VecVec.arg_max_if_true(eg.c.scores, eg.c.is_valid, eg.c.nr_class)
|
2016-11-04 21:19:09 +03:00
|
|
|
self.vocab.morphology.assign_tag_id(&tokens.c[i], guess)
|
2016-02-05 13:44:39 +03:00
|
|
|
eg.fill_scores(0, eg.c.nr_class)
|
2015-08-24 06:25:55 +03:00
|
|
|
tokens.is_tagged = True
|
|
|
|
tokens._py_tokens = [None] * tokens.length
|
2016-02-03 04:04:55 +03:00
|
|
|
|
|
|
|
def pipe(self, stream, batch_size=1000, n_threads=2):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Tag a stream of documents.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
stream: The sequence of documents to tag.
|
|
|
|
batch_size (int):
|
|
|
|
The number of documents to accumulate into a working set.
|
|
|
|
n_threads (int):
|
|
|
|
The number of threads with which to work on the buffer in parallel,
|
|
|
|
if the Matcher implementation supports multi-threading.
|
|
|
|
Yields:
|
|
|
|
Doc Documents, in order.
|
|
|
|
"""
|
2016-02-03 04:04:55 +03:00
|
|
|
for doc in stream:
|
|
|
|
self(doc)
|
|
|
|
yield doc
|
2017-02-27 00:27:11 +03:00
|
|
|
|
2017-03-11 20:12:21 +03:00
|
|
|
def update(self, Doc tokens, GoldParse gold, itn=0):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Update the statistical model, with tags supplied for the given document.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
doc (Doc):
|
|
|
|
The document to update on.
|
|
|
|
gold (GoldParse):
|
|
|
|
Manager for the gold-standard tags.
|
|
|
|
Returns (int):
|
|
|
|
Number of tags correct.
|
|
|
|
"""
|
2016-10-17 01:55:15 +03:00
|
|
|
gold_tag_strs = gold.tags
|
2015-08-27 10:16:11 +03:00
|
|
|
assert len(tokens) == len(gold_tag_strs)
|
2016-02-03 00:59:59 +03:00
|
|
|
for tag in gold_tag_strs:
|
2016-02-18 15:24:20 +03:00
|
|
|
if tag != None and tag not in self.tag_names:
|
2016-10-09 13:24:24 +03:00
|
|
|
msg = ("Unrecognized gold tag: %s. tag_map.json must contain all "
|
2016-02-03 00:59:59 +03:00
|
|
|
"gold tags, to maintain coarse-grained mapping.")
|
|
|
|
raise ValueError(msg % tag)
|
2015-11-06 19:24:30 +03:00
|
|
|
golds = [self.tag_names.index(g) if g is not None else -1 for g in gold_tag_strs]
|
|
|
|
cdef int correct = 0
|
|
|
|
cdef Pool mem = Pool()
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef Example eg = Example(
|
|
|
|
nr_atom=N_CONTEXT_FIELDS,
|
|
|
|
nr_class=self.vocab.morphology.n_tags,
|
|
|
|
nr_feat=self.model.nr_feat)
|
2015-08-24 06:25:55 +03:00
|
|
|
for i in range(tokens.length):
|
2017-01-09 19:08:34 +03:00
|
|
|
self.model.set_featuresC(&eg.c, tokens.c, i)
|
2016-02-18 15:24:20 +03:00
|
|
|
eg.costs = [ 1 if golds[i] not in (c, -1) else 0 for c in xrange(eg.nr_class) ]
|
2016-01-29 05:58:55 +03:00
|
|
|
self.model.set_scoresC(eg.c.scores,
|
2016-09-21 13:26:14 +03:00
|
|
|
eg.c.features, eg.c.nr_feat)
|
2017-03-11 15:59:13 +03:00
|
|
|
self.model.update(eg)
|
2015-08-28 03:02:33 +03:00
|
|
|
|
2016-11-15 09:11:22 +03:00
|
|
|
self.vocab.morphology.assign_tag_id(&tokens.c[i], eg.guess)
|
2017-02-27 00:27:11 +03:00
|
|
|
|
2015-11-06 19:24:30 +03:00
|
|
|
correct += eg.cost == 0
|
2015-11-03 16:15:14 +03:00
|
|
|
self.freqs[TAG][tokens.c[i].tag] += 1
|
2016-02-05 13:44:39 +03:00
|
|
|
eg.fill_scores(0, eg.c.nr_class)
|
|
|
|
eg.fill_costs(0, eg.c.nr_class)
|
2015-11-06 19:24:30 +03:00
|
|
|
tokens.is_tagged = True
|
|
|
|
tokens._py_tokens = [None] * tokens.length
|
2015-08-24 06:25:55 +03:00
|
|
|
return correct
|
2016-10-16 22:34:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
feature_templates = (
|
|
|
|
(W_orth,),
|
|
|
|
(P1_lemma, P1_pos),
|
|
|
|
(P2_lemma, P2_pos),
|
|
|
|
(N1_orth,),
|
|
|
|
(N2_orth,),
|
|
|
|
|
|
|
|
(W_suffix,),
|
|
|
|
(W_prefix,),
|
|
|
|
|
|
|
|
(P1_pos,),
|
|
|
|
(P2_pos,),
|
|
|
|
(P1_pos, P2_pos),
|
|
|
|
(P1_pos, W_orth),
|
|
|
|
(P1_suffix,),
|
|
|
|
(N1_suffix,),
|
|
|
|
|
|
|
|
(W_shape,),
|
|
|
|
(W_cluster,),
|
|
|
|
(N1_cluster,),
|
|
|
|
(N2_cluster,),
|
|
|
|
(P1_cluster,),
|
|
|
|
(P2_cluster,),
|
|
|
|
|
|
|
|
(W_flags,),
|
|
|
|
(N1_flags,),
|
|
|
|
(N2_flags,),
|
|
|
|
(P1_flags,),
|
|
|
|
(P2_flags,),
|
|
|
|
)
|