|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import click |
|
4
|
|
|
from flask import Flask |
|
5
|
|
|
from elasticsearch import Elasticsearch |
|
6
|
|
|
from elasticsearch.client import IndicesClient |
|
7
|
|
|
|
|
8
|
|
|
es = Elasticsearch() |
|
|
|
|
|
|
9
|
|
|
index = IndicesClient(es) |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
annif = Flask(__name__) |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
annif.config.from_object('annif.config.Config') |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
projectIndexConf = { |
|
|
|
|
|
|
17
|
|
|
'mappings': { |
|
|
|
|
|
|
18
|
|
|
'project': { |
|
19
|
|
|
'properties': { |
|
20
|
|
|
'name': { |
|
21
|
|
|
'type': 'text' |
|
22
|
|
|
}, |
|
23
|
|
|
'language': { |
|
24
|
|
|
'type': 'text' |
|
25
|
|
|
}, |
|
26
|
|
|
'analyzer': { |
|
27
|
|
|
'type': 'text' |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def parseIndexname(projectid): |
|
|
|
|
|
|
36
|
|
|
return "{0}-{1}".format(annif.config['INDEX_NAME'], projectid) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
@annif.cli.command('init') |
|
40
|
|
|
def init(): |
|
41
|
|
|
""" |
|
42
|
|
|
Generate the Elasticsearch repository for projects. |
|
43
|
|
|
|
|
44
|
|
|
Usage: annif init |
|
45
|
|
|
""" |
|
46
|
|
|
if index.exists(annif.config['INDEX_NAME']): |
|
47
|
|
|
index.delete(annif.config['INDEX_NAME']) |
|
48
|
|
|
return es.indices.create(index=annif.config['INDEX_NAME'], |
|
49
|
|
|
body=projectIndexConf) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@annif.cli.command('list-projects') |
|
53
|
|
|
def listprojects(): |
|
54
|
|
|
""" |
|
55
|
|
|
List available projects. |
|
56
|
|
|
|
|
57
|
|
|
Usage: annif list-projects |
|
58
|
|
|
|
|
59
|
|
|
REST equivalent: GET /projects/ |
|
60
|
|
|
""" |
|
61
|
|
|
|
|
62
|
|
|
doc = {'size': 1000, 'query': {'match_all': {}}} |
|
63
|
|
|
results = es.search(index=annif.config['INDEX_NAME'], doc_type='project', |
|
64
|
|
|
body=doc) |
|
65
|
|
|
projects = [x['_source'] for x in results['hits']['hits']] |
|
66
|
|
|
for p in projects: |
|
|
|
|
|
|
67
|
|
|
print(p) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
@annif.cli.command('show-project') |
|
71
|
|
|
@click.argument('projectid') |
|
72
|
|
|
def showProject(projectid): |
|
|
|
|
|
|
73
|
|
|
""" |
|
74
|
|
|
Show project information. |
|
75
|
|
|
|
|
76
|
|
|
Usage: annif show-project <projectId> |
|
77
|
|
|
|
|
78
|
|
|
REST equivalent: |
|
79
|
|
|
|
|
80
|
|
|
GET /projects/<projectId> |
|
81
|
|
|
""" |
|
82
|
|
|
pass |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
@annif.cli.command('create-project') |
|
86
|
|
|
@click.argument('projectid') |
|
87
|
|
|
@click.option('--language') |
|
88
|
|
|
@click.option('--analyzer') |
|
89
|
|
|
def createProject(projectid, language, analyzer): |
|
|
|
|
|
|
90
|
|
|
""" |
|
91
|
|
|
Create a new project. |
|
92
|
|
|
|
|
93
|
|
|
Usage: annif create-project <projectId> --language <lang> --analyzer |
|
94
|
|
|
<analyzer> |
|
95
|
|
|
|
|
96
|
|
|
REST equivalent: |
|
97
|
|
|
|
|
98
|
|
|
PUT /projects/<projectId> |
|
99
|
|
|
""" |
|
100
|
|
|
proj_indexname = parseIndexname(projectid) |
|
101
|
|
|
|
|
102
|
|
|
# Create an index for the project |
|
103
|
|
|
index.create(index=proj_indexname) |
|
104
|
|
|
|
|
105
|
|
|
# Add the details of the new project to the 'master' index |
|
106
|
|
|
resp = es.create(index=annif.config['INDEX_NAME'], doc_type='project', |
|
107
|
|
|
id=projectid, |
|
108
|
|
|
body={'name': projectid, 'language': language, |
|
109
|
|
|
'analyzer': analyzer}) |
|
110
|
|
|
print(resp) |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
@annif.cli.command('drop-project') |
|
114
|
|
|
@click.argument('projectid') |
|
115
|
|
|
def dropProject(projectid): |
|
|
|
|
|
|
116
|
|
|
""" |
|
117
|
|
|
Delete a project. |
|
118
|
|
|
USAGE: annif drop-project <projectid> |
|
119
|
|
|
|
|
120
|
|
|
REST equivalent: |
|
121
|
|
|
|
|
122
|
|
|
DELETE /projects/<projectid> |
|
123
|
|
|
""" |
|
124
|
|
|
# Delete the index from the 'master' index |
|
125
|
|
|
result = es.delete(index=annif.config['INDEX_NAME'], |
|
126
|
|
|
doc_type='project', id=projectid) |
|
127
|
|
|
|
|
128
|
|
|
print(result) |
|
129
|
|
|
|
|
130
|
|
|
# Then delete the project index |
|
131
|
|
|
result = index.delete(index=parseIndexname(projectid)) |
|
132
|
|
|
print(result) |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
@annif.cli.command('list-subjects') |
|
136
|
|
|
@click.argument('projectid') |
|
137
|
|
|
def listSubjects(projectid): |
|
|
|
|
|
|
138
|
|
|
""" |
|
139
|
|
|
Show all subjects for a project. |
|
140
|
|
|
|
|
141
|
|
|
USAGE: annif list-subjects <projectid> |
|
142
|
|
|
|
|
143
|
|
|
REST equivalent: |
|
144
|
|
|
|
|
145
|
|
|
GET /projects/<projectid>/subjects |
|
146
|
|
|
""" |
|
147
|
|
|
pass |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
@annif.cli.command('show-subject') |
|
151
|
|
|
@click.argument('projectid') |
|
152
|
|
|
@click.argument('subjectid') |
|
153
|
|
|
def showSubject(projectid, subjectid): |
|
|
|
|
|
|
154
|
|
|
""" |
|
155
|
|
|
Show information about a subject. |
|
156
|
|
|
|
|
157
|
|
|
USAGE: annif show-subject <projectid> <subjectid> |
|
158
|
|
|
|
|
159
|
|
|
REST equivalent: |
|
160
|
|
|
|
|
161
|
|
|
GET /projects/<projectid>/subjects/<subjectid> |
|
162
|
|
|
""" |
|
163
|
|
|
pass |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
@annif.cli.command('create-subject') |
|
167
|
|
|
@click.argument('projectid') |
|
168
|
|
|
@click.argument('subjectid') |
|
169
|
|
|
def createSubject(projectid, subjectid): |
|
|
|
|
|
|
170
|
|
|
""" |
|
171
|
|
|
Create a new subject, or update an existing one. |
|
172
|
|
|
|
|
173
|
|
|
annif create-subject <projectid> <subjectid> <subject.txt |
|
174
|
|
|
|
|
175
|
|
|
REST equivalent: |
|
176
|
|
|
|
|
177
|
|
|
PUT /projects/<projectid>/subjects/<subjectid> |
|
178
|
|
|
""" |
|
179
|
|
|
pass |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
@annif.cli.command('load') |
|
183
|
|
|
@click.argument('projectid') |
|
184
|
|
|
@click.argument('directory') |
|
185
|
|
|
@click.option('--clear', default=False) |
|
186
|
|
|
def load(projectid, directory, clear): |
|
|
|
|
|
|
187
|
|
|
""" |
|
188
|
|
|
Load all subjects from a directory. |
|
189
|
|
|
|
|
190
|
|
|
USAGE: annif load <projectid> <directory> [--clear=CLEAR] |
|
191
|
|
|
""" |
|
192
|
|
|
pass |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
@annif.cli.command('drop-subject') |
|
196
|
|
|
@click.argument('projectid') |
|
197
|
|
|
@click.argument('subjectid') |
|
198
|
|
|
def dropSubject(projectid, subjectid): |
|
|
|
|
|
|
199
|
|
|
""" |
|
200
|
|
|
Delete a subject. |
|
201
|
|
|
|
|
202
|
|
|
USAGE: annif drop-subject <projectid> <subjectid> |
|
203
|
|
|
|
|
204
|
|
|
REST equivalent: |
|
205
|
|
|
|
|
206
|
|
|
DELETE /projects/<projectid>/subjects/<subjectid> |
|
207
|
|
|
|
|
208
|
|
|
""" |
|
209
|
|
|
pass |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
@annif.cli.command('analyze') |
|
213
|
|
|
@click.option('--maxhits', default=20) |
|
214
|
|
|
@click.option('--threshold', default=0.9) # TODO: Check this. |
|
|
|
|
|
|
215
|
|
|
def analyze(projectid, maxhits, threshold): |
|
|
|
|
|
|
216
|
|
|
"""" |
|
217
|
|
|
Delete a subject. |
|
218
|
|
|
|
|
219
|
|
|
USAGE: annif drop-subject <projectid> <subjectid> |
|
220
|
|
|
|
|
221
|
|
|
REST equivalent: |
|
222
|
|
|
|
|
223
|
|
|
DELETE /projects/<projectid>/subjects/<subjectid> |
|
224
|
|
|
|
|
225
|
|
|
""" |
|
226
|
|
|
pass |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
@annif.route('/') |
|
230
|
|
|
def start(): |
|
|
|
|
|
|
231
|
|
|
return 'Started application' |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
if __name__ == "__main__": |
|
235
|
|
|
annif.run(port=8000) |
|
236
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.