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

from snips_inference_agl.common.from_dict import FromDict
from snips_inference_agl.constants import CUSTOM_ENTITY_PARSER_USAGE
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 NLUEngineConfig(FromDict, ProcessingUnitConfig):
    """Configuration of a :class:`.SnipsNLUEngine` object

    Args:
        intent_parsers_configs (list): List of intent parser configs
            (:class:`.ProcessingUnitConfig`). The order in the list determines
            the order in which each parser will be called by the nlu engine.
    """

    def __init__(self, intent_parsers_configs=None, random_seed=None):
        from snips_inference_agl.intent_parser import IntentParser

        if intent_parsers_configs is None:
            from snips_inference_agl.pipeline.configs import (
                ProbabilisticIntentParserConfig,
                DeterministicIntentParserConfig)
            intent_parsers_configs = [
                DeterministicIntentParserConfig(),
                ProbabilisticIntentParserConfig()
            ]
        self.intent_parsers_configs = [
            IntentParser.get_config(conf) for conf in intent_parsers_configs]
        self.random_seed = random_seed

    @property
    def unit_name(self):
        from snips_inference_agl.nlu_engine.nlu_engine import SnipsNLUEngine
        return SnipsNLUEngine.unit_name

    def get_required_resources(self):
        # Resolving custom slot values must be done without stemming
        resources = {
            CUSTOM_ENTITY_PARSER_USAGE: CustomEntityParserUsage.WITHOUT_STEMS
        }
        for config in self.intent_parsers_configs:
            resources = merge_required_resources(
                resources, config.get_required_resources())
        return resources

    def to_dict(self):
        return {
            "unit_name": self.unit_name,
            "intent_parsers_configs": [
                config.to_dict() for config in self.intent_parsers_configs
            ]
        }