Skip to content

Commit

Permalink
adapt to annif.simplemma_util and newer Connexion
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Sep 16, 2024
1 parent 4c7547a commit 1cd8003
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions annif/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from typing import TYPE_CHECKING, Any

import connexion
from simplemma.langdetect import lang_detector

import annif.registry
from annif.corpus import Document, DocumentList, SubjectSet
from annif.exception import AnnifException
from annif.project import Access
from annif.simplemma_util import get_language_detector

if TYPE_CHECKING:
from connexion.lifecycle import ConnexionResponse
Expand Down Expand Up @@ -83,7 +83,7 @@ def show_project(
return project.dump(), 200, {"Content-Type": "application/json"}


def detect_language(body):
def detect_language(body: dict[str, Any]):
"""return scores for detected languages formatted according to Swagger spec"""

text = body.get("text")
Expand All @@ -96,21 +96,23 @@ def detect_language(body):
detail="no candidate languages given",
)

scores = lang_detector(text, tuple(candidates))

if not scores:
detector = get_language_detector(tuple(candidates))
try:
proportions = detector.proportion_in_each_language(text)
except ValueError:
return connexion.problem(
status=400,
title="Bad Request",
detail="unsupported candidate languages",
)

return {
result = {
"results": [
{"language": lang if lang != "unk" else None, "score": score}
for lang, score in scores
for lang, score in proportions.items()
]
}
return result, 200, {"Content-Type": "application/json"}


def _suggestion_to_dict(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_rest_detect_language_unknown(app):
# an unknown language should return None
with app.app_context():
result = annif.rest.detect_language(
{"text": "example text", "candidates": ["fi", "sv"]}
{"text": "exampley texty", "candidates": ["fi", "sv"]}
)
assert {"language": None, "score": 1} in result["results"]

Expand Down

0 comments on commit 1cd8003

Please sign in to comment.