2015-03-09 05:46:22 +00:00
|
|
|
import numpy
|
|
|
|
import codecs
|
2015-05-06 14:27:31 +00:00
|
|
|
import json
|
2015-05-24 00:49:56 +00:00
|
|
|
import random
|
|
|
|
from spacy.munge.alignment import align
|
2015-03-08 05:14:48 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
from libc.string cimport memset
|
2015-03-08 05:14:48 +00:00
|
|
|
|
2015-02-21 16:06:58 +00:00
|
|
|
|
2015-05-06 14:27:31 +00:00
|
|
|
def read_json_file(loc):
|
|
|
|
paragraphs = []
|
|
|
|
for doc in json.load(open(loc)):
|
|
|
|
for paragraph in doc['paragraphs']:
|
|
|
|
words = []
|
|
|
|
ids = []
|
|
|
|
tags = []
|
|
|
|
heads = []
|
|
|
|
labels = []
|
|
|
|
iob_ents = []
|
|
|
|
for token in paragraph['tokens']:
|
|
|
|
words.append(token['orth'])
|
2015-05-24 00:49:56 +00:00
|
|
|
ids.append(token['id'])
|
2015-05-06 14:27:31 +00:00
|
|
|
tags.append(token['tag'])
|
2015-05-24 00:49:56 +00:00
|
|
|
heads.append(token['head'] if token['head'] >= 0 else token['id'])
|
2015-05-06 14:27:31 +00:00
|
|
|
labels.append(token['dep'])
|
2015-05-24 00:49:56 +00:00
|
|
|
iob_ents.append(token.get('iob_ent', '-'))
|
2015-05-06 14:27:31 +00:00
|
|
|
|
|
|
|
brackets = []
|
|
|
|
paragraphs.append((paragraph['raw'],
|
|
|
|
(ids, words, tags, heads, labels, _iob_to_biluo(iob_ents)),
|
2015-05-11 14:12:03 +00:00
|
|
|
paragraph.get('brackets', [])))
|
2015-05-06 14:27:31 +00:00
|
|
|
return paragraphs
|
|
|
|
|
|
|
|
|
2015-04-10 02:59:11 +00: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 05:46:22 +00:00
|
|
|
cdef class GoldParse:
|
2015-05-12 20:33:47 +00:00
|
|
|
def __init__(self, tokens, annot_tuples, brackets=tuple()):
|
2015-03-09 05:46:22 +00:00
|
|
|
self.mem = Pool()
|
|
|
|
self.loss = 0
|
2015-02-23 19:04:53 +00:00
|
|
|
self.length = len(tokens)
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
# These are filled by the tagger/parser/entity recogniser
|
|
|
|
self.c_tags = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
2015-03-09 05:46:22 +00:00
|
|
|
self.c_heads = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
|
|
|
self.c_labels = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
2015-03-09 11:06:01 +00:00
|
|
|
self.c_ner = <Transition*>self.mem.alloc(len(tokens), sizeof(Transition))
|
2015-05-11 14:12:03 +00:00
|
|
|
self.c_brackets = <int**>self.mem.alloc(len(tokens), sizeof(int*))
|
|
|
|
for i in range(len(tokens)):
|
|
|
|
self.c_brackets[i] = <int*>self.mem.alloc(len(tokens), sizeof(int))
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
self.tags = [None] * len(tokens)
|
2015-05-24 00:49:56 +00:00
|
|
|
self.heads = [None] * len(tokens)
|
|
|
|
self.labels = [''] * len(tokens)
|
|
|
|
self.ner = ['-'] * len(tokens)
|
|
|
|
|
2015-05-24 15:35:49 +00: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 05:46:22 +00:00
|
|
|
|
2015-03-10 17:00:23 +00:00
|
|
|
self.ents = []
|
2015-05-24 00:49:56 +00:00
|
|
|
|
2015-05-24 15:35:49 +00:00
|
|
|
for i, gold_i in enumerate(self.cand_to_gold):
|
2015-05-24 00:49:56 +00:00
|
|
|
if gold_i is None:
|
|
|
|
# TODO: What do we do for missing values again?
|
2015-03-09 05:46:22 +00:00
|
|
|
pass
|
2015-05-24 00:49:56 +00:00
|
|
|
else:
|
|
|
|
self.tags[i] = annot_tuples[2][gold_i]
|
2015-05-24 15:35:49 +00:00
|
|
|
self.heads[i] = self.gold_to_cand[annot_tuples[3][gold_i]]
|
2015-05-24 00:49:56 +00:00
|
|
|
self.labels[i] = annot_tuples[4][gold_i]
|
|
|
|
# TODO: Declare NER information MISSING if tokenization incorrect
|
2015-03-10 17:00:23 +00:00
|
|
|
for start, end, label in self.ents:
|
|
|
|
if start == (end - 1):
|
|
|
|
self.ner[start] = 'U-%s' % label
|
|
|
|
else:
|
|
|
|
self.ner[start] = 'B-%s' % label
|
|
|
|
for i in range(start+1, end-1):
|
|
|
|
self.ner[i] = 'I-%s' % label
|
|
|
|
self.ner[end-1] = 'L-%s' % label
|
|
|
|
|
2015-05-11 14:12:03 +00:00
|
|
|
self.brackets = {}
|
2015-05-24 00:49:56 +00:00
|
|
|
for (gold_start, gold_end, label_str) in brackets:
|
2015-05-24 15:35:49 +00:00
|
|
|
start = self.gold_to_cand[gold_start]
|
|
|
|
end = self.gold_to_cand[gold_end]
|
2015-05-24 00:49:56 +00:00
|
|
|
if start is not None and end is not None:
|
|
|
|
self.brackets.setdefault(start, {}).setdefault(end, set())
|
2015-05-11 14:12:03 +00:00
|
|
|
self.brackets[end][start].add(label)
|
|
|
|
|
2015-03-10 17:00:23 +00:00
|
|
|
def __len__(self):
|
|
|
|
return self.length
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-02-21 16:06:58 +00:00
|
|
|
|
|
|
|
def is_punct_label(label):
|
|
|
|
return label == 'P' or label.lower() == 'punct'
|