Skip to content

Commit

Permalink
Renamed stwfaspy backend to stwfsa.
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-fu committed Jan 7, 2021
1 parent b299772 commit 82a6d1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions annif/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import tfidf
from . import pav
from . import maui
from . import stwfsapy
from . import stwfsa
import annif


Expand All @@ -30,7 +30,7 @@ def get_backend(backend_id):
register_backend(tfidf.TFIDFBackend)
register_backend(pav.PAVBackend)
register_backend(maui.MauiBackend)
register_backend(stwfsapy.StwfsapyBackend)
register_backend(stwfsa.StwfsaBackend)

# Optional backends
try:
Expand Down
18 changes: 9 additions & 9 deletions annif/backend/stwfsapy.py → annif/backend/stwfsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
_KEY_SIMPLE_ENGLISH_PLURAL_RULES = 'simple_english_plural_rules'


class StwfsapyBackend(backend.AnnifBackend):
class StwfsaBackend(backend.AnnifBackend):

name = "stwfsapy"
name = "stwfsa"
needs_subject_index = True

STWFSAPY_PARAMETERS = {
STWFSA_PARAMETERS = {
_KEY_CONCEPT_TYPE_URI: str,
_KEY_SUBTHESAURUS_TYPE_URI: str,
_KEY_THESAURUS_RELATION_TYPE_URI: str,
Expand All @@ -53,14 +53,14 @@ class StwfsapyBackend(backend.AnnifBackend):
_KEY_SIMPLE_ENGLISH_PLURAL_RULES: False,
}

MODEL_FILE = 'stwfsapy_predictor.zip'
MODEL_FILE = 'stwfsa_predictor.zip'

_model = None

def initialize(self):
if self._model is None:
path = os.path.join(self.datadir, self.MODEL_FILE)
self.debug(f'Loading STWFSAPY model from {path}.')
self.debug(f'Loading STWFSA model from {path}.')
if os.path.exists(path):
self._model = StwfsapyPredictor.load(path)
self.debug('Loaded model.')
Expand All @@ -72,10 +72,10 @@ def initialize(self):
def _train(self, corpus, params):
if corpus == 'cached':
raise NotSupportedException(
'Training stwfsapy project from cached data not supported.')
'Training stwfsa project from cached data not supported.')
if corpus.is_empty():
raise NotSupportedException(
'Cannot train stwfsapy project with no documents.')
'Cannot train stwfsa project with no documents.')
self.debug("Transforming training data.")
X = []
y = []
Expand All @@ -84,10 +84,10 @@ def _train(self, corpus, params):
y.append(doc.uris)
graph = self.project.vocab.as_graph()
new_params = {
key: self.STWFSAPY_PARAMETERS[key](val)
key: self.STWFSA_PARAMETERS[key](val)
for key, val
in params.items()
if key in self.STWFSAPY_PARAMETERS
if key in self.STWFSA_PARAMETERS
}
p = StwfsapyPredictor(
graph=graph,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def read(fname):
'rdflib',
'gunicorn',
'numpy==1.18.*',
'optuna==2.2.0'
'stwfsapy==0.1.3',
'optuna==2.2.0',
'stwfsapy==0.1.4'
],
tests_require=['py', 'pytest', 'requests'],
extras_require={
Expand Down
62 changes: 31 additions & 31 deletions tests/test_backend_stwfsapy.py → tests/test_backend_stwfsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from annif.backend import get_backend
from rdflib import Graph
import annif.corpus
from annif.backend.stwfsapy import StwfsapyBackend
from annif.backend.stwfsa import StwfsaBackend
from annif.exception import NotSupportedException

import pytest
Expand Down Expand Up @@ -35,10 +35,10 @@ def graph_project(project):
}


def test_stwfsapy_default_params(project):
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
def test_stwfsa_default_params(project):
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params={},
project=project
)
Expand All @@ -54,64 +54,64 @@ def test_stwfsapy_default_params(project):
'expand_abbreviation_with_punctuation': True,
'simple_english_plural_rules': False
}
actual_params = stwfsapy.params
actual_params = stwfsa.params
assert expected_default_params == actual_params


def test_stwfsapy_train(document_corpus, graph_project, datadir):
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
def test_stwfsa_train(document_corpus, graph_project, datadir):
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params=_backend_conf,
project=graph_project)
stwfsapy.train(document_corpus)
assert stwfsapy._model is not None
model_file = datadir.join(stwfsapy.MODEL_FILE)
stwfsa.train(document_corpus)
assert stwfsa._model is not None
model_file = datadir.join(stwfsa.MODEL_FILE)
assert model_file.exists()
assert model_file.size() > 0


def test_empty_corpus(project):
corpus = annif.corpus.DocumentList([])
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params=dict(),
project=project)
with pytest.raises(NotSupportedException):
stwfsapy.train(corpus)
stwfsa.train(corpus)


def test_cached_corpus(project):
corpus = 'cached'
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params=dict(),
project=project)
with pytest.raises(NotSupportedException):
stwfsapy.train(corpus)
stwfsa.train(corpus)


def test_stwfsapy_suggest_unknown(project):
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
def test_stwfsa_suggest_unknown(project):
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params=dict(),
project=project)
results = stwfsapy.suggest('1234')
results = stwfsa.suggest('1234')
assert len(results) == 0


def test_stwfsapy_suggest(project, datadir):
stwfsapy_type = get_backend(StwfsapyBackend.name)
stwfsapy = stwfsapy_type(
backend_id=StwfsapyBackend.name,
def test_stwfsa_suggest(project, datadir):
stwfsa_type = get_backend(StwfsaBackend.name)
stwfsa = stwfsa_type(
backend_id=StwfsaBackend.name,
config_params=dict(),
project=project)
# Just some randomly selected words, taken from YSO archaeology group.
# And "random" words between them
results = stwfsapy.suggest("""random
results = stwfsa.suggest("""random
muinais-DNA random random
labyrintit random random random
Eurooppalainen yleissopimus arkeologisen perinnön suojelusta random
Expand Down

0 comments on commit 82a6d1c

Please sign in to comment.