Exemplo n.º 1
0
def test_entity_train(name):
    chubot = ChuBotBrain(name, language='vi')
    chubot.load_data(
        "/media/nvidia/ssd/catkin_ws/src/chu_bot_source/chubot/usingdata/full_train.json"
    )
    # chubot.load_data("usingdata/vi_nlu_ask_way.json")
    meta = chubot.train_nercrf()
Exemplo n.º 2
0
def test_train_models():
    botname = "greet_en"
    nludatafile = "/media/nvidia/ssd/catkin_ws/src/chu_bot_source/chubot/data/nlu_greet.json"

    chubot = ChuBotBrain(botname, language='en')
    chubot.load_data(nludatafile)
    meta = chubot.train()
    print(meta)
Exemplo n.º 3
0
 def __init__(self, botname, language='vi'):
     self.botname = botname
     self.chubot = ChuBotBrain(botname, language)
     self.followup_actions = None
     self.action_templates = None
     self.action_custom = None
     self.slots = None
     self.start_flag = True
     self.active_entities = {}
Exemplo n.º 4
0
def test_nlu_predict():
    nludatafile = "/media/nvidia/ssd/catkin_ws/src/chu_bot_source/chubot/data/nlu_greet.json"

    chubot = ChuBotBrain("greet_en", language='en')

    inmessage = "a bird and a dog"
    tagged_entities = chubot.predict_entity(inmessage)
    print(inmessage)
    print(tagged_entities)

    inmessage = "sad but great. want a dog picture"
    intent_probs = chubot.predict_intent(inmessage)
    print(inmessage)
    print(intent_probs)
Exemplo n.º 5
0
def retrieve_image_deplicated(action_args, **kwargs):

    #TODO make a synonym mapper class

    def get_default_synonym(entity, value, entity_ldict):
        for ent in entity_ldict:
            if entity == ent['entity']:
                if value in ent['synonyms']:
                    return ent['default_value']

    nludatafile = "data/nlu_greet.json"

    chubot = ChuBotBrain("greet_en", language='en')
    chubot.load_data(nludatafile)
    entity_synonyms = chubot.entity_synonyms

    #handle arguments
    arg_entity = action_args["entity"]
    arg_value_type = action_args["value"]

    active_entities = kwargs["active_entities"]

    for ent in active_entities:
        if ent['entity'] == arg_entity:
            if arg_value_type == "value":
                arg_value = ent['value']
            elif arg_value_type == "default_value":
                arg_value = get_default_synonym(ent['entity'], ent['value'], entity_synonyms)

    print(arg_value)
    # print('http://shibe.online/api/{}?count=1&urls=true&httpsUrls=true'.format(arg_value))
    r = requests.get('http://shibe.online/api/{}?count=1&urls=true&httpsUrls=true'.format(arg_value))

    response = r.content.decode()
    response = response.replace('["', '')
    response = response.replace('"]', '')

    return response
Exemplo n.º 6
0
def test_intent_classification():
    nludatafile = "/media/nvidia/ssd/catkin_ws/src/chu_bot_source/chubot/data/nlu_greet.json"

    chubot = ChuBotBrain("greet_en", language='en')
    chubot.load_data(nludatafile)
    meta = chubot.train_intent_classification()
    print(meta)

    inmessage = "sad but great. want a dog picture"
    intent_probs = chubot.predict_intent(inmessage)
    print(intent_probs)
Exemplo n.º 7
0
def test_nercrf():
    nludatafile = "/media/nvidia/ssd/catkin_ws/src/chu_bot_source/chubot/data/nlu_greet.json"

    chubot = ChuBotBrain("greet_en", language='en')
    chubot.load_data(nludatafile)

    meta = chubot.train_nercrf()
    print(meta)

    inmessage = "a bird and a dog"

    tagged_entities = chubot.predict_entity(inmessage)

    print(tagged_entities)
Exemplo n.º 8
0
def test_intent_train(name):
    chubot = ChuBotBrain(name, language='vi')
    chubot.load_data("data/train.json")
    # chubot.load_data("data/vi_nlu_ask_way.json")
    meta = chubot.train_intent_classification()
    test_link = "data/test.txt"
    with open(test_link,'r',encoding='utf=8') as f:
        rows = f.readlines()
    intent_list_test = []
    intent_list_result = []
    data_list_test = []
    for row in rows:
        parts = row.split(',')
        intent_list_test.append(encode_intent(parts[0]))
        data_list_test.append(parts[1])
    for data in data_list_test:
        responses = chubot.predict_intent(data)
        (prob, intent) = responses[0]
        intent_list_result.append(encode_intent(intent))
    print("accuracy",accuracy_score(intent_list_test,intent_list_result))
Exemplo n.º 9
0
class ChuBotAction():
    def __init__(self, botname, language='vi'):
        self.botname = botname
        self.chubot = ChuBotBrain(botname, language)
        self.followup_actions = None
        self.action_templates = None
        self.action_custom = None
        self.slots = None
        self.start_flag = True
        self.active_entities = {}
        # TODO perhaps if folder exists -> load metadata file

    def load_domain(self, domain_file):
        """Load domain info to bot
        """
        # TODO handle exception here
        # setter -hmm, is this okay? should use @property or not?
        with io.open(domain_file) as f:
            action_domain = json.loads(
                open(domain_file, encoding="utf-8").read())

        # data fields
        self.followup_actions = action_domain["action_domain_data"][
            "followup_actions"]
        self.action_templates = action_domain["action_domain_data"][
            "action_templates"]
        self.action_custom = action_domain["action_domain_data"][
            "action_custom"]
        self.slots = action_domain["action_domain_data"]["slots"]

    def handle_action(self, action, **kwargs):
        """Handle a response action
            Select a random template for the action
            or fill the slot
            return a list of responses based on action
        """
        import random
        import re
        # TODO Handle KeyError dictionary
        response = ""
        if action in self.action_templates:
            # if action is a template
            templates = self.action_templates.get(action)
            template = random.choice(templates)

            # TODO handle multiple detected entity but template only one?
            # ->should in custom action
            # TODO handle multiple slots
            regex_pattern = r"{{(.*)}}"
            template_entity = re.search(regex_pattern, template, re.M | re.I)
            if not template_entity:
                # no found
                # return template as response
                return [template]
            else:
                active_entities = kwargs['active_entities']
                # TODO multiple slot filling
                entity = template_entity.group(1)
                for ent in active_entities:
                    if ent['entity'] == entity:
                        value = ent['value']
                return [template.replace(template_entity.group(), value)]

        elif action in self.action_custom:
            import custom_actions
            func = getattr(custom_actions,
                           self.action_custom[action].get("func_name"))
            response = func(
                action_args=self.action_custom[action].get("kwargs"), **kwargs)

        return response

    def begin_conversation(self, **kwargs):
        """Say welcome
        """
        # TODO should fix name of this action or not?
        start_flag = False
        return self.handle_action("bot_welcome")

    def handle_message(self,
                       inmessage,
                       debug=False,
                       intent_prob_threshold=0.3,
                       **kwargs):
        """Simple rule_based response
        """
        import itertools

        # TODO handle first welcome, repetitive input, etc -> state of conversation
        entities_pred = self.chubot.predict_entity(inmessage)
        intents_pred = self.chubot.predict_intent(inmessage)
        if debug:
            print("Predicted entity: ", entities_pred)
            print("Predicted intent: ", intents_pred)

        # select the first ranked predicted entity/intents
        # handle entities with different values but same entity type

        active_entities = {"active_entities": entities_pred}
        # select highest probable intent
        (prob, intent) = intents_pred[0]

        if prob < intent_prob_threshold:
            bot_actions = ["default_fallback"]
        else:
            # TODO handle key error?
            bot_actions = self.followup_actions.get(intent)

        if debug:
            print(bot_actions)

        bot_responses = [
            self.handle_action(action, **active_entities)
            for action in bot_actions
        ]
        bot_responses = list(itertools.chain(*bot_responses))

        return bot_responses

    def run_commandline_bot(self, **kwargs):
        """A commandline bot
        """
        # TODO handle action_domain_file doesnt match entities and intent in nludata file

        print("Type stop to end.\n -------------------")
        begin = self.begin_conversation()
        print("Chubot:", begin)
        while True:
            sys.stdout.write("You   > ")
            inmessage = input()
            if inmessage == 'stop':
                break
            responses = self.handle_message(inmessage, debug=False)
            for response in responses:
                print("Chubot:", response)

    def custom_handle_message(self,
                              inmessage,
                              debug=False,
                              intent_prob_threshold=0.3,
                              **kwargs):
        """Simple rule_based response
        """
        import itertools

        # TODO handle first welcome, repetitive input, etc -> state of conversation
        entities_pred = self.chubot.predict_entity(inmessage)
        intents_pred = self.chubot.predict_intent(inmessage)
        if debug:
            print("Predicted entity: ", entities_pred)
            print("Predicted intent: ", intents_pred)

        # select the first ranked predicted entity/intents
        # handle entities with different values but same entity type

        active_entities = {"active_entities": entities_pred}
        # select highest probable intent
        (prob, intent) = intents_pred[0]

        if prob < intent_prob_threshold:
            bot_actions = ["default_fallback"]
        else:
            # TODO handle key error?
            bot_actions = self.followup_actions.get(intent)

        if debug:
            print(bot_actions)

        bot_responses = [
            self.handle_action(action, **active_entities)
            for action in bot_actions
        ]
        bot_responses = list(itertools.chain(*bot_responses))

        return bot_responses
Exemplo n.º 10
0
def test_entity_train(name):
    chubot = ChuBotBrain(name, language='vi')
    chubot.load_data("usingdata/full_train.json")
    # chubot.load_data("usingdata/vi_nlu_ask_way.json")
    meta = chubot.train_nercrf()
Exemplo n.º 11
0
def create_model(name):
    chubot = ChuBotBrain(name, language='vi')
    chubot.load_data("usingdata/full_train.json")
    # chubot.load_data("usingdata/vi_nlu_ask_way.json")
    meta = chubot.train()