Passed
Pull Request — master (#461)
by
unknown
02:49
created

annif.backend.yake   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 151
dl 0
loc 185
rs 9.84
c 0
b 0
f 0
wmc 32

17 Methods

Rating   Name   Duplication   Size   Complexity  
A YakeBackend.initialize() 0 2 1
A YakeBackend.default_params() 0 4 1
A YakeBackend._validate_label_types() 0 5 3
A YakeBackend.label_types() 0 9 2
A YakeBackend.is_trained() 0 3 1
A YakeBackend._initialize_index() 0 12 3
A YakeBackend._normalize_phrase() 0 6 2
A YakeBackend._create_index() 0 12 3
A YakeBackend._sort_phrase() 0 3 1
A YakeBackend._combine_scores() 0 6 1
A YakeBackend._combine_suggestions() 0 10 3
A YakeBackend._save_index() 0 6 1
A YakeBackend._keyphrases2suggestions() 0 16 5
A YakeBackend._keyphrase2uris() 0 4 1
A YakeBackend._transform_score() 0 3 1
A YakeBackend._suggest() 0 24 1
A YakeBackend._normalize_label() 0 6 2
1
"""Annif backend using Yake keyword extraction"""
2
# For license remarks of this backend see README.md:
3
# https://github.com/NatLibFi/Annif#license.
4
5
import yake
6
import joblib
7
import os.path
8
import re
9
from collections import defaultdict
10
from rdflib.namespace import SKOS
11
import annif.util
12
from . import backend
13
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
14
from annif.exception import ConfigurationException
15
16
17
class YakeBackend(backend.AnnifBackend):
18
    """Yake based backend for Annif"""
19
    name = "yake"
20
    needs_subject_index = False
21
22
    # defaults for uninitialized instances
23
    _index = None
24
    _graph = None
25
    INDEX_FILE = 'yake-index'
26
27
    DEFAULT_PARAMETERS = {
28
        'max_ngram_size': 4,
29
        'deduplication_threshold': 0.9,
30
        'deduplication_algo': 'levs',
31
        'window_size': 1,
32
        'num_keywords': 100,
33
        'features': None,
34
        'label_types': ['prefLabel', 'altLabel'],
35
        'remove_parentheses': False
36
    }
37
38
    def default_params(self):
39
        params = backend.AnnifBackend.DEFAULT_PARAMETERS.copy()
40
        params.update(self.DEFAULT_PARAMETERS)
41
        return params
42
43
    @property
44
    def is_trained(self):
45
        return True
46
47
    @property
48
    def label_types(self):
49
        if type(self.params['label_types']) == str:  # Label types set by user
50
            label_types = [lt.strip() for lt
51
                           in self.params['label_types'].split(',')]
52
            self._validate_label_types(label_types)
53
        else:
54
            label_types = self.params['label_types']  # The defaults
55
        return [getattr(SKOS, lt) for lt in label_types]
56
57
    def _validate_label_types(self, label_types):
58
        for lt in label_types:
59
            if lt not in ('prefLabel', 'altLabel', 'hiddenLabel'):
60
                raise ConfigurationException(
61
                    f'invalid label type {lt}', backend_id=self.backend_id)
62
63
    def initialize(self):
64
        self._initialize_index()
65
66
    def _initialize_index(self):
67
        if self._index is None:
68
            path = os.path.join(self.datadir, self.INDEX_FILE)
69
            if os.path.exists(path):
70
                self._index = joblib.load(path)
71
                self.debug(
72
                    f'Loaded index from {path} with {len(self._index)} labels')
73
            else:
74
                self.info('Creating index')
75
                self._index = self._create_index()
76
                self._save_index(path)
77
                self.info(f'Created index with {len(self._index)} labels')
78
79
    def _save_index(self, path):
80
        annif.util.atomic_save(
81
            self._index,
82
            self.datadir,
83
            self.INDEX_FILE,
84
            method=joblib.dump)
85
86
    def _create_index(self):
87
        index = defaultdict(set)
88
        skos_vocab = self.project.vocab.skos
89
        for concept in skos_vocab.concepts:
90
            uri = str(concept)
91
            labels = skos_vocab.get_concept_labels(
92
                concept, self.label_types, self.params['language'])
93
            for label in labels:
94
                label = self._normalize_label(label)
95
                index[label].add(uri)
96
        index.pop('', None)  # Remove possible empty string entry
97
        return dict(index)
98
99
    def _normalize_label(self, label):
100
        label = str(label)
101
        if annif.util.boolean(self.params['remove_parentheses']):
102
            label = re.sub(r' \(.*\)', '', label)
103
        normalized_label = self._normalize_phrase(label)
104
        return self._sort_phrase(normalized_label)
105
106
    def _normalize_phrase(self, phrase):
107
        normalized = []
108
        for word in phrase.split():
109
            normalized.append(
110
                self.project.analyzer.normalize_word(word).lower())
111
        return ' '.join(normalized)
112
113
    def _sort_phrase(self, phrase):
114
        words = phrase.split()
115
        return ' '.join(sorted(words))
116
117
    def _suggest(self, text, params):
118
        self.debug(
119
            f'Suggesting subjects for text "{text[:20]}..." (len={len(text)})')
120
        limit = int(params['limit'])
121
122
        self._kw_extractor = yake.KeywordExtractor(
123
            lan=params['language'],
124
            n=int(params['max_ngram_size']),
125
            dedupLim=float(params['deduplication_threshold']),
126
            dedupFunc=params['deduplication_algo'],
127
            windowsSize=int(params['window_size']),
128
            top=int(params['num_keywords']),
129
            features=self.params['features'])
130
        keyphrases = self._kw_extractor.extract_keywords(text)
131
        suggestions = self._keyphrases2suggestions(keyphrases)
132
133
        subject_suggestions = [SubjectSuggestion(
134
                uri=uri,
135
                label=None,
136
                notation=None,
137
                score=score)
138
                for uri, score in suggestions[:limit] if score > 0.0]
139
        return ListSuggestionResult.create_from_index(subject_suggestions,
140
                                                      self.project.subjects)
141
142
    def _keyphrases2suggestions(self, keyphrases):
143
        suggestions = []
144
        not_matched = []
145
        for kp, score in keyphrases:
146
            uris = self._keyphrase2uris(kp)
147
            for uri in uris:
148
                suggestions.append(
149
                    (uri, self._transform_score(score)))
150
            if not uris:
151
                not_matched.append((kp, self._transform_score(score)))
152
        # Remove duplicate uris, conflating the scores
153
        suggestions = self._combine_suggestions(suggestions)
154
        self.debug('Keyphrases not matched:\n' + '\t'.join(
155
            [kp[0] + ' ' + str(kp[1]) for kp
156
             in sorted(not_matched, reverse=True, key=lambda kp: kp[1])]))
157
        return suggestions
158
159
    def _keyphrase2uris(self, keyphrase):
160
        keyphrase = self._normalize_phrase(keyphrase)
161
        keyphrase = self._sort_phrase(keyphrase)
162
        return self._index.get(keyphrase, [])
163
164
    def _transform_score(self, score):
165
        score = max(score, 0)
166
        return 1.0 / (score + 1)
167
168
    def _combine_suggestions(self, suggestions):
169
        combined_suggestions = {}
170
        for uri, score in suggestions:
171
            if uri not in combined_suggestions:
172
                combined_suggestions[uri] = score
173
            else:
174
                old_score = combined_suggestions[uri]
175
                combined_suggestions[uri] = self._combine_scores(
176
                    score, old_score)
177
        return list(combined_suggestions.items())
178
179
    def _combine_scores(self, score1, score2):
180
        # The result is never smaller than the greater input
181
        score1 = score1/2 + 0.5
182
        score2 = score2/2 + 0.5
183
        confl = score1 * score2 / (score1 * score2 + (1-score1) * (1-score2))
184
        return (confl-0.5) * 2
185