|
1
|
|
|
import os |
|
2
|
|
|
from stwfsapy.predictor import StwfsapyPredictor |
|
3
|
|
|
from annif.exception import NotInitializedException, NotSupportedException |
|
4
|
|
|
from annif.suggestion import ListSuggestionResult, SubjectSuggestion |
|
5
|
|
|
from . import backend |
|
6
|
|
|
from annif.util import atomic_save, boolean |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
_KEY_CONCEPT_TYPE_URI = 'concept_type_uri' |
|
10
|
|
|
_KEY_SUBTHESAURUS_TYPE_URI = 'sub_thesaurus_type_uri' |
|
11
|
|
|
_KEY_THESAURUS_RELATION_TYPE_URI = 'thesaurus_relation_type_uri' |
|
12
|
|
|
_KEY_THESAURUS_RELATION_IS_SPECIALISATION = ( |
|
13
|
|
|
'thesaurus_relation_is_specialisation') |
|
14
|
|
|
_KEY_REMOVE_DEPRECATED = 'remove_deprecated' |
|
15
|
|
|
_KEY_HANDLE_TITLE_CASE = 'handle_title_case' |
|
16
|
|
|
_KEY_EXTRACT_UPPER_CASE_FROM_BRACES = 'extract_upper_case_from_braces' |
|
17
|
|
|
_KEY_EXTRACT_ANY_CASE_FROM_BRACES = 'extract_any_case_from_braces' |
|
18
|
|
|
_KEY_EXPAND_AMPERSAND_WITH_SPACES = 'expand_ampersand_with_spaces' |
|
19
|
|
|
_KEY_EXPAND_ABBREVIATION_WITH_PUNCTUATION = ( |
|
20
|
|
|
'expand_abbreviation_with_punctuation') |
|
21
|
|
|
_KEY_SIMPLE_ENGLISH_PLURAL_RULES = 'simple_english_plural_rules' |
|
22
|
|
|
_KEY_INPUT_LIMIT = 'input_limit' |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class StwfsaBackend(backend.AnnifBackend): |
|
26
|
|
|
|
|
27
|
|
|
name = "stwfsa" |
|
28
|
|
|
needs_subject_index = True |
|
29
|
|
|
|
|
30
|
|
|
STWFSA_PARAMETERS = { |
|
31
|
|
|
_KEY_CONCEPT_TYPE_URI: str, |
|
32
|
|
|
_KEY_SUBTHESAURUS_TYPE_URI: str, |
|
33
|
|
|
_KEY_THESAURUS_RELATION_TYPE_URI: str, |
|
34
|
|
|
_KEY_THESAURUS_RELATION_IS_SPECIALISATION: boolean, |
|
35
|
|
|
_KEY_REMOVE_DEPRECATED: boolean, |
|
36
|
|
|
_KEY_HANDLE_TITLE_CASE: boolean, |
|
37
|
|
|
_KEY_EXTRACT_UPPER_CASE_FROM_BRACES: boolean, |
|
38
|
|
|
_KEY_EXTRACT_ANY_CASE_FROM_BRACES: boolean, |
|
39
|
|
|
_KEY_EXPAND_AMPERSAND_WITH_SPACES: boolean, |
|
40
|
|
|
_KEY_EXPAND_ABBREVIATION_WITH_PUNCTUATION: boolean, |
|
41
|
|
|
_KEY_SIMPLE_ENGLISH_PLURAL_RULES: boolean, |
|
42
|
|
|
_KEY_INPUT_LIMIT: int, |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
DEFAULT_PARAMETERS = { |
|
46
|
|
|
_KEY_CONCEPT_TYPE_URI: 'http://www.w3.org/2004/02/skos/core#Concept', |
|
47
|
|
|
_KEY_SUBTHESAURUS_TYPE_URI: |
|
48
|
|
|
'http://www.w3.org/2004/02/skos/core#Collection', |
|
49
|
|
|
_KEY_THESAURUS_RELATION_TYPE_URI: |
|
50
|
|
|
'http://www.w3.org/2004/02/skos/core#member', |
|
51
|
|
|
_KEY_THESAURUS_RELATION_IS_SPECIALISATION: True, |
|
52
|
|
|
_KEY_REMOVE_DEPRECATED: True, |
|
53
|
|
|
_KEY_HANDLE_TITLE_CASE: True, |
|
54
|
|
|
_KEY_EXTRACT_UPPER_CASE_FROM_BRACES: True, |
|
55
|
|
|
_KEY_EXTRACT_ANY_CASE_FROM_BRACES: False, |
|
56
|
|
|
_KEY_EXPAND_AMPERSAND_WITH_SPACES: True, |
|
57
|
|
|
_KEY_EXPAND_ABBREVIATION_WITH_PUNCTUATION: True, |
|
58
|
|
|
_KEY_SIMPLE_ENGLISH_PLURAL_RULES: False, |
|
59
|
|
|
_KEY_INPUT_LIMIT: 0, |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
MODEL_FILE = 'stwfsa_predictor.zip' |
|
63
|
|
|
|
|
64
|
|
|
_model = None |
|
65
|
|
|
|
|
66
|
|
|
def initialize(self): |
|
67
|
|
|
if self._model is None: |
|
68
|
|
|
path = os.path.join(self.datadir, self.MODEL_FILE) |
|
69
|
|
|
self.debug(f'Loading STWFSA model from {path}.') |
|
70
|
|
|
if os.path.exists(path): |
|
71
|
|
|
self._model = StwfsapyPredictor.load(path) |
|
72
|
|
|
self.debug('Loaded model.') |
|
73
|
|
|
else: |
|
74
|
|
|
raise NotInitializedException( |
|
75
|
|
|
f'Model not found at {path}', |
|
76
|
|
|
backend_id=self.backend_id) |
|
77
|
|
|
|
|
78
|
|
|
def _train(self, corpus, params): |
|
79
|
|
|
if corpus == 'cached': |
|
80
|
|
|
raise NotSupportedException( |
|
81
|
|
|
'Training stwfsa project from cached data not supported.') |
|
82
|
|
|
if corpus.is_empty(): |
|
83
|
|
|
raise NotSupportedException( |
|
84
|
|
|
'Cannot train stwfsa project with no documents.') |
|
85
|
|
|
self.debug("Transforming training data.") |
|
86
|
|
|
X = [] |
|
87
|
|
|
y = [] |
|
88
|
|
|
for doc in corpus.documents: |
|
89
|
|
|
X.append(doc.text) |
|
90
|
|
|
y.append(doc.uris) |
|
91
|
|
|
graph = self.project.vocab.as_graph() |
|
92
|
|
|
new_params = { |
|
93
|
|
|
key: self.STWFSA_PARAMETERS[key](val) |
|
94
|
|
|
for key, val |
|
95
|
|
|
in params.items() |
|
96
|
|
|
if key in self.STWFSA_PARAMETERS |
|
97
|
|
|
} |
|
98
|
|
|
new_params.pop(_KEY_INPUT_LIMIT) |
|
99
|
|
|
p = StwfsapyPredictor( |
|
100
|
|
|
graph=graph, |
|
101
|
|
|
langs=frozenset([params['language']]), |
|
102
|
|
|
**new_params) |
|
103
|
|
|
p.fit(X, y) |
|
104
|
|
|
self._model = p |
|
105
|
|
|
atomic_save( |
|
106
|
|
|
p, |
|
107
|
|
|
self.datadir, |
|
108
|
|
|
self.MODEL_FILE, |
|
109
|
|
|
lambda model, store_path: model.store(store_path)) |
|
110
|
|
|
|
|
111
|
|
|
def _suggest(self, text, params): |
|
112
|
|
|
self.debug( |
|
113
|
|
|
f'Suggesting subjects for text "{text[:20]}..." (len={len(text)})') |
|
114
|
|
|
result = self._model.suggest_proba([text])[0] |
|
115
|
|
|
suggestions = [] |
|
116
|
|
|
for uri, score in result: |
|
117
|
|
|
subject_id = self.project.subjects.by_uri(uri) |
|
118
|
|
|
if subject_id: |
|
119
|
|
|
label = self.project.subjects[subject_id][1] |
|
120
|
|
|
else: |
|
121
|
|
|
label = None |
|
122
|
|
|
suggestion = SubjectSuggestion( |
|
123
|
|
|
uri, |
|
124
|
|
|
label, |
|
125
|
|
|
None, |
|
126
|
|
|
score) |
|
127
|
|
|
suggestions.append(suggestion) |
|
128
|
|
|
return ListSuggestionResult(suggestions) |
|
129
|
|
|
|