mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 01:48:04 +03:00 
			
		
		
		
	* Refactor Docs.is_ flags
* Add derived `Doc.has_annotation` method
  * `Doc.has_annotation(attr)` returns `True` for partial annotation
  * `Doc.has_annotation(attr, require_complete=True)` returns `True` for
    complete annotation
* Add deprecation warnings to `is_tagged`, `is_parsed`, `is_sentenced`
and `is_nered`
* Add `Doc._get_array_attrs()`, which returns a full list of `Doc` attrs
for use with `Doc.to_array`, `Doc.to_bytes` and `Doc.from_docs`. The
list is the `DocBin` attributes list plus `SPACY` and `LENGTH`.
Notes on `Doc.has_annotation`:
* `HEAD` is converted to `DEP` because heads don't have an unset state
* Accept `IS_SENT_START` as a synonym of `SENT_START`
Additional changes:
* Add `NORM`, `ENT_ID` and `SENT_START` to default attributes for
`DocBin`
* In `Doc.from_array()` the presence of `DEP` causes `HEAD` to override
`SENT_START`
* In `Doc.from_array()` using `attrs` other than
`Doc._get_array_attrs()` (i.e., a user's custom list rather than our
default internal list) with both `HEAD` and `SENT_START` shows a warning
that `HEAD` will override `SENT_START`
* `set_children_from_heads` does not require dependency labels to set
sentence boundaries and sets `sent_start` for all non-sentence starts to
`-1`
* Fix call to set_children_form_heads
Co-authored-by: Matthew Honnibal <honnibal+gh@gmail.com>
		
	
			
		
			
				
	
	
		
			261 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			Cython
		
	
	
	
	
	
			
		
		
	
	
			261 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			Cython
		
	
	
	
	
	
# cython: infer_types=True, profile=True, binding=True
 | 
						|
from typing import Optional
 | 
						|
import srsly
 | 
						|
from thinc.api import SequenceCategoricalCrossentropy, Model, Config
 | 
						|
from itertools import islice
 | 
						|
 | 
						|
from ..tokens.doc cimport Doc
 | 
						|
from ..vocab cimport Vocab
 | 
						|
from ..morphology cimport Morphology
 | 
						|
 | 
						|
from ..parts_of_speech import IDS as POS_IDS
 | 
						|
from ..symbols import POS
 | 
						|
from ..language import Language
 | 
						|
from ..errors import Errors
 | 
						|
from .pipe import deserialize_config
 | 
						|
from .tagger import Tagger
 | 
						|
from .. import util
 | 
						|
from ..scorer import Scorer
 | 
						|
from ..training import validate_examples
 | 
						|
 | 
						|
 | 
						|
default_model_config = """
 | 
						|
[model]
 | 
						|
@architectures = "spacy.Tagger.v1"
 | 
						|
 | 
						|
[model.tok2vec]
 | 
						|
@architectures = "spacy.Tok2Vec.v1"
 | 
						|
 | 
						|
[model.tok2vec.embed]
 | 
						|
@architectures = "spacy.CharacterEmbed.v1"
 | 
						|
width = 128
 | 
						|
rows = 7000
 | 
						|
nM = 64
 | 
						|
nC = 8
 | 
						|
also_use_static_vectors = false
 | 
						|
 | 
						|
[model.tok2vec.encode]
 | 
						|
@architectures = "spacy.MaxoutWindowEncoder.v1"
 | 
						|
width = 128
 | 
						|
depth = 4
 | 
						|
window_size = 1
 | 
						|
maxout_pieces = 3
 | 
						|
"""
 | 
						|
 | 
						|
DEFAULT_MORPH_MODEL = Config().from_str(default_model_config)["model"]
 | 
						|
 | 
						|
 | 
						|
@Language.factory(
 | 
						|
    "morphologizer",
 | 
						|
    assigns=["token.morph", "token.pos"],
 | 
						|
    default_config={"model": DEFAULT_MORPH_MODEL},
 | 
						|
    scores=["pos_acc", "morph_acc", "morph_per_feat"],
 | 
						|
    default_score_weights={"pos_acc": 0.5, "morph_acc": 0.5},
 | 
						|
)
 | 
						|
def make_morphologizer(
 | 
						|
    nlp: Language,
 | 
						|
    model: Model,
 | 
						|
    name: str,
 | 
						|
):
 | 
						|
    return Morphologizer(nlp.vocab, model, name)
 | 
						|
 | 
						|
 | 
						|
class Morphologizer(Tagger):
 | 
						|
    POS_FEAT = "POS"
 | 
						|
 | 
						|
    def __init__(
 | 
						|
        self,
 | 
						|
        vocab: Vocab,
 | 
						|
        model: Model,
 | 
						|
        name: str = "morphologizer",
 | 
						|
        *,
 | 
						|
        labels_morph: Optional[dict] = None,
 | 
						|
        labels_pos: Optional[dict] = None,
 | 
						|
    ):
 | 
						|
        """Initialize a morphologizer.
 | 
						|
 | 
						|
        vocab (Vocab): The shared vocabulary.
 | 
						|
        model (thinc.api.Model): The Thinc Model powering the pipeline component.
 | 
						|
        name (str): The component instance name, used to add entries to the
 | 
						|
            losses during training.
 | 
						|
        labels_morph (dict): Mapping of morph + POS tags to morph labels.
 | 
						|
        labels_pos (dict): Mapping of morph + POS tags to POS tags.
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#init
 | 
						|
        """
 | 
						|
        self.vocab = vocab
 | 
						|
        self.model = model
 | 
						|
        self.name = name
 | 
						|
        self._rehearsal_model = None
 | 
						|
        # to be able to set annotations without string operations on labels,
 | 
						|
        # store mappings from morph+POS labels to token-level annotations:
 | 
						|
        # 1) labels_morph stores a mapping from morph+POS->morph
 | 
						|
        # 2) labels_pos stores a mapping from morph+POS->POS
 | 
						|
        cfg = {"labels_morph": labels_morph or {}, "labels_pos": labels_pos or {}}
 | 
						|
        self.cfg = dict(sorted(cfg.items()))
 | 
						|
        # add mappings for empty morph
 | 
						|
        self.cfg["labels_morph"][Morphology.EMPTY_MORPH] = Morphology.EMPTY_MORPH
 | 
						|
        self.cfg["labels_pos"][Morphology.EMPTY_MORPH] = POS_IDS[""]
 | 
						|
 | 
						|
    @property
 | 
						|
    def labels(self):
 | 
						|
        """RETURNS (Tuple[str]): The labels currently added to the component."""
 | 
						|
        return tuple(self.cfg["labels_morph"].keys())
 | 
						|
 | 
						|
    def add_label(self, label):
 | 
						|
        """Add a new label to the pipe.
 | 
						|
 | 
						|
        label (str): The label to add.
 | 
						|
        RETURNS (int): 0 if label is already present, otherwise 1.
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#add_label
 | 
						|
        """
 | 
						|
        if not isinstance(label, str):
 | 
						|
            raise ValueError(Errors.E187)
 | 
						|
        if label in self.labels:
 | 
						|
            return 0
 | 
						|
        self._allow_extra_label()
 | 
						|
        # normalize label
 | 
						|
        norm_label = self.vocab.morphology.normalize_features(label)
 | 
						|
        # extract separate POS and morph tags
 | 
						|
        label_dict = Morphology.feats_to_dict(label)
 | 
						|
        pos = label_dict.get(self.POS_FEAT, "")
 | 
						|
        if self.POS_FEAT in label_dict:
 | 
						|
            label_dict.pop(self.POS_FEAT)
 | 
						|
        # normalize morph string and add to morphology table
 | 
						|
        norm_morph = self.vocab.strings[self.vocab.morphology.add(label_dict)]
 | 
						|
        # add label mappings
 | 
						|
        if norm_label not in self.cfg["labels_morph"]:
 | 
						|
            self.cfg["labels_morph"][norm_label] = norm_morph
 | 
						|
            self.cfg["labels_pos"][norm_label] = POS_IDS[pos]
 | 
						|
        return 1
 | 
						|
 | 
						|
    def begin_training(self, get_examples, *, pipeline=None, sgd=None):
 | 
						|
        """Initialize the pipe for training, using a representative set
 | 
						|
        of data examples.
 | 
						|
 | 
						|
        get_examples (Callable[[], Iterable[Example]]): Function that
 | 
						|
            returns a representative sample of gold-standard Example objects.
 | 
						|
        pipeline (List[Tuple[str, Callable]]): Optional list of pipeline
 | 
						|
            components that this component is part of. Corresponds to
 | 
						|
            nlp.pipeline.
 | 
						|
        sgd (thinc.api.Optimizer): Optional optimizer. Will be created with
 | 
						|
            create_optimizer if it doesn't exist.
 | 
						|
        RETURNS (thinc.api.Optimizer): The optimizer.
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#begin_training
 | 
						|
        """
 | 
						|
        self._ensure_examples(get_examples)
 | 
						|
        # First, fetch all labels from the data
 | 
						|
        for example in get_examples():
 | 
						|
            for i, token in enumerate(example.reference):
 | 
						|
                pos = token.pos_
 | 
						|
                morph = token.morph_
 | 
						|
                # create and add the combined morph+POS label
 | 
						|
                morph_dict = Morphology.feats_to_dict(morph)
 | 
						|
                if pos:
 | 
						|
                    morph_dict[self.POS_FEAT] = pos
 | 
						|
                norm_label = self.vocab.strings[self.vocab.morphology.add(morph_dict)]
 | 
						|
                # add label->morph and label->POS mappings
 | 
						|
                if norm_label not in self.cfg["labels_morph"]:
 | 
						|
                    self.cfg["labels_morph"][norm_label] = morph
 | 
						|
                    self.cfg["labels_pos"][norm_label] = POS_IDS[pos]
 | 
						|
        if len(self.labels) <= 1:
 | 
						|
            raise ValueError(Errors.E143.format(name=self.name))
 | 
						|
        doc_sample = []
 | 
						|
        label_sample = []
 | 
						|
        for example in islice(get_examples(), 10):
 | 
						|
            gold_array = []
 | 
						|
            for i, token in enumerate(example.reference):
 | 
						|
                pos = token.pos_
 | 
						|
                morph = token.morph_
 | 
						|
                morph_dict = Morphology.feats_to_dict(morph)
 | 
						|
                if pos:
 | 
						|
                    morph_dict[self.POS_FEAT] = pos
 | 
						|
                norm_label = self.vocab.strings[self.vocab.morphology.add(morph_dict)]
 | 
						|
                gold_array.append([1.0 if label == norm_label else 0.0 for label in self.labels])
 | 
						|
            doc_sample.append(example.x)
 | 
						|
            label_sample.append(self.model.ops.asarray(gold_array, dtype="float32"))
 | 
						|
        assert len(doc_sample) > 0, Errors.E923.format(name=self.name)
 | 
						|
        assert len(label_sample) > 0, Errors.E923.format(name=self.name)
 | 
						|
        self.model.initialize(X=doc_sample, Y=label_sample)
 | 
						|
        if sgd is None:
 | 
						|
            sgd = self.create_optimizer()
 | 
						|
        return sgd
 | 
						|
 | 
						|
    def set_annotations(self, docs, batch_tag_ids):
 | 
						|
        """Modify a batch of documents, using pre-computed scores.
 | 
						|
 | 
						|
        docs (Iterable[Doc]): The documents to modify.
 | 
						|
        batch_tag_ids: The IDs to set, produced by Morphologizer.predict.
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#set_annotations
 | 
						|
        """
 | 
						|
        if isinstance(docs, Doc):
 | 
						|
            docs = [docs]
 | 
						|
        cdef Doc doc
 | 
						|
        cdef Vocab vocab = self.vocab
 | 
						|
        for i, doc in enumerate(docs):
 | 
						|
            doc_tag_ids = batch_tag_ids[i]
 | 
						|
            if hasattr(doc_tag_ids, "get"):
 | 
						|
                doc_tag_ids = doc_tag_ids.get()
 | 
						|
            for j, tag_id in enumerate(doc_tag_ids):
 | 
						|
                morph = self.labels[tag_id]
 | 
						|
                doc.c[j].morph = self.vocab.morphology.add(self.cfg["labels_morph"][morph])
 | 
						|
                doc.c[j].pos = self.cfg["labels_pos"][morph]
 | 
						|
 | 
						|
    def get_loss(self, examples, scores):
 | 
						|
        """Find the loss and gradient of loss for the batch of documents and
 | 
						|
        their predicted scores.
 | 
						|
 | 
						|
        examples (Iterable[Examples]): The batch of examples.
 | 
						|
        scores: Scores representing the model's predictions.
 | 
						|
        RETUTNRS (Tuple[float, float]): The loss and the gradient.
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#get_loss
 | 
						|
        """
 | 
						|
        validate_examples(examples, "Morphologizer.get_loss")
 | 
						|
        loss_func = SequenceCategoricalCrossentropy(names=self.labels, normalize=False)
 | 
						|
        truths = []
 | 
						|
        for eg in examples:
 | 
						|
            eg_truths = []
 | 
						|
            pos_tags = eg.get_aligned("POS", as_string=True)
 | 
						|
            morphs = eg.get_aligned("MORPH", as_string=True)
 | 
						|
            for i in range(len(morphs)):
 | 
						|
                pos = pos_tags[i]
 | 
						|
                morph = morphs[i]
 | 
						|
                # POS may align (same value for multiple tokens) when morph
 | 
						|
                # doesn't, so if either is None, treat both as None here so that
 | 
						|
                # truths doesn't end up with an unknown morph+POS combination
 | 
						|
                if pos is None or morph is None:
 | 
						|
                    pos = None
 | 
						|
                    morph = None
 | 
						|
                label_dict = Morphology.feats_to_dict(morph)
 | 
						|
                if pos:
 | 
						|
                    label_dict[self.POS_FEAT] = pos
 | 
						|
                label = self.vocab.strings[self.vocab.morphology.add(label_dict)]
 | 
						|
                eg_truths.append(label)
 | 
						|
            truths.append(eg_truths)
 | 
						|
        d_scores, loss = loss_func(scores, truths)
 | 
						|
        if self.model.ops.xp.isnan(loss):
 | 
						|
            raise ValueError("nan value when computing loss")
 | 
						|
        return float(loss), d_scores
 | 
						|
 | 
						|
    def score(self, examples, **kwargs):
 | 
						|
        """Score a batch of examples.
 | 
						|
 | 
						|
        examples (Iterable[Example]): The examples to score.
 | 
						|
        RETURNS (Dict[str, Any]): The scores, produced by
 | 
						|
            Scorer.score_token_attr for the attributes "pos" and "morph" and
 | 
						|
            Scorer.score_token_attr_per_feat for the attribute "morph".
 | 
						|
 | 
						|
        DOCS: https://nightly.spacy.io/api/morphologizer#score
 | 
						|
        """
 | 
						|
        validate_examples(examples, "Morphologizer.score")
 | 
						|
        results = {}
 | 
						|
        results.update(Scorer.score_token_attr(examples, "pos", **kwargs))
 | 
						|
        results.update(Scorer.score_token_attr(examples, "morph", **kwargs))
 | 
						|
        results.update(Scorer.score_token_attr_per_feat(examples,
 | 
						|
            "morph", **kwargs))
 | 
						|
        return results
 |