2019-05-07 17:03:42 +03:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
from os import listdir
|
2019-05-14 23:55:56 +03:00
|
|
|
import numpy as np
|
2019-05-16 19:25:34 +03:00
|
|
|
import random
|
2019-05-23 00:40:10 +03:00
|
|
|
from random import shuffle
|
2019-05-16 19:25:34 +03:00
|
|
|
from thinc.neural._classes.convolution import ExtractWindow
|
2019-05-07 17:03:42 +03:00
|
|
|
|
|
|
|
from examples.pipeline.wiki_entity_linking import run_el, training_set_creator, kb_creator
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
from spacy._ml import SpacyVectors, create_default_optimizer, zero_init, logistic
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-20 12:58:48 +03:00
|
|
|
from thinc.api import chain, concatenate, flatten_add_lengths, clone, with_flatten
|
2019-05-21 00:54:55 +03:00
|
|
|
from thinc.v2v import Model, Maxout, Affine, ReLu
|
2019-05-20 12:58:48 +03:00
|
|
|
from thinc.t2v import Pooling, mean_pool, sum_pool
|
2019-05-14 09:37:52 +03:00
|
|
|
from thinc.t2t import ParametricAttention
|
|
|
|
from thinc.misc import Residual
|
2019-05-16 19:25:34 +03:00
|
|
|
from thinc.misc import LayerNorm as LN
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
from spacy.tokens import Doc
|
|
|
|
|
2019-05-07 17:03:42 +03:00
|
|
|
""" TODO: this code needs to be implemented in pipes.pyx"""
|
|
|
|
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
class EL_Model:
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-21 00:54:55 +03:00
|
|
|
PRINT_TRAIN = False
|
2019-05-15 03:23:08 +03:00
|
|
|
EPS = 0.0000000005
|
2019-05-16 19:25:34 +03:00
|
|
|
CUTOFF = 0.5
|
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
BATCH_SIZE = 5
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
INPUT_DIM = 300
|
2019-05-23 00:40:10 +03:00
|
|
|
HIDDEN_1_WIDTH = 32 # 10
|
2019-05-22 00:42:46 +03:00
|
|
|
HIDDEN_2_WIDTH = 32 # 6
|
2019-05-23 00:40:10 +03:00
|
|
|
DESC_WIDTH = 64 # 4
|
|
|
|
ARTICLE_WIDTH = 64 # 8
|
2019-05-17 18:44:11 +03:00
|
|
|
|
2019-05-21 00:54:55 +03:00
|
|
|
DROP = 0.1
|
2019-05-14 23:55:56 +03:00
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
name = "entity_linker"
|
|
|
|
|
|
|
|
def __init__(self, kb, nlp):
|
|
|
|
run_el._prepare_pipeline(nlp, kb)
|
|
|
|
self.nlp = nlp
|
|
|
|
self.kb = kb
|
|
|
|
|
2019-05-22 00:42:46 +03:00
|
|
|
self._build_cnn(in_width=self.INPUT_DIM,
|
2019-05-23 00:40:10 +03:00
|
|
|
desc_width=self.DESC_WIDTH,
|
2019-05-22 00:42:46 +03:00
|
|
|
article_width=self.ARTICLE_WIDTH,
|
|
|
|
hidden_1_width=self.HIDDEN_1_WIDTH,
|
|
|
|
hidden_2_width=self.HIDDEN_2_WIDTH)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-14 09:37:52 +03:00
|
|
|
def train_model(self, training_dir, entity_descr_output, trainlimit=None, devlimit=None, to_print=True):
|
2019-05-16 19:25:34 +03:00
|
|
|
# raise errors instead of runtime warnings in case of int/float overflow
|
|
|
|
np.seterr(all='raise')
|
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
train_ent, train_gold, train_desc, train_article, train_texts = self._get_training_data(training_dir,
|
|
|
|
entity_descr_output,
|
|
|
|
False,
|
|
|
|
trainlimit,
|
|
|
|
to_print=False)
|
|
|
|
|
|
|
|
train_pos_entities = [k for k,v in train_gold.items() if v]
|
|
|
|
train_neg_entities = [k for k,v in train_gold.items() if not v]
|
|
|
|
|
|
|
|
train_pos_count = len(train_pos_entities)
|
|
|
|
train_neg_count = len(train_neg_entities)
|
|
|
|
|
|
|
|
# upsample positives to 50-50 distribution
|
|
|
|
while train_pos_count < train_neg_count:
|
|
|
|
train_ent.append(random.choice(train_pos_entities))
|
|
|
|
train_pos_count += 1
|
|
|
|
|
|
|
|
# upsample negatives to 50-50 distribution
|
|
|
|
while train_neg_count < train_pos_count:
|
|
|
|
train_ent.append(random.choice(train_neg_entities))
|
|
|
|
train_neg_count += 1
|
|
|
|
|
|
|
|
shuffle(train_ent)
|
|
|
|
|
|
|
|
dev_ent, dev_gold, dev_desc, dev_article, dev_texts = self._get_training_data(training_dir,
|
|
|
|
entity_descr_output,
|
|
|
|
True,
|
|
|
|
devlimit,
|
|
|
|
to_print=False)
|
|
|
|
shuffle(dev_ent)
|
|
|
|
|
|
|
|
dev_pos_count = len([g for g in dev_gold.values() if g])
|
|
|
|
dev_neg_count = len([g for g in dev_gold.values() if not g])
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
self._begin_training()
|
|
|
|
|
2019-05-17 02:51:18 +03:00
|
|
|
print()
|
2019-05-23 00:40:10 +03:00
|
|
|
self._test_dev(dev_ent, dev_gold, dev_desc, dev_article, dev_texts, print_string="dev_random", calc_random=True)
|
|
|
|
print()
|
|
|
|
self._test_dev(dev_ent, dev_gold, dev_desc, dev_article, dev_texts, print_string="dev_pre", avg=True)
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
if to_print:
|
|
|
|
print()
|
2019-05-23 00:40:10 +03:00
|
|
|
print("Training on", len(train_ent), "entities in", len(train_texts), "articles")
|
|
|
|
print("Training instances pos/neg", train_pos_count, train_neg_count)
|
|
|
|
print()
|
|
|
|
print("Dev test on", len(dev_ent), "entities in", len(dev_texts), "articles")
|
|
|
|
print("Dev instances pos/neg", dev_pos_count, dev_neg_count)
|
2019-05-17 18:44:11 +03:00
|
|
|
print()
|
|
|
|
print(" CUTOFF", self.CUTOFF)
|
|
|
|
print(" INPUT_DIM", self.INPUT_DIM)
|
2019-05-22 00:42:46 +03:00
|
|
|
print(" HIDDEN_1_WIDTH", self.HIDDEN_1_WIDTH)
|
2019-05-23 00:40:10 +03:00
|
|
|
print(" DESC_WIDTH", self.DESC_WIDTH)
|
2019-05-17 18:44:11 +03:00
|
|
|
print(" ARTICLE_WIDTH", self.ARTICLE_WIDTH)
|
2019-05-22 00:42:46 +03:00
|
|
|
print(" HIDDEN_2_WIDTH", self.HIDDEN_2_WIDTH)
|
2019-05-17 18:44:11 +03:00
|
|
|
print(" DROP", self.DROP)
|
|
|
|
print()
|
2019-05-17 02:51:18 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
start = 0
|
|
|
|
stop = min(self.BATCH_SIZE, len(train_ent))
|
|
|
|
processed = 0
|
|
|
|
|
|
|
|
while start < len(train_ent):
|
|
|
|
next_batch = train_ent[start:stop]
|
|
|
|
|
|
|
|
golds = [train_gold[e] for e in next_batch]
|
|
|
|
descs = [train_desc[e] for e in next_batch]
|
|
|
|
articles = [train_texts[train_article[e]] for e in next_batch]
|
|
|
|
|
|
|
|
self.update(entities=next_batch, golds=golds, descs=descs, texts=articles)
|
|
|
|
self._test_dev(dev_ent, dev_gold, dev_desc, dev_article, dev_texts, print_string="dev_inter", avg=True)
|
|
|
|
|
|
|
|
processed += len(next_batch)
|
|
|
|
|
|
|
|
start = start + self.BATCH_SIZE
|
|
|
|
stop = min(stop + self.BATCH_SIZE, len(train_ent))
|
2019-05-13 18:02:34 +03:00
|
|
|
|
|
|
|
if to_print:
|
2019-05-17 02:51:18 +03:00
|
|
|
print()
|
2019-05-23 00:40:10 +03:00
|
|
|
print("Trained on", processed, "entities in total")
|
2019-05-21 14:43:59 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
def _test_dev(self, entities, gold_by_entity, desc_by_entity, article_by_entity, texts_by_id, print_string, avg=True, calc_random=False):
|
|
|
|
golds = [gold_by_entity[e] for e in entities]
|
|
|
|
|
|
|
|
if calc_random:
|
|
|
|
predictions = self._predict_random(entities=entities)
|
2019-05-17 02:51:18 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
else:
|
|
|
|
desc_docs = self.nlp.pipe([desc_by_entity[e] for e in entities])
|
|
|
|
article_docs = self.nlp.pipe([texts_by_id[article_by_entity[e]] for e in entities])
|
|
|
|
predictions = self._predict(entities=entities, article_docs=article_docs, desc_docs=desc_docs, avg=avg)
|
|
|
|
|
|
|
|
# TODO: combine with prior probability
|
|
|
|
p, r, f, acc = run_el.evaluate(predictions, golds, to_print=False)
|
2019-05-22 13:46:40 +03:00
|
|
|
loss, gradient = self.get_loss(self.model.ops.asarray(predictions), self.model.ops.asarray(golds))
|
2019-05-23 00:40:10 +03:00
|
|
|
|
|
|
|
print("p/r/F/acc/loss", print_string, round(p, 1), round(r, 1), round(f, 1), round(acc, 2), round(loss, 5))
|
2019-05-17 02:51:18 +03:00
|
|
|
|
|
|
|
return loss, p, r, f
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
def _predict(self, entities, article_docs, desc_docs, avg=True, apply_threshold=True):
|
2019-05-16 19:25:34 +03:00
|
|
|
if avg:
|
2019-05-17 18:44:11 +03:00
|
|
|
with self.article_encoder.use_params(self.sgd_article.averages) \
|
2019-05-23 00:40:10 +03:00
|
|
|
and self.desc_encoder.use_params(self.sgd_entity.averages):
|
|
|
|
doc_encodings = self.article_encoder(article_docs)
|
|
|
|
desc_encodings = self.desc_encoder(desc_docs)
|
2019-05-17 18:44:11 +03:00
|
|
|
|
|
|
|
else:
|
2019-05-23 00:40:10 +03:00
|
|
|
doc_encodings = self.article_encoder(article_docs)
|
|
|
|
desc_encodings = self.desc_encoder(desc_docs)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
concat_encodings = [list(desc_encodings[i]) + list(doc_encodings[i]) for i in range(len(entities))]
|
2019-05-21 14:43:59 +03:00
|
|
|
np_array_list = np.asarray(concat_encodings)
|
2019-05-17 18:44:11 +03:00
|
|
|
|
|
|
|
if avg:
|
2019-05-21 14:43:59 +03:00
|
|
|
with self.model.use_params(self.sgd.averages):
|
|
|
|
predictions = self.model(np_array_list)
|
2019-05-17 18:44:11 +03:00
|
|
|
else:
|
2019-05-21 14:43:59 +03:00
|
|
|
predictions = self.model(np_array_list)
|
2019-05-17 18:44:11 +03:00
|
|
|
|
2019-05-21 14:43:59 +03:00
|
|
|
predictions = self.model.ops.flatten(predictions)
|
|
|
|
predictions = [float(p) for p in predictions]
|
|
|
|
if apply_threshold:
|
|
|
|
predictions = [float(1.0) if p > self.CUTOFF else float(0.0) for p in predictions]
|
|
|
|
|
|
|
|
return predictions
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-21 14:43:59 +03:00
|
|
|
def _predict_random(self, entities, apply_threshold=True):
|
2019-05-16 19:25:34 +03:00
|
|
|
if not apply_threshold:
|
2019-05-23 00:40:10 +03:00
|
|
|
return [float(random.uniform(0, 1)) for e in entities]
|
2019-05-21 14:43:59 +03:00
|
|
|
else:
|
2019-05-23 00:40:10 +03:00
|
|
|
return [float(1.0) if random.uniform(0, 1) > self.CUTOFF else float(0.0) for e in entities]
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
def _build_cnn(self, in_width, desc_width, article_width, hidden_1_width, hidden_2_width):
|
2019-05-16 19:25:34 +03:00
|
|
|
with Model.define_operators({">>": chain, "|": concatenate, "**": clone}):
|
2019-05-23 00:40:10 +03:00
|
|
|
self.desc_encoder = self._encoder(in_width=in_width, hidden_with=hidden_1_width, end_width=desc_width)
|
2019-05-22 00:42:46 +03:00
|
|
|
self.article_encoder = self._encoder(in_width=in_width, hidden_with=hidden_1_width, end_width=article_width)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
in_width = desc_width + article_width
|
2019-05-22 00:42:46 +03:00
|
|
|
out_width = hidden_2_width
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-22 00:42:46 +03:00
|
|
|
self.model = Affine(out_width, in_width) \
|
|
|
|
>> LN(Maxout(out_width, out_width)) \
|
|
|
|
>> Affine(1, out_width) \
|
2019-05-17 02:51:18 +03:00
|
|
|
>> logistic
|
2019-05-16 19:25:34 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2019-05-22 00:42:46 +03:00
|
|
|
def _encoder(in_width, hidden_with, end_width):
|
2019-05-20 18:20:39 +03:00
|
|
|
conv_depth = 2
|
2019-05-20 12:58:48 +03:00
|
|
|
cnn_maxout_pieces = 3
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
with Model.define_operators({">>": chain}):
|
2019-05-22 00:42:46 +03:00
|
|
|
convolution = Residual((ExtractWindow(nW=1) >> LN(Maxout(hidden_with, hidden_with * 3, pieces=cnn_maxout_pieces))))
|
2019-05-20 12:58:48 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
encoder = SpacyVectors \
|
2019-05-22 00:42:46 +03:00
|
|
|
>> with_flatten(LN(Maxout(hidden_with, in_width)) >> convolution ** conv_depth, pad=conv_depth) \
|
2019-05-20 12:58:48 +03:00
|
|
|
>> flatten_add_lengths \
|
2019-05-22 00:42:46 +03:00
|
|
|
>> ParametricAttention(hidden_with)\
|
2019-05-20 12:58:48 +03:00
|
|
|
>> Pooling(mean_pool) \
|
2019-05-22 00:42:46 +03:00
|
|
|
>> Residual(zero_init(Maxout(hidden_with, hidden_with))) \
|
|
|
|
>> zero_init(Affine(end_width, hidden_with, drop_factor=0.0))
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-21 00:54:55 +03:00
|
|
|
# TODO: ReLu or LN(Maxout) ?
|
2019-05-20 12:58:48 +03:00
|
|
|
# sum_pool or mean_pool ?
|
2019-05-17 02:51:18 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
return encoder
|
|
|
|
|
|
|
|
def _begin_training(self):
|
2019-05-17 18:44:11 +03:00
|
|
|
self.sgd_article = create_default_optimizer(self.article_encoder.ops)
|
2019-05-23 00:40:10 +03:00
|
|
|
self.sgd_entity = create_default_optimizer(self.desc_encoder.ops)
|
2019-05-16 19:25:34 +03:00
|
|
|
self.sgd = create_default_optimizer(self.model.ops)
|
|
|
|
|
2019-05-17 02:51:18 +03:00
|
|
|
@staticmethod
|
|
|
|
def get_loss(predictions, golds):
|
|
|
|
d_scores = (predictions - golds)
|
2019-05-23 00:40:10 +03:00
|
|
|
gradient = d_scores.mean()
|
2019-05-22 13:46:40 +03:00
|
|
|
loss = (d_scores ** 2).mean()
|
2019-05-23 00:40:10 +03:00
|
|
|
return loss, gradient
|
2019-05-17 02:51:18 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
def update(self, entities, golds, descs, texts):
|
|
|
|
golds = self.model.ops.asarray(golds)
|
2019-05-21 14:43:59 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
desc_docs = self.nlp.pipe(descs)
|
|
|
|
article_docs = self.nlp.pipe(texts)
|
2019-05-20 12:58:48 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
doc_encodings, bp_doc = self.article_encoder.begin_update(article_docs, drop=self.DROP)
|
2019-05-20 12:58:48 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
desc_encodings, bp_entity = self.desc_encoder.begin_update(desc_docs, drop=self.DROP)
|
2019-05-17 18:44:11 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
concat_encodings = [list(desc_encodings[i]) + list(doc_encodings[i]) for i in range(len(entities))]
|
2019-05-21 14:43:59 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
predictions, bp_model = self.model.begin_update(np.asarray(concat_encodings), drop=self.DROP)
|
|
|
|
predictions = self.model.ops.flatten(predictions)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
# print("entities", entities)
|
|
|
|
# print("predictions", predictions)
|
|
|
|
# print("golds", golds)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
loss, gradient = self.get_loss(predictions, golds)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
if self.PRINT_TRAIN:
|
|
|
|
print("loss train", round(loss, 5))
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
gradient = float(gradient)
|
|
|
|
# print("gradient", gradient)
|
|
|
|
# print("loss", loss)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
model_gradient = bp_model(gradient, sgd=self.sgd)
|
|
|
|
# print("model_gradient", model_gradient)
|
2019-05-20 18:20:39 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
# concat = desc + doc, but doc is the same within this function (TODO: multiple docs/articles)
|
|
|
|
doc_gradient = model_gradient[0][self.DESC_WIDTH:]
|
|
|
|
entity_gradients = list()
|
|
|
|
for x in model_gradient:
|
|
|
|
entity_gradients.append(list(x[0:self.DESC_WIDTH]))
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
# print("doc_gradient", doc_gradient)
|
|
|
|
# print("entity_gradients", entity_gradients)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
bp_doc([doc_gradient], sgd=self.sgd_article)
|
|
|
|
bp_entity(entity_gradients, sgd=self.sgd_entity)
|
2019-05-16 19:25:34 +03:00
|
|
|
|
2019-05-23 00:40:10 +03:00
|
|
|
def _get_training_data(self, training_dir, entity_descr_output, dev, limit, to_print):
|
2019-05-09 18:23:19 +03:00
|
|
|
id_to_descr = kb_creator._get_id_to_description(entity_descr_output)
|
|
|
|
|
|
|
|
correct_entries, incorrect_entries = training_set_creator.read_training_entities(training_output=training_dir,
|
|
|
|
collect_correct=True,
|
|
|
|
collect_incorrect=True)
|
|
|
|
|
|
|
|
local_vectors = list() # TODO: local vectors
|
2019-05-21 14:43:59 +03:00
|
|
|
text_by_article = dict()
|
2019-05-23 00:40:10 +03:00
|
|
|
gold_by_entity = dict()
|
|
|
|
desc_by_entity = dict()
|
|
|
|
article_by_entity = dict()
|
|
|
|
entities = list()
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
cnt = 0
|
2019-05-23 00:40:10 +03:00
|
|
|
next_entity_nr = 0
|
|
|
|
files = listdir(training_dir)
|
|
|
|
shuffle(files)
|
|
|
|
for f in files:
|
2019-05-09 18:23:19 +03:00
|
|
|
if not limit or cnt < limit:
|
2019-05-13 15:26:04 +03:00
|
|
|
if dev == run_el.is_dev(f):
|
2019-05-09 18:23:19 +03:00
|
|
|
article_id = f.replace(".txt", "")
|
|
|
|
if cnt % 500 == 0 and to_print:
|
2019-05-13 18:02:34 +03:00
|
|
|
print(datetime.datetime.now(), "processed", cnt, "files in the training dataset")
|
2019-05-09 18:23:19 +03:00
|
|
|
cnt += 1
|
2019-05-21 14:43:59 +03:00
|
|
|
if article_id not in text_by_article:
|
2019-05-09 18:23:19 +03:00
|
|
|
with open(os.path.join(training_dir, f), mode="r", encoding='utf8') as file:
|
|
|
|
text = file.read()
|
2019-05-21 14:43:59 +03:00
|
|
|
text_by_article[article_id] = text
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 19:11:49 +03:00
|
|
|
for mention, entity_pos in correct_entries[article_id].items():
|
2019-05-07 17:03:42 +03:00
|
|
|
descr = id_to_descr.get(entity_pos)
|
|
|
|
if descr:
|
2019-05-23 00:40:10 +03:00
|
|
|
entities.append(next_entity_nr)
|
|
|
|
gold_by_entity[next_entity_nr] = 1
|
|
|
|
desc_by_entity[next_entity_nr] = descr
|
|
|
|
article_by_entity[next_entity_nr] = article_id
|
|
|
|
next_entity_nr += 1
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 19:11:49 +03:00
|
|
|
for mention, entity_negs in incorrect_entries[article_id].items():
|
2019-05-23 00:40:10 +03:00
|
|
|
for entity_neg in entity_negs:
|
|
|
|
descr = id_to_descr.get(entity_neg)
|
|
|
|
if descr:
|
|
|
|
entities.append(next_entity_nr)
|
|
|
|
gold_by_entity[next_entity_nr] = 0
|
|
|
|
desc_by_entity[next_entity_nr] = descr
|
|
|
|
article_by_entity[next_entity_nr] = article_id
|
|
|
|
next_entity_nr += 1
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
if to_print:
|
|
|
|
print()
|
2019-05-13 18:02:34 +03:00
|
|
|
print("Processed", cnt, "training articles, dev=" + str(dev))
|
2019-05-09 18:23:19 +03:00
|
|
|
print()
|
2019-05-23 00:40:10 +03:00
|
|
|
return entities, gold_by_entity, desc_by_entity, article_by_entity, text_by_article
|
|
|
|
|