aboutsummaryrefslogtreecommitdiffstats
path: root/snips_inference_agl/pipeline/configs/intent_parser.py
blob: f0174721d6e5cc95a4b80677404f3d6be153aa24 (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
from __future__ import unicode_literals

from snips_inference_agl.common.from_dict import FromDict
from snips_inference_agl.constants import CUSTOM_ENTITY_PARSER_USAGE, STOP_WORDS
from snips_inference_agl.entity_parser import CustomEntityParserUsage
from snips_inference_agl.pipeline.configs import ProcessingUnitConfig
from snips_inference_agl.resources import merge_required_resources


class ProbabilisticIntentParserConfig(FromDict, ProcessingUnitConfig):
    """Configuration of a :class:`.ProbabilisticIntentParser` object

    Args:
        intent_classifier_config (:class:`.ProcessingUnitConfig`): The
            configuration of the underlying intent classifier, by default
            it uses a :class:`.LogRegIntentClassifierConfig`
        slot_filler_config (:class:`.ProcessingUnitConfig`): The configuration
            that will be used for the underlying slot fillers, by default it
            uses a :class:`.CRFSlotFillerConfig`
    """

    def __init__(self, intent_classifier_config=None, slot_filler_config=None):
        from snips_inference_agl.intent_classifier import IntentClassifier
        from snips_inference_agl.slot_filler import SlotFiller

        if intent_classifier_config is None:
            from snips_inference_agl.pipeline.configs import LogRegIntentClassifierConfig
            intent_classifier_config = LogRegIntentClassifierConfig()
        if slot_filler_config is None:
            from snips_inference_agl.pipeline.configs import CRFSlotFillerConfig
            slot_filler_config = CRFSlotFillerConfig()
        self.intent_classifier_config = IntentClassifier.get_config(
            intent_classifier_config)
        self.slot_filler_config = SlotFiller.get_config(slot_filler_config)

    @property
    def unit_name(self):
        from snips_inference_agl.intent_parser import ProbabilisticIntentParser
        return ProbabilisticIntentParser.unit_name

    def get_required_resources(self):
        resources = self.intent_classifier_config.get_required_resources()
        resources = merge_required_resources(
            resources, self.slot_filler_config.get_required_resources())
        return resources

    def to_dict(self):
        return {
            "unit_name": self.unit_name,
            "slot_filler_config": self.slot_filler_config.to_dict(),
            "intent_classifier_config": self.intent_classifier_config.to_dict()
        }


class DeterministicIntentParserConfig(FromDict, ProcessingUnitConfig):
    """Configuration of a :class:`.DeterministicIntentParser`

    Args:
        max_queries (int, optional): Maximum number of regex patterns per
            intent. 50 by default.
        max_pattern_length (int, optional): Maximum length of regex patterns.
        ignore_stop_words (bool, optional): If True, stop words will be
            removed before building patterns.


    This allows to deactivate the usage of regular expression when they are
    too big to avoid explosion in time and memory

    Note:
        In the future, a FST will be used instead of regexps, removing the need
        for all this
    """

    def __init__(self, max_queries=100, max_pattern_length=1000,
                 ignore_stop_words=False):
        self.max_queries = max_queries
        self.max_pattern_length = max_pattern_length
        self.ignore_stop_words = ignore_stop_words

    @property
    def unit_name(self):
        from snips_inference_agl.intent_parser import DeterministicIntentParser
        return DeterministicIntentParser.unit_name

    def get_required_resources(self):
        return {
            CUSTOM_ENTITY_PARSER_USAGE: CustomEntityParserUsage.WITHOUT_STEMS,
            STOP_WORDS: self.ignore_stop_words
        }

    def to_dict(self):
        return {
            "unit_name": self.unit_name,
            "max_queries": self.max_queries,
            "max_pattern_length": self.max_pattern_length,
            "ignore_stop_words": self.ignore_stop_words
        }


class LookupIntentParserConfig(FromDict, ProcessingUnitConfig):
    """Configuration of a :class:`.LookupIntentParser`

    Args:
        ignore_stop_words (bool, optional): If True, stop words will be
            removed before building patterns.
    """

    def __init__(self, ignore_stop_words=False):
        self.ignore_stop_words = ignore_stop_words

    @property
    def unit_name(self):
        from snips_inference_agl.intent_parser.lookup_intent_parser import \
            LookupIntentParser
        return LookupIntentParser.unit_name

    def get_required_resources(self):
        return {
            CUSTOM_ENTITY_PARSER_USAGE: CustomEntityParserUsage.WITHOUT_STEMS,
            STOP_WORDS: self.ignore_stop_words
        }

    def to_dict(self):
        return {
            "unit_name": self.unit_name,
            "ignore_stop_words": self.ignore_stop_words
        }