Config error with nlp.add_pipe #9749
-
I am refactoring my code from Spacy v2 to v3 and I am facing some problems. I got the following error using the below code: E thinc.config.ConfigValidationError:
E
E Config validation error
E
E phrase_matcher.phrase_patterns -> self field required
E
E {'@misc': 'phrase_matcher.patterns.v1'}
Code: class PhraseMatcherComponent:
...
@Language.factory("phrase_matcher", default_config={"phrase_patterns": []})
def create_phrase_component(nlp: Language,
name: str,
phrase_patterns):
return PhraseMatcherComponent(nlp, phrase_patterns)
class Metabase:
def __init__(subscriptions):
self.subscriptions = subscriptions
self._init_matchers()
@spacy.registry.misc("phrase_matcher.patterns.v1")
def _init_phrase_matcher(self):
phrase_patterns = []
for query, case_sensitive in self.subscriptions.queries:
tokens = self.language.make_doc(query)
if not query.endswith("*"):
phrase_patterns.append(
(str(query), None, tokens, case_sensitive))
return phrase_patterns
def _init_matchers(self):
self.language = English()
self.language.add_pipe("phrase_matcher", config={"phrase_patterns": {"@misc": "phrase_matcher.patterns.v1"}}, last=True) How should I then define the function Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A function in a registry should not be a method on an instance of an object. As you have the decorator now, the function you have for patterns has to be called on an instance of Metabase, but there's no place to get the Metabase object from. The error means that you're creating the function but you're not passing the required argument of Things that go in the config need to be JSON serializable. Assuming Metabase is not JSON serializable, the easiest thing here is probably to have a function on the Also, you might want to look at the |
Beta Was this translation helpful? Give feedback.
A function in a registry should not be a method on an instance of an object. As you have the decorator now, the function you have for patterns has to be called on an instance of Metabase, but there's no place to get the Metabase object from. The error means that you're creating the function but you're not passing the required argument of
self
, but there's also no way to specify that in the config file.Things that go in the config need to be JSON serializable. Assuming Metabase is not JSON serializable, the easiest thing here is probably to have a function on the
PhraseMatcherComponent
that allows you to set the Metabase after the component has been initialized, such asset_metabase(self,…