Smarter initialization of optional analyzers #820
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a problem with the initialization of optional analyzers that I noticed while working on the EstNLTK analyzer in PR #818 . For the spaCy analyzer, the code in
analyzer/__init__.py
never noticed the case when spaCy is not installed, because theimport spacy
statement only happens inside a method (due to performance reasons).This PR adds a new static method
is_available
to all Analyzer implementations and uses that to determine whether the analyzer is available (i.e. the optional dependency is installed) or not. For spaCy and Voikko analyzers, the method is implemented by using importlib.find_spec to see if the necessary modules are available or not. This should be faster than actually importing the module, which can be done at a later time within the methods if it's actually needed.