Skip to content

Commit

Permalink
Improve Morphology errors (explosion#4314)
Browse files Browse the repository at this point in the history
* Improve Morphology errors

* Also clean up some other errors

* Update errors.py
  • Loading branch information
ines authored and honnibal committed Sep 21, 2019
1 parent 9bf69bf commit 16aa092
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
7 changes: 7 additions & 0 deletions spacy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spacy/morphology.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down
3 changes: 1 addition & 2 deletions spacy/syntax/transition_system.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion spacy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 16aa092

Please sign in to comment.