Improve Morphology errors (#4314)

* Improve Morphology errors

* Also clean up some other errors

* Update errors.py
This commit is contained in:
Ines Montani 2019-09-21 14:37:06 +02:00 committed by Matthew Honnibal
parent 9bf69bfbb2
commit 16aa092fb5
4 changed files with 12 additions and 6 deletions

View File

@ -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

View File

@ -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 = {

View File

@ -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)

View File

@ -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