Passed
Pull Request — master (#663)
by Juho
03:59
created

annif.rest.language_not_supported_error()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
"""Definitions for REST API operations. These are wired via Connexion to
2
methods defined in the Swagger specification."""
3
4
import importlib
5
6
import connexion
7
8
import annif.registry
9
from annif.corpus import Document, DocumentList, SubjectSet
10
from annif.exception import AnnifException
11
from annif.project import Access
12
from annif.suggestion import SuggestionFilter
13
14
15
def project_not_found_error(project_id):
16
    """return a Connexion error object when a project is not found"""
17
18
    return connexion.problem(
19
        status=404,
20
        title="Project not found",
21
        detail="Project '{}' not found".format(project_id),
22
    )
23
24
25
def server_error(err):
26
    """return a Connexion error object when there is a server error (project
27
    or backend problem)"""
28
29
    return connexion.problem(
30
        status=503, title="Service unavailable", detail=err.format_message()
31
    )
32
33
34
def language_not_supported_error(lang):
35
    """return a Connexion error object when attempting to use unsupported language"""
36
37
    return connexion.problem(
38
        status=400,
39
        title="Bad Request",
40
        detail=f'language "{lang}" not supported by vocabulary',
41
    )
42
43
44
def show_info():
45
    """return version of annif and a title for the api according to Swagger spec"""
46
47
    return {"title": "Annif REST API", "version": importlib.metadata.version("annif")}
48
49
50
def list_projects():
51
    """return a dict with projects formatted according to Swagger spec"""
52
53
    return {
54
        "projects": [
55
            proj.dump()
56
            for proj in annif.registry.get_projects(min_access=Access.public).values()
57
        ]
58
    }
59
60
61
def show_project(project_id):
62
    """return a single project formatted according to Swagger spec"""
63
64
    try:
65
        project = annif.registry.get_project(project_id, min_access=Access.hidden)
66
    except ValueError:
67
        return project_not_found_error(project_id)
68
    return project.dump()
69
70
71
def _suggestion_to_dict(suggestion, subject_index, language):
72
    subject = subject_index[suggestion.subject_id]
73
    return {
74
        "uri": subject.uri,
75
        "label": subject.labels[language],
76
        "notation": subject.notation,
77
        "score": suggestion.score,
78
    }
79
80
81
def suggest(project_id, body):
82
    """suggest subjects for the given text and return a dict with results
83
    formatted according to Swagger spec"""
84
85
    parameters = dict(
86
        (key, body[key]) for key in ["language", "limit", "threshold"] if key in body
87
    )
88
    documents = [{"text": body["text"]}]
89
    result = _suggest(project_id, documents, parameters)
90
91
    if isinstance(result, list):
92
        return result[0]  # successful operation
93
    else:
94
        return result  # connexion problem
95
96
97
def suggest_batch(project_id, body):
98
    """suggest subjects for the given documents and return a list of dicts with results
99
    formatted according to Swagger spec"""
100
101
    parameters = body.get("parameters", {})
102
    return _suggest(project_id, body["documents"], parameters)
103
104
105
def _suggest(project_id, documents, parameters):
106
    corpus = DocumentList(
107
        [
108
            Document(
109
                text=d["text"],
110
                subject_set=None,
111
            )
112
            for d in documents
113
        ]
114
    )
115
116
    try:
117
        project = annif.registry.get_project(project_id, min_access=Access.hidden)
118
    except ValueError:
119
        return project_not_found_error(project_id)
120
121
    try:
122
        lang = parameters.get("language") or project.vocab_lang
123
    except AnnifException as err:
124
        return server_error(err)
125
126
    if lang not in project.vocab.languages:
127
        return language_not_supported_error(lang)
128
129
    limit = parameters.get("limit", 10)
130
    threshold = parameters.get("threshold", 0.0)
131
132
    try:
133
        hit_filter = SuggestionFilter(project.subjects, limit, threshold)
134
        hit_sets = project.suggest_batch(corpus)
135
    except AnnifException as err:
136
        return server_error(err)
137
138
    return [
139
        {
140
            "results": [
141
                _suggestion_to_dict(hit, project.subjects, lang)
142
                for hit in hit_filter(hits).as_list()
143
            ]
144
        }
145
        for hits in hit_sets
146
    ]
147
148
149
def _documents_to_corpus(documents, subject_index):
150
    corpus = [
151
        Document(
152
            text=d["text"],
153
            subject_set=SubjectSet(
154
                [subject_index.by_uri(subj["uri"]) for subj in d["subjects"]]
155
            ),
156
        )
157
        for d in documents
158
        if "text" in d and "subjects" in d
159
    ]
160
    return DocumentList(corpus)
161
162
163
def learn(project_id, body):
164
    """learn from documents and return an empty 204 response if succesful"""
165
166
    try:
167
        project = annif.registry.get_project(project_id, min_access=Access.hidden)
168
    except ValueError:
169
        return project_not_found_error(project_id)
170
171
    try:
172
        corpus = _documents_to_corpus(body, project.subjects)
173
        project.learn(corpus)
174
    except AnnifException as err:
175
        return server_error(err)
176
177
    return None, 204
178