Skip to content

Commit

Permalink
limit number of candidate languages on schema level; change parameter…
Browse files Browse the repository at this point in the history
… name to languages
  • Loading branch information
osma committed Sep 16, 2024
1 parent 065bfeb commit 36b479a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
6 changes: 4 additions & 2 deletions annif/openapi/annif.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,22 @@ paths:
type: object
required:
- text
- candidates
- languages
properties:
text:
type: string
description: input text
example: A quick brown fox jumped over the lazy dog.
candidates:
languages:
type: array
description: candidate languages as IETF BCP 47 codes
items:
type: string
maxLength: 3
minLength: 2
example: en
minItems: 1
maxItems: 5
required: true
responses:
200:
Expand Down
11 changes: 2 additions & 9 deletions annif/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,9 @@ def detect_language(body: dict[str, Any]):
"""return scores for detected languages formatted according to Swagger spec"""

text = body.get("text")
candidates = body.get("candidates")
languages = body.get("languages")

if not candidates:
return connexion.problem(
status=400,
title="Bad Request",
detail="no candidate languages given",
)

detector = get_language_detector(tuple(candidates))
detector = get_language_detector(tuple(languages))
try:
proportions = detector.proportion_in_each_language(text)
except ValueError:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ def test_openapi_learn_novocab(app_client):
data = []
req = app_client.post("http://localhost:8000/v1/projects/novocab/learn", json=data)
assert req.status_code == 503


def test_rest_detect_language_no_candidates(app_client):
data = {"text": "example text", "languages": []}
req = app_client.post("http://localhost:8000/v1/detect-language", json=data)
assert req.status_code == 400


def test_rest_detect_language_too_many_candidates(app_client):
data = {"text": "example text", "languages": ["en", "fr", "de", "it", "es", "nl"]}
req = app_client.post("http://localhost:8000/v1/detect-language", json=data)
assert req.status_code == 400
14 changes: 4 additions & 10 deletions tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_rest_detect_language_english(app):
# english text should be detected
with app.app_context():
result = annif.rest.detect_language(
{"text": "example text", "candidates": ["en", "fi", "sv"]}
{"text": "example text", "languages": ["en", "fi", "sv"]}
)[0]
assert {"language": "en", "score": 1} in result["results"]

Expand All @@ -66,27 +66,21 @@ def test_rest_detect_language_unknown(app):
# an unknown language should return None
with app.app_context():
result = annif.rest.detect_language(
{"text": "exampley texty", "candidates": ["fi", "sv"]}
{"text": "exampley texty", "languages": ["fi", "sv"]}
)[0]
assert {"language": None, "score": 1} in result["results"]


def test_rest_detect_language_no_text(app):
with app.app_context():
result = annif.rest.detect_language({"text": "", "candidates": ["en"]})[0]
result = annif.rest.detect_language({"text": "", "languages": ["en"]})[0]
assert {"language": None, "score": 1} in result["results"]


def test_rest_detect_language_no_candidates(app):
with app.app_context():
result = annif.rest.detect_language({"text": "example text", "candidates": []})
assert result.status_code == 400


def test_rest_detect_language_unsupported_candidates(app):
with app.app_context():
result = annif.rest.detect_language(
{"text": "example text", "candidates": ["unk"]}
{"text": "example text", "languages": ["unk"]}
)
assert result.status_code == 400

Expand Down

0 comments on commit 36b479a

Please sign in to comment.