Example #1
0
    def question_answer_hub(cls, question_str):
        """
        问答主总控,基于aiml构建问题匹配器
        :param question_str:问句输入
        :return:
        """
        if cls.user == None:
            cls.user = User()
            cls.user.set_age(20)
            cls.user.set_sex('女')

        question_str = cls.nlp_util.clear_question(question_str)
        question_str = cls.clear_util.filter(question_str)
        if '*' in question_str:
            return ['很抱歉,请您注意文明用语']
        aiml_response = ''
        aiml_response_normal = ''
        aiml_response_specify = ''

        question_first,question_replaced_normal,question_replaced_spcify,entity_dict = cls.nlp_util.repalce_question(question_str)

        #print(question_first,question_replaced_normal,question_replaced_spcify)

        #print(question_first)
        aiml_response = cls.aiml_util.response(question_first,cls.intent.get_intent())
        #print(question_first,question_replaced_normal,question_replaced_spcify,entity_dict,aiml_response)

        if 'task_' in aiml_response:
            cls.intent.set_intent(aiml_response)

            graph_response = Bot.task_response(aiml_response, entity_dict,question_str,cls.user.age,cls.user.sex)

        elif aiml_response != '':
            cls.intent.set_intent(aiml_response)
            graph_response = [aiml_response]
        else:
            aiml_response_normal = cls.aiml_util.response(question_replaced_normal,cls.intent.get_intent())
            if 'task_' in aiml_response_normal:
                cls.intent.set_intent(aiml_response_normal)
                graph_response = Bot.task_response(aiml_response_normal, entity_dict,question_str,cls.user.age,cls.user.sex)
            elif aiml_response_normal != '':
                cls.intent.set_intent(aiml_response_normal)
                graph_response = [aiml_response_normal]
            else:
                aiml_response_specify = cls.aiml_util.response(question_replaced_spcify,cls.intent.get_intent())
                if 'task_' in aiml_response_specify:
                    cls.intent.set_intent(aiml_response_specify)
                    graph_response = Bot.task_response(aiml_response_specify, entity_dict,question_str,cls.user.age,cls.user.sex)
                elif aiml_response_specify != '':
                    cls.intent.set_intent(aiml_response_specify)
                    graph_response = [aiml_response_specify]
                else:
                    #print(question_str)
                    response = dict(cls.search_bot.answer_question(question_str)[0])
                    #print(response)

                    if float(response['score']) > 0.7:
                        cls.intent.set_intent(response['answer'])
                        graph_response = [response['answer']]

                    else:

                        words, pattern, arcs_dict, postags, hed_index = NLPUtil.get_sentence_pattern(question_str)
                        #print(words, pattern, arcs_dict, postags, hed_index)
                        aiml_reponse = AIMLUtil.pedia_response(pattern)

                        answer = TaskManager.task_response(aiml_reponse, words, arcs_dict, postags, hed_index)
                        if answer != None:
                            cls.intent.reset_intent('pedia')
                            return [answer]
                        else:
                            cls.intent.reset_intent('chat')
                            #print("cls.chat.get_response(question_str)",cls.chat.get_response(question_str))
                            graph_response = [cls.chat.get_response(question_str)]

        dialog_dict = {'intent':cls.intent.get_intent(),'question':question_str,'answer':graph_response[0]}
        #print(aiml_response,aiml_response_normal,aiml_response_specify)

        return graph_response
Example #2
0
    def question_answer_hub_master(self, question_str):
        """
        问答主总控,基于aiml构建问题匹配器
        :param question_str:问句输入
        :return:
        """

        question_str = self.nlp_util.clear_question(question_str)
        aiml_response = ''
        aiml_response_normal = ''
        aiml_response_specify = ''

        question_first, question_replaced_normal, question_replaced_spcify, entity_dict = self.nlp_util.repalce_question(
            question_str)
        aiml_response = self.aiml_util.response(question_first, self.type)
        print(question_first, question_replaced_normal,
              question_replaced_spcify, entity_dict, aiml_response)

        if 'task_' in aiml_response:

            graph_response = Bot.task_response(aiml_response, entity_dict,
                                               question_str, self.age,
                                               self.sex)
            #print("graph_response",graph_response)

        elif aiml_response != '':
            graph_response = [aiml_response]
        else:
            aiml_response_normal = self.aiml_util.response(
                question_replaced_normal, self.type)

            #print("aiml_response_normal",aiml_response_normal, question_replaced_normal)

            if 'task_' in aiml_response_normal:
                graph_response = Bot.task_response(aiml_response_normal,
                                                   entity_dict, question_str,
                                                   self.age, self.sex)
            elif aiml_response_normal != '':
                graph_response = [aiml_response_normal]
            else:
                aiml_response_specify = self.aiml_util.response(
                    question_replaced_spcify, self.type)
                if 'task_' in aiml_response_specify:
                    graph_response = Bot.task_response(aiml_response_specify,
                                                       entity_dict,
                                                       question_str, self.age,
                                                       self.sex)
                elif aiml_response_specify != '':
                    graph_response = [aiml_response_specify]
                else:
                    response = dict(
                        self.search_bot.answer_question(question_str)[0])
                    #print(response['answer'], response['score'], question_str, "===")
                    #print(response['score'], response['question'])
                    if float(response['score']) > 0.7:

                        graph_response = [response['answer']]

                    else:
                        words, pattern, arcs_dict, postags, hed_index = NLPUtil.get_sentence_pattern(
                            question_str)
                        print(words, pattern, arcs_dict, postags, hed_index)
                        aiml_reponse = AIMLUtil.response(pattern, self.type)
                        print(aiml_reponse)
                        answer = TaskManager.task_response(
                            aiml_reponse, words, arcs_dict, postags, hed_index)
                        if answer != None:
                            return [answer]
                        else:
                            graph_response = [
                                self.chat.get_response(question_str)
                            ]
                            """
                            import requests, json
                            github_url = "http://openapi.tuling123.com/openapi/api/v2"
                            data = json.dumps({
                                "reqType": 0,
                                "perception": {
                                    "inputText": {
                                        "text": question_str
                                    },
                                },
                                "userInfo": {
                                    "apiKey": "62f1c8991a6d499c8b22e497de6cdd11",
                                    "userId": "219315"
                                }
                            })

                            r = requests.post(github_url, data)
                            print(r.json())
                            res_msg = r.json()['results'][0]['values']['text']
                            return [res_msg]
                            """
        '''
        由于服务类同时具有共性与特性,所以生产两个模版,即一份模版将服务实体替换为service进行模版匹配,一类模版
        不讲服务实体替换为service直接用原词汇匹配模版
        '''
        """
        question_first, question_replaced, question_replaced2, entity_dict = self.nlp_util.repalce_question(
            question_str)
        aiml_response = self.aiml_util.response(question_replaced)
        print(aiml_response, question_replaced)
        if 'task_' in aiml_response:

            graph_response = Bot.task_response(aiml_response, entity_dict)

        elif aiml_response!='':
            graph_response=[aiml_response]
        else:
            aiml_response2 = self.aiml_util.response(question_replaced2)
            print(aiml_response2,question_replaced2)

            if 'task_' in aiml_response2:
                graph_response = Bot.task_response(aiml_response2, entity_dict)
            elif aiml_response2 != '':
                graph_response = [aiml_response2]
            else:
                response = dict(self.search_bot.answer_question(question_str)[0])
                print(response['answer'],response['score'],question_str,"===")

                if float(response['score'])>0.7:
                    print(response['score'])
                    graph_response = [response['answer']]
                else:
                    graph_response = ['很抱歉,我好像不明白,请您换一种说法']
        """
        #print("===============haiyouma")

        #self.set_type_by_response([aiml_response,aiml_response_normal,aiml_response_specify])
        #print("===lll",self.type,self)
        return graph_response