2014-11-04 16:37:59 +03:00
|
|
|
# cython: profile=True
|
2014-11-04 19:42:14 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import division
|
|
|
|
|
2014-11-04 16:37:59 +03:00
|
|
|
from os import path
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import random
|
|
|
|
import json
|
|
|
|
import cython
|
|
|
|
|
2014-12-07 07:29:04 +03:00
|
|
|
from thinc.features cimport Feature, count_feats
|
2014-11-04 16:37:59 +03:00
|
|
|
|
2014-11-04 19:42:14 +03:00
|
|
|
|
2014-12-08 13:12:15 +03:00
|
|
|
def setup_model_dir(tag_names, tag_map, tag_counts, templates, model_dir):
|
2014-11-04 19:42:14 +03:00
|
|
|
if path.exists(model_dir):
|
|
|
|
shutil.rmtree(model_dir)
|
|
|
|
os.mkdir(model_dir)
|
|
|
|
config = {
|
|
|
|
'templates': templates,
|
|
|
|
'tag_names': tag_names,
|
2014-12-08 13:12:15 +03:00
|
|
|
'tag_map': tag_map,
|
2014-12-07 14:07:16 +03:00
|
|
|
'tag_counts': tag_counts,
|
2014-11-04 19:42:14 +03:00
|
|
|
}
|
|
|
|
with open(path.join(model_dir, 'config.json'), 'w') as file_:
|
|
|
|
json.dump(config, file_)
|
|
|
|
|
|
|
|
|
2014-11-04 16:37:59 +03:00
|
|
|
cdef class Tagger:
|
2014-12-07 16:03:55 +03:00
|
|
|
"""Predict some type of tag, using greedy decoding. The tagger reads its
|
|
|
|
model and configuration from disk.
|
2014-11-04 16:37:59 +03:00
|
|
|
"""
|
|
|
|
def __init__(self, model_dir):
|
|
|
|
self.mem = Pool()
|
2014-11-04 19:42:14 +03:00
|
|
|
cfg = json.load(open(path.join(model_dir, 'config.json')))
|
2014-11-04 16:37:59 +03:00
|
|
|
templates = cfg['templates']
|
2014-12-08 13:12:15 +03:00
|
|
|
univ_counts = {}
|
|
|
|
cdef unicode tag
|
|
|
|
cdef unicode univ_tag
|
2014-11-04 16:37:59 +03:00
|
|
|
self.tag_names = cfg['tag_names']
|
2014-12-07 14:07:16 +03:00
|
|
|
self.tagdict = _make_tag_dict(cfg['tag_counts'])
|
2014-12-07 07:29:04 +03:00
|
|
|
self.extractor = Extractor(templates)
|
2014-12-07 14:07:16 +03:00
|
|
|
self.model = LinearModel(len(self.tag_names), self.extractor.n_templ+2)
|
2014-11-04 16:37:59 +03:00
|
|
|
if path.exists(path.join(model_dir, 'model')):
|
|
|
|
self.model.load(path.join(model_dir, 'model'))
|
|
|
|
|
2014-12-08 13:12:15 +03:00
|
|
|
cdef class_t predict(self, atom_t* context, object golds=None) except *:
|
|
|
|
"""Predict the tag of tokens[i].
|
2014-11-04 16:37:59 +03:00
|
|
|
|
|
|
|
>>> tokens = EN.tokenize(u'An example sentence.')
|
|
|
|
>>> tag = EN.pos_tagger.predict(0, tokens)
|
|
|
|
>>> assert tag == EN.pos_tagger.tag_id('DT') == 5
|
|
|
|
"""
|
2014-12-07 14:07:16 +03:00
|
|
|
cdef int n_feats
|
2014-12-18 12:41:52 +03:00
|
|
|
cdef const Feature* feats = self.extractor.get_feats(context, &n_feats)
|
|
|
|
cdef const weight_t* scores = self.model.get_scores(feats, n_feats)
|
2014-12-07 14:07:16 +03:00
|
|
|
guess = _arg_max(scores, self.model.nr_class)
|
2014-12-07 07:29:04 +03:00
|
|
|
if golds is not None and guess not in golds:
|
|
|
|
best = _arg_max_among(scores, golds)
|
2014-12-07 14:07:16 +03:00
|
|
|
counts = {guess: {}, best: {}}
|
2014-12-07 07:29:04 +03:00
|
|
|
count_feats(counts[guess], feats, n_feats, -1)
|
|
|
|
count_feats(counts[best], feats, n_feats, 1)
|
|
|
|
self.model.update(counts)
|
|
|
|
return guess
|
2014-11-04 16:37:59 +03:00
|
|
|
|
|
|
|
def tag_id(self, object tag_name):
|
|
|
|
"""Encode tag_name into a tag ID integer."""
|
|
|
|
tag_id = self.tag_names.index(tag_name)
|
|
|
|
if tag_id == -1:
|
|
|
|
tag_id = len(self.tag_names)
|
|
|
|
self.tag_names.append(tag_name)
|
|
|
|
return tag_id
|
2014-12-07 07:29:04 +03:00
|
|
|
|
|
|
|
|
2014-12-07 14:07:16 +03:00
|
|
|
def _make_tag_dict(counts):
|
2014-12-09 17:02:04 +03:00
|
|
|
freq_thresh = 20
|
|
|
|
ambiguity_thresh = 0.97
|
2014-12-07 14:07:16 +03:00
|
|
|
tagdict = {}
|
|
|
|
cdef atom_t word
|
|
|
|
cdef atom_t tag
|
|
|
|
for word_str, tag_freqs in counts.items():
|
|
|
|
tag_str, mode = max(tag_freqs.items(), key=lambda item: item[1])
|
|
|
|
n = sum(tag_freqs.values())
|
|
|
|
word = int(word_str)
|
|
|
|
tag = int(tag_str)
|
|
|
|
if n >= freq_thresh and (float(mode) / n) >= ambiguity_thresh:
|
|
|
|
tagdict[word] = tag
|
|
|
|
return tagdict
|
|
|
|
|
|
|
|
|
2014-12-18 12:41:52 +03:00
|
|
|
cdef int _arg_max(const weight_t* scores, int n_classes) except -1:
|
2014-12-07 07:29:04 +03:00
|
|
|
cdef int best = 0
|
|
|
|
cdef weight_t score = scores[best]
|
|
|
|
cdef int i
|
|
|
|
for i in range(1, n_classes):
|
2014-12-07 14:07:16 +03:00
|
|
|
if scores[i] >= score:
|
2014-12-07 07:29:04 +03:00
|
|
|
score = scores[i]
|
|
|
|
best = i
|
|
|
|
return best
|
|
|
|
|
|
|
|
|
2014-12-18 12:41:52 +03:00
|
|
|
cdef int _arg_max_among(const weight_t* scores, list classes) except -1:
|
2014-12-07 07:29:04 +03:00
|
|
|
cdef int best = classes[0]
|
|
|
|
cdef weight_t score = scores[best]
|
|
|
|
cdef class_t clas
|
|
|
|
for clas in classes:
|
|
|
|
if scores[clas] > score:
|
|
|
|
score = scores[clas]
|
|
|
|
best = clas
|
|
|
|
return best
|