How to use the annif.backend function in annif

To help you get started, we’ve selected a few annif examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NatLibFi / Annif / tests / test_backend_vw_ensemble.py View on Github external
def test_vw_ensemble_default_params(datadir, project):
    vw_type = annif.backend.get_backend("vw_ensemble")
    vw = vw_type(
        backend_id='vw_ensemble',
        config_params={},
        datadir=str(datadir))

    expected_default_params = {
        'limit': 100,
        'discount_rate': 0.01,
        'loss_function': 'squared',
    }
    actual_params = vw.params
    for param, val in expected_default_params.items():
        assert param in actual_params and actual_params[param] == val
github NatLibFi / Annif / tests / test_backend.py View on Github external
def test_get_backend_dummy(app, project):
    dummy_type = annif.backend.get_backend("dummy")
    dummy = dummy_type(backend_id='dummy', config_params={},
                       datadir=app.config['DATADIR'])
    result = dummy.suggest(text='this is some text', project=project)
    assert len(result) == 1
    assert result[0].uri == 'http://example.org/dummy'
    assert result[0].label == 'dummy'
    assert result[0].score == 1.0
github NatLibFi / Annif / tests / test_backend_nn_ensemble.py View on Github external
def test_nn_ensemble_initialize(app, app_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=app_project)

    assert nn_ensemble._model is None
    with app.app_context():
        nn_ensemble.initialize()
    assert nn_ensemble._model is not None
    # initialize a second time - this shouldn't do anything
    with app.app_context():
        nn_ensemble.initialize()
github NatLibFi / Annif / tests / test_backend_fasttext.py View on Github external
def test_fasttext_default_params(datadir, project):
    fasttext_type = annif.backend.get_backend("fasttext")
    fasttext = fasttext_type(
        backend_id='fasttext',
        config_params={},
        datadir=str(datadir))

    expected_default_params = {
        'limit': 100,
        'chunksize': 1,
        'dim': 100,
        'lr': 0.25,
        'epoch': 5,
        'loss': 'hs',
    }
    actual_params = fasttext.params
    for param, val in expected_default_params.items():
        assert param in actual_params and actual_params[param] == val
github NatLibFi / Annif / tests / test_backend_vw_ensemble.py View on Github external
def test_vw_ensemble_suggest(app, datadir):
    vw_ensemble_type = annif.backend.get_backend("vw_ensemble")
    vw_ensemble = vw_ensemble_type(
        backend_id='vw_ensemble',
        config_params={'sources': 'dummy-en'},
        datadir=str(datadir))

    project = annif.project.get_project('dummy-en')

    results = vw_ensemble.suggest("""Arkeologiaa sanotaan joskus myös
        muinaistutkimukseksi tai muinaistieteeksi. Se on humanistinen tiede
        tai oikeammin joukko tieteitä, jotka tutkivat ihmisen menneisyyttä.
        Tutkimusta tehdään analysoimalla muinaisjäännöksiä eli niitä jälkiä,
        joita ihmisten toiminta on jättänyt maaperään tai vesistöjen
        pohjaan.""", project)

    assert vw_ensemble._model is not None
    assert len(results) > 0
github NatLibFi / Annif / tests / test_backend_maui.py View on Github external
def test_maui_train_missing_tagger(document_corpus, project):
    maui_type = annif.backend.get_backend("maui")
    maui = maui_type(
        backend_id='maui',
        config_params={
            'endpoint': 'http://api.example.org/mauiservice/',
            'language': 'en'},
        project=project)

    with pytest.raises(ConfigurationException):
        maui.train(document_corpus)
github NatLibFi / Annif / tests / test_backend.py View on Github external
def test_get_backend_nonexistent():
    with pytest.raises(ValueError):
        annif.backend.get_backend("nonexistent")
github NatLibFi / Annif / tests / test_backend_maui.py View on Github external
def maui(app_project):
    maui_type = annif.backend.get_backend("maui")
    maui = maui_type(
        backend_id='maui',
        config_params={
            'endpoint': 'http://api.example.org/mauiservice/',
            'tagger': 'dummy',
            'language': 'en'},
        project=app_project)
    return maui
github NatLibFi / Annif / tests / test_project.py View on Github external
def test_get_project_fi(app):
    with app.app_context():
        project = annif.project.get_project('dummy-fi')
    assert project.project_id == 'dummy-fi'
    assert project.language == 'fi'
    assert project.analyzer.name == 'snowball'
    assert project.analyzer.param == 'finnish'
    assert project.access == Access.public
    assert isinstance(project.backend, annif.backend.dummy.DummyBackend)
github NatLibFi / Annif / annif / project.py View on Github external
def backend(self):
        if self._backend is None:
            if 'backend' not in self.config:
                raise ConfigurationException(
                    "backend setting is missing", project_id=self.project_id)
            backend_id = self.config['backend']
            try:
                backend_class = annif.backend.get_backend(backend_id)
                self._backend = backend_class(
                    backend_id, config_params=self.config,
                    datadir=self.datadir)
            except ValueError:
                logger.warning(
                    "Could not create backend %s, "
                    "make sure you've installed optional dependencies",
                    backend_id)
        return self._backend