mirror of
https://github.com/explosion/spaCy.git
synced 2025-01-24 08:14:15 +03:00
Refactor error messages to remove hardcoded strings (#10729)
* Use custom error msg instead of hardcoded string: replaced remaining hardcoded error message strings. * Use custom error msg instead of hardcoded string: fixing faulty Errors import.
This commit is contained in:
parent
0a503ce5e0
commit
f5390e278a
|
@ -905,7 +905,15 @@ class Errors(metaclass=ErrorsWithCodes):
|
||||||
E1026 = ("Edit tree has an invalid format:\n{errors}")
|
E1026 = ("Edit tree has an invalid format:\n{errors}")
|
||||||
E1027 = ("AlignmentArray only supports slicing with a step of 1.")
|
E1027 = ("AlignmentArray only supports slicing with a step of 1.")
|
||||||
E1028 = ("AlignmentArray only supports indexing using an int or a slice.")
|
E1028 = ("AlignmentArray only supports indexing using an int or a slice.")
|
||||||
|
E1029 = ("Edit tree cannot be applied to form.")
|
||||||
|
E1030 = ("Edit tree identifier out of range.")
|
||||||
|
E1031 = ("Could not find gold transition - see logs above.")
|
||||||
|
E1032 = ("`{var}` should not be {forbidden}, but received {value}.")
|
||||||
|
E1033 = ("Dimension {name} invalid -- only nO, nF, nP")
|
||||||
|
E1034 = ("Node index {i} out of bounds ({length})")
|
||||||
|
E1035 = ("Token index {i} out of bounds ({length})")
|
||||||
|
E1036 = ("Cannot index into NoneNode")
|
||||||
|
|
||||||
|
|
||||||
# Deprecated model shortcuts, only used in errors and warnings
|
# Deprecated model shortcuts, only used in errors and warnings
|
||||||
OLD_MODEL_SHORTCUTS = {
|
OLD_MODEL_SHORTCUTS = {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import numpy.random
|
||||||
from thinc.api import Model, CupyOps, NumpyOps
|
from thinc.api import Model, CupyOps, NumpyOps
|
||||||
|
|
||||||
from .. import util
|
from .. import util
|
||||||
|
from ..errors import Errors
|
||||||
from ..typedefs cimport weight_t, class_t, hash_t
|
from ..typedefs cimport weight_t, class_t, hash_t
|
||||||
from ..pipeline._parser_internals.stateclass cimport StateClass
|
from ..pipeline._parser_internals.stateclass cimport StateClass
|
||||||
|
|
||||||
|
@ -411,7 +412,7 @@ cdef class precompute_hiddens:
|
||||||
elif name == "nO":
|
elif name == "nO":
|
||||||
return self.nO
|
return self.nO
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP")
|
raise ValueError(Errors.E1033.format(name=name))
|
||||||
|
|
||||||
def set_dim(self, name, value):
|
def set_dim(self, name, value):
|
||||||
if name == "nF":
|
if name == "nF":
|
||||||
|
@ -421,7 +422,7 @@ cdef class precompute_hiddens:
|
||||||
elif name == "nO":
|
elif name == "nO":
|
||||||
self.nO = value
|
self.nO = value
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP")
|
raise ValueError(Errors.E1033.format(name=name))
|
||||||
|
|
||||||
def __call__(self, X, bint is_train):
|
def __call__(self, X, bint is_train):
|
||||||
if is_train:
|
if is_train:
|
||||||
|
|
|
@ -132,7 +132,7 @@ cdef class EditTrees:
|
||||||
could not be applied to the form.
|
could not be applied to the form.
|
||||||
"""
|
"""
|
||||||
if tree_id >= self.trees.size():
|
if tree_id >= self.trees.size():
|
||||||
raise IndexError("Edit tree identifier out of range")
|
raise IndexError(Errors.E1030)
|
||||||
|
|
||||||
lemma_pieces = []
|
lemma_pieces = []
|
||||||
try:
|
try:
|
||||||
|
@ -154,7 +154,7 @@ cdef class EditTrees:
|
||||||
match_node = tree.inner.match_node
|
match_node = tree.inner.match_node
|
||||||
|
|
||||||
if match_node.prefix_len + match_node.suffix_len > len(form_part):
|
if match_node.prefix_len + match_node.suffix_len > len(form_part):
|
||||||
raise ValueError("Edit tree cannot be applied to form")
|
raise ValueError(Errors.E1029)
|
||||||
|
|
||||||
suffix_start = len(form_part) - match_node.suffix_len
|
suffix_start = len(form_part) - match_node.suffix_len
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ cdef class EditTrees:
|
||||||
if form_part == self.strings[tree.inner.subst_node.orig]:
|
if form_part == self.strings[tree.inner.subst_node.orig]:
|
||||||
lemma_pieces.append(self.strings[tree.inner.subst_node.subst])
|
lemma_pieces.append(self.strings[tree.inner.subst_node.subst])
|
||||||
else:
|
else:
|
||||||
raise ValueError("Edit tree cannot be applied to form")
|
raise ValueError(Errors.E1029)
|
||||||
|
|
||||||
cpdef unicode tree_to_str(self, uint32_t tree_id):
|
cpdef unicode tree_to_str(self, uint32_t tree_id):
|
||||||
"""Return the tree as a string. The tree tree string is formatted
|
"""Return the tree as a string. The tree tree string is formatted
|
||||||
|
@ -187,7 +187,7 @@ cdef class EditTrees:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if tree_id >= self.trees.size():
|
if tree_id >= self.trees.size():
|
||||||
raise IndexError("Edit tree identifier out of range")
|
raise IndexError(Errors.E1030)
|
||||||
|
|
||||||
cdef EditTreeC tree = self.trees[tree_id]
|
cdef EditTreeC tree = self.trees[tree_id]
|
||||||
cdef SubstNodeC subst_node
|
cdef SubstNodeC subst_node
|
||||||
|
|
|
@ -824,7 +824,7 @@ cdef class ArcEager(TransitionSystem):
|
||||||
for i in range(self.n_moves):
|
for i in range(self.n_moves):
|
||||||
print(self.get_class_name(i), is_valid[i], costs[i])
|
print(self.get_class_name(i), is_valid[i], costs[i])
|
||||||
print("Gold sent starts?", is_sent_start(&gold_state, state.B(0)), is_sent_start(&gold_state, state.B(1)))
|
print("Gold sent starts?", is_sent_start(&gold_state, state.B(0)), is_sent_start(&gold_state, state.B(1)))
|
||||||
raise ValueError("Could not find gold transition - see logs above.")
|
raise ValueError(Errors.E1031)
|
||||||
|
|
||||||
def get_oracle_sequence_from_state(self, StateClass state, ArcEagerGold gold, _debug=None):
|
def get_oracle_sequence_from_state(self, StateClass state, ArcEagerGold gold, _debug=None):
|
||||||
cdef int i
|
cdef int i
|
||||||
|
|
|
@ -9,6 +9,8 @@ cimport cython
|
||||||
import weakref
|
import weakref
|
||||||
from preshed.maps cimport map_get_unless_missing
|
from preshed.maps cimport map_get_unless_missing
|
||||||
from murmurhash.mrmr cimport hash64
|
from murmurhash.mrmr cimport hash64
|
||||||
|
|
||||||
|
from .. import Errors
|
||||||
from ..typedefs cimport hash_t
|
from ..typedefs cimport hash_t
|
||||||
from ..strings import get_string_id
|
from ..strings import get_string_id
|
||||||
from ..structs cimport EdgeC, GraphC
|
from ..structs cimport EdgeC, GraphC
|
||||||
|
@ -68,7 +70,7 @@ cdef class Node:
|
||||||
"""
|
"""
|
||||||
cdef int length = graph.c.nodes.size()
|
cdef int length = graph.c.nodes.size()
|
||||||
if i >= length or -i >= length:
|
if i >= length or -i >= length:
|
||||||
raise IndexError(f"Node index {i} out of bounds ({length})")
|
raise IndexError(Errors.E1034.format(i=i, length=length))
|
||||||
if i < 0:
|
if i < 0:
|
||||||
i += length
|
i += length
|
||||||
self.graph = graph
|
self.graph = graph
|
||||||
|
@ -88,7 +90,7 @@ cdef class Node:
|
||||||
"""Get a token index from the node's set of tokens."""
|
"""Get a token index from the node's set of tokens."""
|
||||||
length = self.graph.c.nodes[self.i].size()
|
length = self.graph.c.nodes[self.i].size()
|
||||||
if i >= length or -i >= length:
|
if i >= length or -i >= length:
|
||||||
raise IndexError(f"Token index {i} out of bounds ({length})")
|
raise IndexError(Errors.E1035.format(i=i, length=length))
|
||||||
if i < 0:
|
if i < 0:
|
||||||
i += length
|
i += length
|
||||||
return self.graph.c.nodes[self.i][i]
|
return self.graph.c.nodes[self.i][i]
|
||||||
|
@ -306,7 +308,7 @@ cdef class NoneNode(Node):
|
||||||
self.i = -1
|
self.i = -1
|
||||||
|
|
||||||
def __getitem__(self, int i):
|
def __getitem__(self, int i):
|
||||||
raise IndexError("Cannot index into NoneNode.")
|
raise IndexError(Errors.E1036)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 0
|
return 0
|
||||||
|
|
|
@ -730,7 +730,7 @@ cdef class Span:
|
||||||
|
|
||||||
def __set__(self, int start):
|
def __set__(self, int start):
|
||||||
if start < 0:
|
if start < 0:
|
||||||
raise IndexError("TODO")
|
raise IndexError(Errors.E1032.format(var="start", forbidden="< 0", value=start))
|
||||||
self.c.start = start
|
self.c.start = start
|
||||||
|
|
||||||
property end:
|
property end:
|
||||||
|
@ -739,7 +739,7 @@ cdef class Span:
|
||||||
|
|
||||||
def __set__(self, int end):
|
def __set__(self, int end):
|
||||||
if end < 0:
|
if end < 0:
|
||||||
raise IndexError("TODO")
|
raise IndexError(Errors.E1032.format(var="end", forbidden="< 0", value=end))
|
||||||
self.c.end = end
|
self.c.end = end
|
||||||
|
|
||||||
property start_char:
|
property start_char:
|
||||||
|
@ -748,7 +748,7 @@ cdef class Span:
|
||||||
|
|
||||||
def __set__(self, int start_char):
|
def __set__(self, int start_char):
|
||||||
if start_char < 0:
|
if start_char < 0:
|
||||||
raise IndexError("TODO")
|
raise IndexError(Errors.E1032.format(var="start_char", forbidden="< 0", value=start_char))
|
||||||
self.c.start_char = start_char
|
self.c.start_char = start_char
|
||||||
|
|
||||||
property end_char:
|
property end_char:
|
||||||
|
@ -757,7 +757,7 @@ cdef class Span:
|
||||||
|
|
||||||
def __set__(self, int end_char):
|
def __set__(self, int end_char):
|
||||||
if end_char < 0:
|
if end_char < 0:
|
||||||
raise IndexError("TODO")
|
raise IndexError(Errors.E1032.format(var="end_char", forbidden="< 0", value=end_char))
|
||||||
self.c.end_char = end_char
|
self.c.end_char = end_char
|
||||||
|
|
||||||
property label:
|
property label:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user