From 16aa092fb5cffb5ec7079951ea0c04cb96733b3e Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Sat, 21 Sep 2019 14:37:06 +0200 Subject: [PATCH] Improve Morphology errors (#4314) * Improve Morphology errors * Also clean up some other errors * Update errors.py --- spacy/errors.py | 7 +++++++ spacy/morphology.pyx | 6 +++--- spacy/syntax/transition_system.pyx | 3 +-- spacy/util.py | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spacy/errors.py b/spacy/errors.py index b03bd6d23fd..a6b199a5098 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -469,6 +469,13 @@ class Errors(object): "that case.") E166 = ("Can only merge DocBins with the same pre-defined attributes.\n" "Current DocBin: {current}\nOther DocBin: {other}") + E167 = ("Unknown morphological feature: '{feat}' ({feat_id}). This can " + "happen if the tagger was trained with a different set of " + "morphological features. If you're using a pre-trained model, make " + "sure that your models are up to date:\npython -m spacy validate") + E168 = ("Unknown field: {field}") + E169 = ("Can't find module: {module}") + E170 = ("Cannot apply transition {name}: invalid for the current state.") @add_codes diff --git a/spacy/morphology.pyx b/spacy/morphology.pyx index 190ca8d00a6..c146094a900 100644 --- a/spacy/morphology.pyx +++ b/spacy/morphology.pyx @@ -197,7 +197,7 @@ cdef class Morphology: cdef attr_t feature for feature in features: if feature != 0 and feature not in self._feat_map.id2feat: - raise KeyError("Unknown feature: %s" % self.strings[feature]) + raise ValueError(Errors.E167.format(feat=self.strings[feature], feat_id=feature)) cdef MorphAnalysisC tag tag = create_rich_tag(features) cdef hash_t key = self.insert(tag) @@ -531,7 +531,7 @@ cdef attr_t get_field(const MorphAnalysisC* tag, int field_id) nogil: elif field == Field_VerbType: return tag.verb_type else: - raise ValueError("Unknown field: (%d)" % field_id) + raise ValueError(Errors.E168.format(field=field_id)) cdef int check_feature(const MorphAnalysisC* tag, attr_t feature) nogil: @@ -726,7 +726,7 @@ cdef int set_feature(MorphAnalysisC* tag, elif field == Field_VerbType: tag.verb_type = value_ else: - raise ValueError("Unknown feature: %s (%d)" % (FEATURE_NAMES.get(feature), feature)) + raise ValueError(Errors.E167.format(field=FEATURE_NAMES.get(feature), field_id=feature)) FIELDS = { diff --git a/spacy/syntax/transition_system.pyx b/spacy/syntax/transition_system.pyx index 523cd66994c..fede704b536 100644 --- a/spacy/syntax/transition_system.pyx +++ b/spacy/syntax/transition_system.pyx @@ -96,8 +96,7 @@ cdef class TransitionSystem: def apply_transition(self, StateClass state, name): if not self.is_valid(state, name): - raise ValueError( - "Cannot apply transition {name}: invalid for the current state.".format(name=name)) + raise ValueError(Errors.E170.format(name=name)) action = self.lookup_transition(name) action.do(state.c, action.label) diff --git a/spacy/util.py b/spacy/util.py index e88d664524f..dbe965392c7 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -136,7 +136,7 @@ def load_language_data(path): def get_module_path(module): if not hasattr(module, "__module__"): - raise ValueError("Can't find module {}".format(repr(module))) + raise ValueError(Errors.E169.format(module=repr(module))) return Path(sys.modules[module.__module__].__file__).parent