aboutsummaryrefslogtreecommitdiffstats
path: root/snips_inference_agl/slot_filler/feature_factory.py
blob: 50f4598e1eb0c1c12ef498c72535e0625fcb45df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
from __future__ import unicode_literals

import logging

from abc import ABCMeta, abstractmethod
from builtins import str

from future.utils import with_metaclass

from snips_inference_agl.common.abc_utils import classproperty
from snips_inference_agl.common.registrable import Registrable
from snips_inference_agl.common.utils import check_random_state
from snips_inference_agl.constants import (
    CUSTOM_ENTITY_PARSER_USAGE, END, GAZETTEERS, LANGUAGE, RES_MATCH_RANGE,
    START, STEMS, WORD_CLUSTERS, CUSTOM_ENTITY_PARSER, BUILTIN_ENTITY_PARSER,
    RESOURCES, RANDOM_STATE, AUTOMATICALLY_EXTENSIBLE, ENTITIES)
from snips_inference_agl.dataset import (
    extract_intent_entities, get_dataset_gazetteer_entities)
from snips_inference_agl.entity_parser.builtin_entity_parser import is_builtin_entity
from snips_inference_agl.entity_parser.custom_entity_parser import (
    CustomEntityParserUsage)
from snips_inference_agl.languages import get_default_sep
from snips_inference_agl.preprocessing import Token, normalize_token, stem_token
from snips_inference_agl.resources import get_gazetteer, get_word_cluster
from snips_inference_agl.slot_filler.crf_utils import TaggingScheme, get_scheme_prefix
from snips_inference_agl.slot_filler.feature import Feature
from snips_inference_agl.slot_filler.features_utils import (
    entity_filter, get_word_chunk, initial_string_from_tokens)

logger = logging.getLogger(__name__)


class CRFFeatureFactory(with_metaclass(ABCMeta, Registrable)):
    """Abstraction to implement to build CRF features

    A :class:`CRFFeatureFactory` is initialized with a dict which describes
    the feature, it must contains the three following keys:

    -   'factory_name'
    -   'args': the parameters of the feature, if any
    -   'offsets': the offsets to consider when using the feature in the CRF.
        An empty list corresponds to no feature.


    In addition, a 'drop_out' to use at training time can be specified.
    """

    def __init__(self, factory_config, **shared):
        self.factory_config = factory_config
        self.resources = shared.get(RESOURCES)
        self.builtin_entity_parser = shared.get(BUILTIN_ENTITY_PARSER)
        self.custom_entity_parser = shared.get(CUSTOM_ENTITY_PARSER)
        self.random_state = check_random_state(shared.get(RANDOM_STATE))

    @classmethod
    def from_config(cls, factory_config, **shared):
        """Retrieve the :class:`CRFFeatureFactory` corresponding the provided
        config

        Raises:
            NotRegisteredError: when the factory is not registered
        """
        factory_name = factory_config["factory_name"]
        factory = cls.by_name(factory_name)
        return factory(factory_config, **shared)

    @classproperty
    def name(cls):  # pylint:disable=no-self-argument
        return CRFFeatureFactory.registered_name(cls)

    @property
    def args(self):
        return self.factory_config["args"]

    @property
    def offsets(self):
        return self.factory_config["offsets"]

    @property
    def drop_out(self):
        return self.factory_config.get("drop_out", 0.0)

    def fit(self, dataset, intent):  # pylint: disable=unused-argument
        """Fit the factory, if needed, with the provided *dataset* and *intent*
        """
        return self

    @abstractmethod
    def build_features(self):
        """Build a list of :class:`.Feature`"""
        pass

    def get_required_resources(self):
        return None


class SingleFeatureFactory(with_metaclass(ABCMeta, CRFFeatureFactory)):
    """A CRF feature factory which produces only one feature"""

    @property
    def feature_name(self):
        # by default, use the factory name
        return self.name

    @abstractmethod
    def compute_feature(self, tokens, token_index):
        pass

    def build_features(self):
        return [
            Feature(
                base_name=self.feature_name,
                func=self.compute_feature,
                offset=offset,
                drop_out=self.drop_out) for offset in self.offsets
        ]


@CRFFeatureFactory.register("is_digit")
class IsDigitFactory(SingleFeatureFactory):
    """Feature: is the considered token a digit?"""

    def compute_feature(self, tokens, token_index):
        return "1" if tokens[token_index].value.isdigit() else None


@CRFFeatureFactory.register("is_first")
class IsFirstFactory(SingleFeatureFactory):
    """Feature: is the considered token the first in the input?"""

    def compute_feature(self, tokens, token_index):
        return "1" if token_index == 0 else None


@CRFFeatureFactory.register("is_last")
class IsLastFactory(SingleFeatureFactory):
    """Feature: is the considered token the last in the input?"""

    def compute_feature(self, tokens, token_index):
        return "1" if token_index == len(tokens) - 1 else None


@CRFFeatureFactory.register("ngram")
class NgramFactory(SingleFeatureFactory):
    """Feature: the n-gram consisting of the considered token and potentially
    the following ones

    This feature has several parameters:

    -   'n' (int): Corresponds to the size of the n-gram. n=1 corresponds to a
        unigram, n=2 is a bigram etc
    -   'use_stemming' (bool): Whether or not to stem the n-gram
    -   'common_words_gazetteer_name' (str, optional): If defined, use a
        gazetteer of common words and replace out-of-corpus ngram with the
        alias
        'rare_word'

    """

    def __init__(self, factory_config, **shared):
        super(NgramFactory, self).__init__(factory_config, **shared)
        self.n = self.args["n"]
        if self.n < 1:
            raise ValueError("n should be >= 1")

        self.use_stemming = self.args["use_stemming"]
        self.common_words_gazetteer_name = self.args[
            "common_words_gazetteer_name"]
        self._gazetteer = None
        self._language = None
        self.language = self.args.get("language_code")

    @property
    def language(self):
        return self._language

    @language.setter
    def language(self, value):
        if value is not None:
            self._language = value
            self.args["language_code"] = self.language

    @property
    def gazetteer(self):
        # Load the gazetteer lazily
        if self.common_words_gazetteer_name is None:
            return None
        if self._gazetteer is None:
            self._gazetteer = get_gazetteer(
                self.resources, self.common_words_gazetteer_name)
        return self._gazetteer

    @property
    def feature_name(self):
        return "ngram_%s" % self.n

    def fit(self, dataset, intent):
        self.language = dataset[LANGUAGE]

    def compute_feature(self, tokens, token_index):
        max_len = len(tokens)
        end = token_index + self.n
        if 0 <= token_index < max_len and end <= max_len:
            if self.gazetteer is None:
                if self.use_stemming:
                    stems = (stem_token(t, self.resources)
                             for t in tokens[token_index:end])
                    return get_default_sep(self.language).join(stems)
                normalized_values = (normalize_token(t)
                                     for t in tokens[token_index:end])
                return get_default_sep(self.language).join(normalized_values)
            words = []
            for t in tokens[token_index:end]:
                if self.use_stemming:
                    value = stem_token(t, self.resources)
                else:
                    value = normalize_token(t)
                words.append(value if value in self.gazetteer else "rare_word")
            return get_default_sep(self.language).join(words)
        return None

    def get_required_resources(self):
        resources = dict()
        if self.common_words_gazetteer_name is not None:
            resources[GAZETTEERS] = {self.common_words_gazetteer_name}
        if self.use_stemming:
            resources[STEMS] = True
        return resources


@CRFFeatureFactory.register("shape_ngram")
class ShapeNgramFactory(SingleFeatureFactory):
    """Feature: the shape of the n-gram consisting of the considered token and
    potentially the following ones

    This feature has one parameters, *n*, which corresponds to the size of the
    n-gram.

    Possible types of shape are:

        -   'xxx' -> lowercased
        -   'Xxx' -> Capitalized
        -   'XXX' -> UPPERCASED
        -   'xX' -> None of the above
    """

    def __init__(self, factory_config, **shared):
        super(ShapeNgramFactory, self).__init__(factory_config, **shared)
        self.n = self.args["n"]
        if self.n < 1:
            raise ValueError("n should be >= 1")
        self._language = None
        self.language = self.args.get("language_code")

    @property
    def language(self):
        return self._language

    @language.setter
    def language(self, value):
        if value is not None:
            self._language = value
            self.args["language_code"] = value

    @property
    def feature_name(self):
        return "shape_ngram_%s" % self.n

    def fit(self, dataset, intent):
        self.language = dataset[LANGUAGE]

    def compute_feature(self, tokens, token_index):
        from snips_nlu_utils import get_shape

        max_len = len(tokens)
        end = token_index + self.n
        if 0 <= token_index < max_len and end <= max_len:
            return get_default_sep(self.language).join(
                get_shape(t.value) for t in tokens[token_index:end])
        return None


@CRFFeatureFactory.register("word_cluster")
class WordClusterFactory(SingleFeatureFactory):
    """Feature: The cluster which the considered token belongs to, if any

    This feature has several parameters:

    -   'cluster_name' (str): the name of the word cluster to use
    -   'use_stemming' (bool): whether or not to stem the token before looking
        for its cluster

    Typical words clusters are the Brown Clusters in which words are
    clustered into a binary tree resulting in clusters of the form '100111001'
    See https://en.wikipedia.org/wiki/Brown_clustering
    """

    def __init__(self, factory_config, **shared):
        super(WordClusterFactory, self).__init__(factory_config, **shared)
        self.cluster_name = self.args["cluster_name"]
        self.use_stemming = self.args["use_stemming"]
        self._cluster = None

    @property
    def cluster(self):
        if self._cluster is None:
            self._cluster = get_word_cluster(self.resources, self.cluster_name)
        return self._cluster

    @property
    def feature_name(self):
        return "word_cluster_%s" % self.cluster_name

    def compute_feature(self, tokens, token_index):
        if self.use_stemming:
            value = stem_token(tokens[token_index], self.resources)
        else:
            value = normalize_token(tokens[token_index])
        return self.cluster.get(value, None)

    def get_required_resources(self):
        return {
            WORD_CLUSTERS: {self.cluster_name},
            STEMS: self.use_stemming
        }


@CRFFeatureFactory.register("entity_match")
class CustomEntityMatchFactory(CRFFeatureFactory):
    """Features: does the considered token belongs to the values of one of the
    entities in the training dataset

    This factory builds as many features as there are entities in the dataset,
    one per entity.

    It has the following parameters:

    -   'use_stemming' (bool): whether or not to stem the token before looking
        for it among the (stemmed) entity values
    -   'tagging_scheme_code' (int): Represents a :class:`.TaggingScheme`. This
        allows to give more information about the match.
    -   'entity_filter' (dict): a filter applied to select the custom entities
        for which the custom match feature will be computed. Available
        filters:
        - 'automatically_extensible': if True, selects automatically
        extensible entities only, if False selects non automatically
        extensible entities only
    """

    def __init__(self, factory_config, **shared):
        super(CustomEntityMatchFactory, self).__init__(factory_config,
                                                       **shared)
        self.use_stemming = self.args["use_stemming"]
        self.tagging_scheme = TaggingScheme(
            self.args["tagging_scheme_code"])
        self._entities = None
        self.entities = self.args.get("entities")
        ent_filter = self.args.get("entity_filter")
        if ent_filter:
            try:
                _check_custom_entity_filter(ent_filter)
            except _InvalidCustomEntityFilter as e:
                logger.warning(
                    "Invalid filter '%s', invalid arguments have been ignored:"
                    " %s", ent_filter, e,
                )
        self.entity_filter = ent_filter or dict()

    @property
    def entities(self):
        return self._entities

    @entities.setter
    def entities(self, value):
        if value is not None:
            self._entities = value
            self.args["entities"] = value

    def fit(self, dataset, intent):
        entities_names = extract_intent_entities(
            dataset, lambda e: not is_builtin_entity(e))[intent]
        extensible = self.entity_filter.get(AUTOMATICALLY_EXTENSIBLE)
        if extensible is not None:
            entities_names = [
                e for e in entities_names
                if dataset[ENTITIES][e][AUTOMATICALLY_EXTENSIBLE] == extensible
            ]
        self.entities = list(entities_names)
        return self

    def _transform(self, tokens):
        if self.use_stemming:
            light_tokens = (stem_token(t, self.resources) for t in tokens)
        else:
            light_tokens = (normalize_token(t) for t in tokens)
        current_index = 0
        transformed_tokens = []
        for light_token in light_tokens:
            transformed_token = Token(
                value=light_token,
                start=current_index,
                end=current_index + len(light_token))
            transformed_tokens.append(transformed_token)
            current_index = transformed_token.end + 1
        return transformed_tokens

    def build_features(self):
        features = []
        for entity_name in self.entities:
            # We need to call this wrapper in order to properly capture
            # `entity_name`
            entity_match = self._build_entity_match_fn(entity_name)

            for offset in self.offsets:
                feature = Feature("entity_match_%s" % entity_name,
                                  entity_match, offset, self.drop_out)
                features.append(feature)
        return features

    def _build_entity_match_fn(self, entity):

        def entity_match(tokens, token_index):
            transformed_tokens = self._transform(tokens)
            text = initial_string_from_tokens(transformed_tokens)
            token_start = transformed_tokens[token_index].start
            token_end = transformed_tokens[token_index].end
            custom_entities = self.custom_entity_parser.parse(
                text, scope=[entity], use_cache=True)
            # only keep builtin entities (of type `entity`) which overlap with
            # the current token
            custom_entities = [ent for ent in custom_entities
                               if entity_filter(ent, token_start, token_end)]
            if custom_entities:
                # In most cases, 0 or 1 entity will be found. We fall back to
                # the first entity if 2 or more were found
                ent = custom_entities[0]
                indexes = []
                for index, token in enumerate(transformed_tokens):
                    if entity_filter(ent, token.start, token.end):
                        indexes.append(index)
                return get_scheme_prefix(token_index, indexes,
                                         self.tagging_scheme)
            return None

        return entity_match

    def get_required_resources(self):
        if self.use_stemming:
            return {
                STEMS: True,
                CUSTOM_ENTITY_PARSER_USAGE: CustomEntityParserUsage.WITH_STEMS
            }
        return {
            STEMS: False,
            CUSTOM_ENTITY_PARSER_USAGE:
                CustomEntityParserUsage.WITHOUT_STEMS
        }


class _InvalidCustomEntityFilter(ValueError):
    pass


CUSTOM_ENTITIES_FILTER_KEYS = {"automatically_extensible"}


# pylint: disable=redefined-outer-name
def _check_custom_entity_filter(entity_filter):
    for k in entity_filter:
        if k not in CUSTOM_ENTITIES_FILTER_KEYS:
            msg = "Invalid custom entity filter key '%s'. Accepted filter " \
                  "keys are %s" % (k, list(CUSTOM_ENTITIES_FILTER_KEYS))
            raise _InvalidCustomEntityFilter(msg)


@CRFFeatureFactory.register("builtin_entity_match")
class BuiltinEntityMatchFactory(CRFFeatureFactory):
    """Features: is the considered token part of a builtin entity such as a
    date, a temperature etc

    This factory builds as many features as there are builtin entities
    available in the considered language.

    It has one parameter, *tagging_scheme_code*, which represents a
    :class:`.TaggingScheme`. This allows to give more information about the
    match.
    """

    def __init__(self, factory_config, **shared):
        super(BuiltinEntityMatchFactory, self).__init__(factory_config,
                                                        **shared)
        self.tagging_scheme = TaggingScheme(
            self.args["tagging_scheme_code"])
        self.builtin_entities = None
        self.builtin_entities = self.args.get("entity_labels")
        self._language = None
        self.language = self.args.get("language_code")

    @property
    def language(self):
        return self._language

    @language.setter
    def language(self, value):
        if value is not None:
            self._language = value
            self.args["language_code"] = self.language

    def fit(self, dataset, intent):
        self.language = dataset[LANGUAGE]
        self.builtin_entities = sorted(
            self._get_builtin_entity_scope(dataset, intent))
        self.args["entity_labels"] = self.builtin_entities

    def build_features(self):
        features = []

        for builtin_entity in self.builtin_entities:
            # We need to call this wrapper in order to properly capture
            # `builtin_entity`
            builtin_entity_match = self._build_entity_match_fn(builtin_entity)
            for offset in self.offsets:
                feature_name = "builtin_entity_match_%s" % builtin_entity
                feature = Feature(feature_name, builtin_entity_match, offset,
                                  self.drop_out)
                features.append(feature)

        return features

    def _build_entity_match_fn(self, builtin_entity):

        def builtin_entity_match(tokens, token_index):
            text = initial_string_from_tokens(tokens)
            start = tokens[token_index].start
            end = tokens[token_index].end

            builtin_entities = self.builtin_entity_parser.parse(
                text, scope=[builtin_entity], use_cache=True)
            # only keep builtin entities (of type `builtin_entity`) which
            # overlap with the current token
            builtin_entities = [ent for ent in builtin_entities
                                if entity_filter(ent, start, end)]
            if builtin_entities:
                # In most cases, 0 or 1 entity will be found. We fall back to
                # the first entity if 2 or more were found
                ent = builtin_entities[0]
                entity_start = ent[RES_MATCH_RANGE][START]
                entity_end = ent[RES_MATCH_RANGE][END]
                indexes = []
                for index, token in enumerate(tokens):
                    if (entity_start <= token.start < entity_end) \
                            and (entity_start < token.end <= entity_end):
                        indexes.append(index)
                return get_scheme_prefix(token_index, indexes,
                                         self.tagging_scheme)
            return None

        return builtin_entity_match

    @staticmethod
    def _get_builtin_entity_scope(dataset, intent=None):
        from snips_nlu_parsers import get_supported_grammar_entities

        language = dataset[LANGUAGE]
        grammar_entities = list(get_supported_grammar_entities(language))
        gazetteer_entities = list(
            get_dataset_gazetteer_entities(dataset, intent))
        return grammar_entities + gazetteer_entities