<commit_before>from typing import Any, Dict, Optional, Text

from rasa_nlu.components import Component
from rasa_nlu.training_data import Message


class CustomKeywordIntentClassifier(Component):
    """Intent classifier using simple keyword matching.


    The classifier takes a list of keywords and associated intents as an input.
    A input sentence is checked for the keywords and the intent is returned.

    """

    name = "intent_classifier_custom_keyword"

    provides = ["intent"]

    intent_keyword_map = {}

    def train(self,
              training_data: 'TrainingData',
              cfg: Optional['RasaNLUModelConfig'] = None,
              **kwargs: Any) -> None:

        all_intents = list(training_data.intents)

        for intent in all_intents:
            self.intent_keyword_map[intent] = [example.text.lower() for example in training_data.intent_examples if example.data['intent'] == intent]


    def process(self, message: Message, **kwargs: Any) -> None:

        intent = {"name": self.parse(message.text), "confidence": 1.0}
        message.set("intent", intent,
                    add_to_output=True)


    def parse(self, text: Text) -> Optional[Text]:
        _text = text.lower()

        def is_present(x):
            return x in _text


        for intent in self.intent_keyword_map.keys():
            if any(map(is_present, self.intent_keyword_map[intent])):
                return intent

        # If none of the keywords is in the text:
        return None


    def persist(self, model_dir: Text) -> Dict[Text, Any]:
        import os
        import pickle

        keyword_file = os.path.join(model_dir, "keys.p")
        pickle.dump(self.intent_keyword_map,  open(keyword_file, "wb"))

        return {"keyword_file": keyword_file}

    @classmethod
    def load(self,
             model_dir: Optional[Text] = None,
             model_metadata: Optional['Metadata'] = None,
             cached_component: Optional['Component'] = None,
             **kwargs: Any) -> 'CustomKeywordIntentClassifier':

        import pickle
        self.intent_keyword_map = pickle.load(open(model_dir + "/keys.p", "rb"))

        return self()<commit_msg>Adding custom keyword intent classifier<commit_after>from typing import Any, Dict, Optional, Text

from rasa_nlu.components import Component
from rasa_nlu.training_data import Message
import pickle
import os


class CustomKeywordIntentClassifier(Component):
    """Intent classifier using simple keyword matching.


    The classifier takes a list of keywords and associated intents as an input.
    A input sentence is checked for the keywords and the intent is returned.

    """

    name = "intent_classifier_custom_keyword"

    provides = ["intent"]

    intent_keyword_map = {}

    def train(self,
              training_data: 'TrainingData',
              cfg: Optional['RasaNLUModelConfig'] = None,
              **kwargs: Any) -> None:

        all_intents = list(training_data.intents)

        for intent in all_intents:
            self.intent_keyword_map[intent] = [example.text.lower() for example in training_data.intent_examples if example.data['intent'] == intent]


    def process(self, message: Message, **kwargs: Any) -> None:

        intent = {"name": self.parse(message.text), "confidence": 1.0}
        message.set("intent", intent,
                    add_to_output=True)


    def parse(self, text: Text) -> Optional[Text]:
        _text = text.lower()

        def is_present(x):
            return x in _text


        for intent in self.intent_keyword_map.keys():
            if any(map(is_present, self.intent_keyword_map[intent])):
                return intent

        # If none of the keywords is in the text:
        return None


    def persist(self, model_dir: Text) -> Dict[Text, Any]:
        keyword_file = os.path.join(model_dir, "keys.p")
        pickle.dump(self.intent_keyword_map,  open(keyword_file, "wb"))

        return {"keyword_file": keyword_file}

    @classmethod
    def load(self,
             model_dir: Optional[Text] = None,
             model_metadata: Optional['Metadata'] = None,
             cached_component: Optional['Component'] = None,
             **kwargs: Any) -> 'CustomKeywordIntentClassifier':

        keyword_file = os.path.join(model_dir, "keys.p")

        self.intent_keyword_map = pickle.load(open(keyword_file, "rb"))

        return self()
{'fraction_non_alphanumeric': 0.10154125113327289, 'fraction_numerical': 0.0009066183136899365, 'mean_word_length': 2.6652823920265782, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7555204', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1205, 'n_words': 341}