2020-07-22 11:42:59 +00:00
|
|
|
from typing import List, Iterable, Optional, Dict, Tuple, Callable
|
2020-05-18 20:23:33 +00:00
|
|
|
from thinc.types import Floats2d
|
2020-07-22 11:42:59 +00:00
|
|
|
from thinc.api import SequenceCategoricalCrossentropy, set_dropout_rate, Model
|
|
|
|
from thinc.api import Optimizer, Config
|
2020-05-18 20:23:33 +00:00
|
|
|
from thinc.util import to_numpy
|
2020-05-19 14:20:03 +00:00
|
|
|
|
2020-08-05 12:43:29 +00:00
|
|
|
from ..errors import Errors
|
2020-05-18 20:23:33 +00:00
|
|
|
from ..gold import Example, spans_from_biluo_tags, iob_to_biluo, biluo_to_iob
|
|
|
|
from ..tokens import Doc
|
2020-07-22 11:42:59 +00:00
|
|
|
from ..language import Language
|
|
|
|
from ..vocab import Vocab
|
2020-07-27 13:08:51 +00:00
|
|
|
from ..scorer import Scorer
|
2020-07-22 11:42:59 +00:00
|
|
|
from .pipe import Pipe
|
|
|
|
|
|
|
|
|
|
|
|
default_model_config = """
|
|
|
|
[model]
|
|
|
|
@architectures = "spacy.BiluoTagger.v1"
|
|
|
|
|
|
|
|
[model.tok2vec]
|
|
|
|
@architectures = "spacy.HashEmbedCNN.v1"
|
|
|
|
pretrained_vectors = null
|
|
|
|
width = 128
|
|
|
|
depth = 4
|
|
|
|
embed_size = 7000
|
|
|
|
window_size = 1
|
|
|
|
maxout_pieces = 3
|
|
|
|
subword_features = true
|
|
|
|
dropout = null
|
|
|
|
"""
|
|
|
|
DEFAULT_SIMPLE_NER_MODEL = Config().from_str(default_model_config)["model"]
|
|
|
|
|
|
|
|
|
|
|
|
@Language.factory(
|
|
|
|
"simple_ner",
|
|
|
|
assigns=["doc.ents"],
|
|
|
|
default_config={"labels": [], "model": DEFAULT_SIMPLE_NER_MODEL},
|
2020-07-27 10:27:40 +00:00
|
|
|
scores=["ents_p", "ents_r", "ents_f", "ents_per_type"],
|
|
|
|
default_score_weights={"ents_f": 1.0, "ents_p": 0.0, "ents_r": 0.0},
|
2020-07-22 11:42:59 +00:00
|
|
|
)
|
|
|
|
def make_simple_ner(
|
|
|
|
nlp: Language, name: str, model: Model, labels: Iterable[str]
|
|
|
|
) -> "SimpleNER":
|
|
|
|
return SimpleNER(nlp.vocab, model, name, labels=labels)
|
2020-05-18 20:23:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SimpleNER(Pipe):
|
|
|
|
"""Named entity recognition with a tagging model. The model should include
|
|
|
|
validity constraints to ensure that only valid tag sequences are returned."""
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
vocab: Vocab,
|
|
|
|
model: Model,
|
|
|
|
name: str = "simple_ner",
|
|
|
|
*,
|
|
|
|
labels: Iterable[str],
|
|
|
|
) -> None:
|
2020-05-18 20:23:33 +00:00
|
|
|
self.vocab = vocab
|
|
|
|
self.model = model
|
2020-07-22 11:42:59 +00:00
|
|
|
self.name = name
|
2020-08-05 12:43:29 +00:00
|
|
|
self.cfg = {"labels": []}
|
|
|
|
for label in labels:
|
|
|
|
self.add_label(label)
|
2020-05-18 20:23:33 +00:00
|
|
|
self.loss_func = SequenceCategoricalCrossentropy(
|
2020-06-20 12:15:04 +00:00
|
|
|
names=self.get_tag_names(), normalize=True, missing_value=None
|
2020-05-18 20:23:33 +00:00
|
|
|
)
|
|
|
|
assert self.model is not None
|
|
|
|
|
|
|
|
@property
|
2020-07-22 11:42:59 +00:00
|
|
|
def is_biluo(self) -> bool:
|
2020-05-18 20:23:33 +00:00
|
|
|
return self.model.name.startswith("biluo")
|
|
|
|
|
2020-08-05 12:43:29 +00:00
|
|
|
@property
|
|
|
|
def labels(self) -> Tuple[str]:
|
|
|
|
return tuple(self.cfg["labels"])
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def add_label(self, label: str) -> None:
|
2020-08-05 12:43:29 +00:00
|
|
|
"""Add a new label to the pipe.
|
|
|
|
label (str): The label to add.
|
|
|
|
DOCS: https://spacy.io/api/simplener#add_label
|
|
|
|
"""
|
|
|
|
if not isinstance(label, str):
|
|
|
|
raise ValueError(Errors.E187)
|
2020-07-22 11:42:59 +00:00
|
|
|
if label not in self.labels:
|
2020-08-05 12:43:29 +00:00
|
|
|
self.cfg["labels"].append(label)
|
|
|
|
self.vocab.strings.add(label)
|
2020-06-20 12:15:04 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def get_tag_names(self) -> List[str]:
|
2020-05-18 20:23:33 +00:00
|
|
|
if self.is_biluo:
|
|
|
|
return (
|
2020-06-20 12:15:04 +00:00
|
|
|
[f"B-{label}" for label in self.labels]
|
|
|
|
+ [f"I-{label}" for label in self.labels]
|
|
|
|
+ [f"L-{label}" for label in self.labels]
|
|
|
|
+ [f"U-{label}" for label in self.labels]
|
|
|
|
+ ["O"]
|
2020-05-18 20:23:33 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return (
|
2020-06-20 12:15:04 +00:00
|
|
|
[f"B-{label}" for label in self.labels]
|
|
|
|
+ [f"I-{label}" for label in self.labels]
|
|
|
|
+ ["O"]
|
2020-05-18 20:23:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def predict(self, docs: List[Doc]) -> List[Floats2d]:
|
|
|
|
scores = self.model.predict(docs)
|
|
|
|
return scores
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def set_annotations(self, docs: List[Doc], scores: List[Floats2d]) -> None:
|
2020-05-18 20:23:33 +00:00
|
|
|
"""Set entities on a batch of documents from a batch of scores."""
|
|
|
|
tag_names = self.get_tag_names()
|
|
|
|
for i, doc in enumerate(docs):
|
|
|
|
actions = to_numpy(scores[i].argmax(axis=1))
|
|
|
|
tags = [tag_names[actions[j]] for j in range(len(doc))]
|
|
|
|
if not self.is_biluo:
|
|
|
|
tags = iob_to_biluo(tags)
|
|
|
|
doc.ents = spans_from_biluo_tags(doc, tags)
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
examples: List[Example],
|
|
|
|
*,
|
|
|
|
set_annotations: bool = False,
|
|
|
|
drop: float = 0.0,
|
|
|
|
sgd: Optional[Optimizer] = None,
|
|
|
|
losses: Optional[Dict[str, float]] = None,
|
|
|
|
) -> Dict[str, float]:
|
2020-07-09 17:43:39 +00:00
|
|
|
if losses is None:
|
|
|
|
losses = {}
|
|
|
|
losses.setdefault("ner", 0.0)
|
2020-05-18 20:23:33 +00:00
|
|
|
if not any(_has_ner(eg) for eg in examples):
|
2020-07-09 17:43:39 +00:00
|
|
|
return losses
|
2020-06-29 12:33:00 +00:00
|
|
|
docs = [eg.predicted for eg in examples]
|
2020-05-18 20:23:33 +00:00
|
|
|
set_dropout_rate(self.model, drop)
|
|
|
|
scores, bp_scores = self.model.begin_update(docs)
|
|
|
|
loss, d_scores = self.get_loss(examples, scores)
|
|
|
|
bp_scores(d_scores)
|
|
|
|
if set_annotations:
|
|
|
|
self.set_annotations(docs, scores)
|
|
|
|
if sgd is not None:
|
|
|
|
self.model.finish_update(sgd)
|
2020-07-09 17:43:39 +00:00
|
|
|
losses["ner"] += loss
|
|
|
|
return losses
|
2020-05-18 20:23:33 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def get_loss(self, examples: List[Example], scores) -> Tuple[List[Floats2d], float]:
|
2020-05-18 20:23:33 +00:00
|
|
|
truths = []
|
|
|
|
for eg in examples:
|
2020-08-05 12:43:29 +00:00
|
|
|
tags = eg.get_aligned_ner()
|
2020-06-29 12:33:00 +00:00
|
|
|
gold_tags = [(tag if tag != "-" else None) for tag in tags]
|
2020-05-18 20:23:33 +00:00
|
|
|
if not self.is_biluo:
|
|
|
|
gold_tags = biluo_to_iob(gold_tags)
|
|
|
|
truths.append(gold_tags)
|
|
|
|
for i in range(len(scores)):
|
|
|
|
if len(scores[i]) != len(truths[i]):
|
|
|
|
raise ValueError(
|
|
|
|
f"Mismatched output and gold sizes.\n"
|
|
|
|
f"Output: {len(scores[i])}, gold: {len(truths[i])}."
|
|
|
|
f"Input: {len(examples[i].doc)}"
|
|
|
|
)
|
|
|
|
d_scores, loss = self.loss_func(scores, truths)
|
|
|
|
return loss, d_scores
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def begin_training(
|
|
|
|
self,
|
|
|
|
get_examples: Callable,
|
|
|
|
pipeline: Optional[List[Tuple[str, Callable[[Doc], Doc]]]] = None,
|
|
|
|
sgd: Optional[Optimizer] = None,
|
|
|
|
):
|
2020-06-20 12:15:04 +00:00
|
|
|
if not hasattr(get_examples, "__call__"):
|
2020-05-18 20:23:33 +00:00
|
|
|
gold_tuples = get_examples
|
|
|
|
get_examples = lambda: gold_tuples
|
|
|
|
for label in _get_labels(get_examples()):
|
|
|
|
self.add_label(label)
|
|
|
|
labels = self.labels
|
|
|
|
n_actions = self.model.attrs["get_num_actions"](len(labels))
|
|
|
|
self.model.set_dim("nO", n_actions)
|
2020-06-20 12:15:04 +00:00
|
|
|
self.model.initialize()
|
2020-05-18 20:23:33 +00:00
|
|
|
if pipeline is not None:
|
|
|
|
self.init_multitask_objectives(get_examples, pipeline, sgd=sgd, **self.cfg)
|
|
|
|
self.loss_func = SequenceCategoricalCrossentropy(
|
2020-06-20 12:15:04 +00:00
|
|
|
names=self.get_tag_names(), normalize=True, missing_value=None
|
2020-05-18 20:23:33 +00:00
|
|
|
)
|
|
|
|
return sgd
|
|
|
|
|
|
|
|
def init_multitask_objectives(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2020-07-27 10:27:40 +00:00
|
|
|
def score(self, examples, **kwargs):
|
|
|
|
return Scorer.score_spans(examples, "ents", **kwargs)
|
|
|
|
|
2020-05-18 20:23:33 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def _has_ner(example: Example) -> bool:
|
2020-06-29 12:33:00 +00:00
|
|
|
for ner_tag in example.get_aligned_ner():
|
2020-06-20 12:15:04 +00:00
|
|
|
if ner_tag != "-" and ner_tag is not None:
|
2020-05-18 20:23:33 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def _get_labels(examples: List[Example]) -> List[str]:
|
2020-05-18 20:23:33 +00:00
|
|
|
labels = set()
|
|
|
|
for eg in examples:
|
2020-06-26 17:34:12 +00:00
|
|
|
for ner_tag in eg.get_aligned("ENT_TYPE", as_string=True):
|
2020-06-20 12:15:04 +00:00
|
|
|
if ner_tag != "O" and ner_tag != "-":
|
2020-06-26 17:34:12 +00:00
|
|
|
labels.add(ner_tag)
|
2020-05-18 20:23:33 +00:00
|
|
|
return list(sorted(labels))
|