Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nn_ensemble_suggest_no_model(project):
nn_ensemble_type = annif.backend.get_backend('nn_ensemble')
nn_ensemble = nn_ensemble_type(
backend_id='nn_ensemble',
config_params={'sources': 'dummy-en'},
project=project)
with pytest.raises(NotInitializedException):
results = nn_ensemble.suggest("example text")
def test_omikuji_suggest_no_model(datadir, project):
omikuji_type = annif.backend.get_backend('omikuji')
omikuji = omikuji_type(
backend_id='omikuji',
config_params={},
project=project)
datadir.join('omikuji-model').remove()
with pytest.raises(NotInitializedException):
results = omikuji.suggest("example text")
def test_vw_multi_suggest_no_model(datadir, project):
vw_type = annif.backend.get_backend('vw_multi')
vw = vw_type(
backend_id='vw_multi',
config_params={'chunksize': 4},
datadir=str(datadir))
with pytest.raises(NotInitializedException):
results = vw.suggest("example text", project)
def test_omikuji_suggest_no_vectorizer(project):
omikuji_type = annif.backend.get_backend('omikuji')
omikuji = omikuji_type(
backend_id='omikuji',
config_params={},
project=project)
with pytest.raises(NotInitializedException):
results = omikuji.suggest("example text")
def initialize(self):
if self._model is None:
path = os.path.join(self.datadir, self.MODEL_FILE)
if not os.path.exists(path):
raise NotInitializedException(
'model {} not found'.format(path),
backend_id=self.backend_id)
self.debug('loading VW model from {}'.format(path))
params = self._create_params({'i': path, 'quiet': True})
if 'passes' in params:
# don't confuse the model with passes
del params['passes']
self.debug("model parameters: {}".format(params))
self._model = pyvw.vw(**params)
self.debug('loaded model {}'.format(str(self._model)))
def _initialize_vectorizer(self):
if self._vectorizer is None:
path = os.path.join(self.datadir, self.VECTORIZER_FILE)
if os.path.exists(path):
self.debug('loading vectorizer from {}'.format(path))
self._vectorizer = joblib.load(path)
else:
raise NotInitializedException(
"vectorizer file '{}' not found".format(path),
backend_id=self.backend_id)
def initialize(self):
if self._model is not None:
return # already initialized
model_filename = os.path.join(self.datadir, self.MODEL_FILE)
if not os.path.exists(model_filename):
raise NotInitializedException(
'model file {} not found'.format(model_filename),
backend_id=self.backend_id)
self.debug('loading Keras model from {}'.format(model_filename))
self._model = load_model(model_filename)
def initialize(self):
if self._models is not None:
return # already initialized
self._models = {}
sources = annif.util.parse_sources(self.params['sources'])
for source_project_id, _ in sources:
model_filename = self.MODEL_FILE_PREFIX + source_project_id
path = os.path.join(self.datadir, model_filename)
if os.path.exists(path):
self.debug('loading PAV model from {}'.format(path))
self._models[source_project_id] = joblib.load(path)
else:
raise NotInitializedException(
"PAV model file '{}' not found".format(path),
backend_id=self.backend_id)
def initialize(self):
if self._model is None:
path = os.path.join(self.datadir, self.MODEL_FILE)
self.debug('loading fastText model from {}'.format(path))
if os.path.exists(path):
self._model = fastText.load_model(path)
self.debug('loaded model {}'.format(str(self._model)))
self.debug('dim: {}'.format(self._model.get_dimension()))
else:
raise NotInitializedException(
'model {} not found'.format(path),
backend_id=self.backend_id)