Passed
Pull Request — master (#604)
by Osma
02:54
created

annif.rest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 32.71 %

Importance

Changes 0
Metric Value
eloc 70
dl 35
loc 107
rs 10
c 0
b 0
f 0
wmc 13

8 Functions

Rating   Name   Duplication   Size   Complexity  
A project_not_found_error() 0 7 1
A show_project() 0 9 2
A server_error() 0 8 1
A list_projects() 0 7 1
A _documents_to_corpus() 0 7 1
A _suggestion_to_dict() 0 7 1
A suggest() 18 18 3
A learn() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""Definitions for REST API operations. These are wired via Connexion to
2
methods defined in the Swagger specification."""
3
4
import connexion
5
import annif.registry
6
from annif.corpus import Document, DocumentList
7
from annif.suggestion import SuggestionFilter
8
from annif.exception import AnnifException
9
from annif.project import Access
10
11
12
def project_not_found_error(project_id):
13
    """return a Connexion error object when a project is not found"""
14
15
    return connexion.problem(
16
        status=404,
17
        title='Project not found',
18
        detail="Project '{}' not found".format(project_id))
19
20
21
def server_error(err):
22
    """return a Connexion error object when there is a server error (project
23
    or backend problem)"""
24
25
    return connexion.problem(
26
        status=503,
27
        title='Service unavailable',
28
        detail=err.format_message())
29
30
31
def list_projects():
32
    """return a dict with projects formatted according to Swagger spec"""
33
34
    return {
35
        'projects': [
36
            proj.dump() for proj in annif.registry.get_projects(
37
                min_access=Access.public).values()]}
38
39
40
def show_project(project_id):
41
    """return a single project formatted according to Swagger spec"""
42
43
    try:
44
        project = annif.registry.get_project(
45
            project_id, min_access=Access.hidden)
46
    except ValueError:
47
        return project_not_found_error(project_id)
48
    return project.dump()
49
50
51
def _suggestion_to_dict(suggestion, subject_index):
52
    subject = subject_index[suggestion.subject_id]
53
    return {
54
        'uri': subject.uri,
55
        'label': subject.label,
56
        'notation': subject.notation,
57
        'score': suggestion.score
58
    }
59
60
61 View Code Duplication
def suggest(project_id, text, limit, threshold):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
62
    """suggest subjects for the given text and return a dict with results
63
    formatted according to Swagger spec"""
64
65
    try:
66
        project = annif.registry.get_project(
67
            project_id, min_access=Access.hidden)
68
    except ValueError:
69
        return project_not_found_error(project_id)
70
71
    try:
72
        hit_filter = SuggestionFilter(project.subjects, limit, threshold)
73
        result = project.suggest(text)
74
    except AnnifException as err:
75
        return server_error(err)
76
    hits = hit_filter(result).as_list()
77
    return {'results': [_suggestion_to_dict(hit, project.subjects)
78
                        for hit in hits]}
79
80
81
def _documents_to_corpus(documents):
82
    corpus = [Document(text=d['text'],
83
                       uris=[subj['uri'] for subj in d['subjects']],
84
                       labels=[subj['label'] for subj in d['subjects']])
85
              for d in documents
86
              if 'text' in d and 'subjects' in d]
87
    return DocumentList(corpus)
88
89
90 View Code Duplication
def learn(project_id, documents):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
    """learn from documents and return an empty 204 response if succesful"""
92
93
    try:
94
        project = annif.registry.get_project(
95
            project_id, min_access=Access.hidden)
96
    except ValueError:
97
        return project_not_found_error(project_id)
98
99
    corpus = _documents_to_corpus(documents)
100
101
    try:
102
        project.learn(corpus)
103
    except AnnifException as err:
104
        return server_error(err)
105
106
    return None, 204
107