2015-03-09 08:46:22 +03:00
|
|
|
import numpy
|
|
|
|
import codecs
|
2015-05-06 17:27:31 +03:00
|
|
|
import json
|
2015-05-30 18:54:52 +03:00
|
|
|
import ujson
|
2015-05-24 03:49:56 +03:00
|
|
|
import random
|
2015-05-24 22:50:48 +03:00
|
|
|
import re
|
2015-05-29 04:52:55 +03:00
|
|
|
import os
|
|
|
|
from os import path
|
2015-03-08 08:14:48 +03:00
|
|
|
|
2015-03-09 08:46:22 +03:00
|
|
|
from libc.string cimport memset
|
2015-03-08 08:14:48 +03:00
|
|
|
|
2015-02-21 19:06:58 +03:00
|
|
|
|
2015-06-08 01:54:13 +03:00
|
|
|
def tags_to_entities(tags):
|
|
|
|
entities = []
|
|
|
|
start = None
|
|
|
|
for i, tag in enumerate(tags):
|
|
|
|
if tag.startswith('O'):
|
|
|
|
# TODO: We shouldn't be getting these malformed inputs. Fix this.
|
|
|
|
if start is not None:
|
|
|
|
start = None
|
|
|
|
continue
|
|
|
|
elif tag == '-':
|
|
|
|
continue
|
|
|
|
elif tag.startswith('I'):
|
|
|
|
assert start is not None, tags[:i]
|
|
|
|
continue
|
|
|
|
if tag.startswith('U'):
|
|
|
|
entities.append((tag[2:], i, i))
|
|
|
|
elif tag.startswith('B'):
|
|
|
|
start = i
|
|
|
|
elif tag.startswith('L'):
|
|
|
|
entities.append((tag[2:], start, i))
|
|
|
|
start = None
|
|
|
|
else:
|
|
|
|
raise Exception(tag)
|
|
|
|
return entities
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-24 22:50:48 +03:00
|
|
|
def align(cand_words, gold_words):
|
|
|
|
cost, edit_path = _min_edit_path(cand_words, gold_words)
|
|
|
|
alignment = []
|
|
|
|
i_of_gold = 0
|
|
|
|
for move in edit_path:
|
|
|
|
if move == 'M':
|
|
|
|
alignment.append(i_of_gold)
|
|
|
|
i_of_gold += 1
|
|
|
|
elif move == 'S':
|
|
|
|
alignment.append(None)
|
|
|
|
i_of_gold += 1
|
|
|
|
elif move == 'D':
|
|
|
|
alignment.append(None)
|
|
|
|
elif move == 'I':
|
|
|
|
i_of_gold += 1
|
|
|
|
else:
|
|
|
|
raise Exception(move)
|
|
|
|
return alignment
|
|
|
|
|
|
|
|
|
|
|
|
punct_re = re.compile(r'\W')
|
|
|
|
def _min_edit_path(cand_words, gold_words):
|
|
|
|
cdef:
|
|
|
|
Pool mem
|
|
|
|
int i, j, n_cand, n_gold
|
|
|
|
int* curr_costs
|
|
|
|
int* prev_costs
|
|
|
|
|
|
|
|
# TODO: Fix this --- just do it properly, make the full edit matrix and
|
|
|
|
# then walk back over it...
|
|
|
|
# Preprocess inputs
|
|
|
|
cand_words = [punct_re.sub('', w) for w in cand_words]
|
|
|
|
gold_words = [punct_re.sub('', w) for w in gold_words]
|
2015-05-27 20:13:11 +03:00
|
|
|
|
|
|
|
if cand_words == gold_words:
|
2015-06-06 06:58:53 +03:00
|
|
|
return 0, ''.join(['M' for _ in gold_words])
|
2015-05-27 20:13:11 +03:00
|
|
|
mem = Pool()
|
2015-05-24 22:50:48 +03:00
|
|
|
n_cand = len(cand_words)
|
|
|
|
n_gold = len(gold_words)
|
|
|
|
# Levenshtein distance, except we need the history, and we may want different
|
|
|
|
# costs.
|
|
|
|
# Mark operations with a string, and score the history using _edit_cost.
|
|
|
|
previous_row = []
|
|
|
|
prev_costs = <int*>mem.alloc(n_gold + 1, sizeof(int))
|
|
|
|
curr_costs = <int*>mem.alloc(n_gold + 1, sizeof(int))
|
|
|
|
for i in range(n_gold + 1):
|
|
|
|
cell = ''
|
|
|
|
for j in range(i):
|
|
|
|
cell += 'I'
|
|
|
|
previous_row.append('I' * i)
|
|
|
|
prev_costs[i] = i
|
|
|
|
for i, cand in enumerate(cand_words):
|
|
|
|
current_row = ['D' * (i + 1)]
|
|
|
|
curr_costs[0] = i+1
|
|
|
|
for j, gold in enumerate(gold_words):
|
|
|
|
if gold.lower() == cand.lower():
|
|
|
|
s_cost = prev_costs[j]
|
|
|
|
i_cost = curr_costs[j] + 1
|
|
|
|
d_cost = prev_costs[j + 1] + 1
|
|
|
|
else:
|
|
|
|
s_cost = prev_costs[j] + 1
|
|
|
|
i_cost = curr_costs[j] + 1
|
|
|
|
d_cost = prev_costs[j + 1] + (1 if cand else 0)
|
|
|
|
|
|
|
|
if s_cost <= i_cost and s_cost <= d_cost:
|
|
|
|
best_cost = s_cost
|
|
|
|
best_hist = previous_row[j] + ('M' if gold == cand else 'S')
|
|
|
|
elif i_cost <= s_cost and i_cost <= d_cost:
|
|
|
|
best_cost = i_cost
|
|
|
|
best_hist = current_row[j] + 'I'
|
|
|
|
else:
|
|
|
|
best_cost = d_cost
|
|
|
|
best_hist = previous_row[j + 1] + 'D'
|
|
|
|
|
|
|
|
current_row.append(best_hist)
|
|
|
|
curr_costs[j+1] = best_cost
|
|
|
|
previous_row = current_row
|
|
|
|
for j in range(len(gold_words) + 1):
|
|
|
|
prev_costs[j] = curr_costs[j]
|
|
|
|
curr_costs[j] = 0
|
|
|
|
|
|
|
|
return prev_costs[n_gold], previous_row[-1]
|
|
|
|
|
2015-05-27 20:13:11 +03:00
|
|
|
|
2015-06-12 03:42:08 +03:00
|
|
|
def read_json_file(loc, docs_filter=None):
|
2015-05-30 18:54:52 +03:00
|
|
|
print loc
|
2015-05-29 04:52:55 +03:00
|
|
|
if path.isdir(loc):
|
|
|
|
for filename in os.listdir(loc):
|
|
|
|
yield from read_json_file(path.join(loc, filename))
|
|
|
|
else:
|
|
|
|
with open(loc) as file_:
|
2015-05-30 18:54:52 +03:00
|
|
|
docs = ujson.load(file_)
|
|
|
|
for doc in docs:
|
2015-06-12 03:42:08 +03:00
|
|
|
if docs_filter is not None and not docs_filter(doc):
|
|
|
|
continue
|
2015-05-30 18:54:52 +03:00
|
|
|
paragraphs = []
|
|
|
|
for paragraph in doc['paragraphs']:
|
|
|
|
sents = []
|
|
|
|
for sent in paragraph['sentences']:
|
|
|
|
words = []
|
|
|
|
ids = []
|
|
|
|
tags = []
|
|
|
|
heads = []
|
|
|
|
labels = []
|
|
|
|
ner = []
|
|
|
|
for i, token in enumerate(sent['tokens']):
|
|
|
|
words.append(token['orth'])
|
|
|
|
ids.append(i)
|
|
|
|
tags.append(token['tag'])
|
|
|
|
heads.append(token['head'] + i)
|
|
|
|
labels.append(token['dep'])
|
|
|
|
ner.append(token.get('ner', '-'))
|
|
|
|
sents.append((
|
|
|
|
(ids, words, tags, heads, labels, ner),
|
|
|
|
sent.get('brackets', [])))
|
|
|
|
if sents:
|
2015-05-30 02:25:46 +03:00
|
|
|
yield (paragraph.get('raw', None), sents)
|
2015-05-06 17:27:31 +03:00
|
|
|
|
|
|
|
|
2015-04-10 05:59:11 +03:00
|
|
|
def _iob_to_biluo(tags):
|
|
|
|
out = []
|
|
|
|
curr_label = None
|
|
|
|
tags = list(tags)
|
|
|
|
while tags:
|
|
|
|
out.extend(_consume_os(tags))
|
|
|
|
out.extend(_consume_ent(tags))
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def _consume_os(tags):
|
|
|
|
while tags and tags[0] == 'O':
|
|
|
|
yield tags.pop(0)
|
|
|
|
|
|
|
|
|
|
|
|
def _consume_ent(tags):
|
|
|
|
if not tags:
|
|
|
|
return []
|
|
|
|
target = tags.pop(0).replace('B', 'I')
|
|
|
|
length = 1
|
|
|
|
while tags and tags[0] == target:
|
|
|
|
length += 1
|
|
|
|
tags.pop(0)
|
|
|
|
label = target[2:]
|
|
|
|
if length == 1:
|
|
|
|
return ['U-' + label]
|
|
|
|
else:
|
|
|
|
start = 'B-' + label
|
|
|
|
end = 'L-' + label
|
|
|
|
middle = ['I-%s' % label for _ in range(1, length - 1)]
|
|
|
|
return [start] + middle + [end]
|
|
|
|
|
|
|
|
|
2015-03-09 08:46:22 +03:00
|
|
|
cdef class GoldParse:
|
2015-05-31 02:11:56 +03:00
|
|
|
def __init__(self, tokens, annot_tuples, brackets=tuple(), make_projective=False):
|
2015-03-09 08:46:22 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self.loss = 0
|
2015-02-23 22:04:53 +03:00
|
|
|
self.length = len(tokens)
|
2015-03-09 08:46:22 +03:00
|
|
|
|
2015-03-09 14:06:01 +03:00
|
|
|
# These are filled by the tagger/parser/entity recogniser
|
2015-06-02 21:01:06 +03:00
|
|
|
self.c.tags = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
|
|
|
self.c.heads = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
|
|
|
self.c.labels = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
|
|
|
self.c.ner = <Transition*>self.mem.alloc(len(tokens), sizeof(Transition))
|
|
|
|
self.c.brackets = <int**>self.mem.alloc(len(tokens), sizeof(int*))
|
2015-05-11 17:12:03 +03:00
|
|
|
for i in range(len(tokens)):
|
2015-06-02 21:01:06 +03:00
|
|
|
self.c.brackets[i] = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
2015-03-09 08:46:22 +03:00
|
|
|
|
2015-03-09 14:06:01 +03:00
|
|
|
self.tags = [None] * len(tokens)
|
2015-05-24 03:49:56 +03:00
|
|
|
self.heads = [None] * len(tokens)
|
|
|
|
self.labels = [''] * len(tokens)
|
|
|
|
self.ner = ['-'] * len(tokens)
|
|
|
|
|
2015-05-24 18:35:49 +03:00
|
|
|
self.cand_to_gold = align([t.orth_ for t in tokens], annot_tuples[1])
|
|
|
|
self.gold_to_cand = align(annot_tuples[1], [t.orth_ for t in tokens])
|
|
|
|
|
|
|
|
self.orig_annot = zip(*annot_tuples)
|
2015-03-09 08:46:22 +03:00
|
|
|
|
2015-05-24 18:35:49 +03:00
|
|
|
for i, gold_i in enumerate(self.cand_to_gold):
|
2015-05-24 03:49:56 +03:00
|
|
|
if gold_i is None:
|
|
|
|
# TODO: What do we do for missing values again?
|
2015-03-09 08:46:22 +03:00
|
|
|
pass
|
2015-05-24 03:49:56 +03:00
|
|
|
else:
|
|
|
|
self.tags[i] = annot_tuples[2][gold_i]
|
2015-05-24 18:35:49 +03:00
|
|
|
self.heads[i] = self.gold_to_cand[annot_tuples[3][gold_i]]
|
2015-05-24 03:49:56 +03:00
|
|
|
self.labels[i] = annot_tuples[4][gold_i]
|
2015-05-27 04:17:50 +03:00
|
|
|
self.ner[i] = annot_tuples[5][gold_i]
|
2015-05-31 02:11:56 +03:00
|
|
|
|
|
|
|
# If we have any non-projective arcs, i.e. crossing brackets, consider
|
|
|
|
# the heads for those words missing in the gold-standard.
|
|
|
|
# This way, we can train from these sentences
|
|
|
|
cdef int w1, w2, h1, h2
|
|
|
|
if make_projective:
|
|
|
|
heads = list(self.heads)
|
|
|
|
for w1 in range(self.length):
|
|
|
|
if heads[w1] is not None:
|
|
|
|
h1 = heads[w1]
|
|
|
|
for w2 in range(w1+1, self.length):
|
|
|
|
if heads[w2] is not None:
|
|
|
|
h2 = heads[w2]
|
|
|
|
if _arcs_cross(w1, h1, w2, h2):
|
|
|
|
self.heads[w1] = None
|
|
|
|
self.labels[w1] = ''
|
|
|
|
self.heads[w2] = None
|
|
|
|
self.labels[w2] = ''
|
2015-03-10 20:00:23 +03:00
|
|
|
|
2015-05-11 17:12:03 +03:00
|
|
|
self.brackets = {}
|
2015-05-24 03:49:56 +03:00
|
|
|
for (gold_start, gold_end, label_str) in brackets:
|
2015-05-24 18:35:49 +03:00
|
|
|
start = self.gold_to_cand[gold_start]
|
|
|
|
end = self.gold_to_cand[gold_end]
|
2015-05-24 03:49:56 +03:00
|
|
|
if start is not None and end is not None:
|
|
|
|
self.brackets.setdefault(start, {}).setdefault(end, set())
|
2015-05-27 04:17:50 +03:00
|
|
|
self.brackets[end][start].add(label_str)
|
2015-05-11 17:12:03 +03:00
|
|
|
|
2015-03-10 20:00:23 +03:00
|
|
|
def __len__(self):
|
|
|
|
return self.length
|
2015-03-09 08:46:22 +03:00
|
|
|
|
2015-05-30 02:25:46 +03:00
|
|
|
@property
|
|
|
|
def is_projective(self):
|
2015-05-31 02:11:56 +03:00
|
|
|
heads = list(self.heads)
|
|
|
|
for w1 in range(self.length):
|
|
|
|
if heads[w1] is not None:
|
|
|
|
h1 = heads[w1]
|
|
|
|
for w2 in range(self.length):
|
|
|
|
if heads[w2] is not None and _arcs_cross(w1, h1, w2, heads[w2]):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
cdef int _arcs_cross(int w1, int h1, int w2, int h2) except -1:
|
|
|
|
if w1 > h1:
|
|
|
|
w1, h1 = h1, w1
|
|
|
|
if w2 > h2:
|
|
|
|
w2, h2 = h2, w2
|
|
|
|
if w1 > w2:
|
|
|
|
w1, h1, w2, h2 = w2, h2, w1, h1
|
|
|
|
return w1 < w2 < h1 < h2 or w1 < w2 == h2 < h1
|
2015-05-30 02:25:46 +03:00
|
|
|
|
2015-02-21 19:06:58 +03:00
|
|
|
|
|
|
|
def is_punct_label(label):
|
|
|
|
return label == 'P' or label.lower() == 'punct'
|