Example #1
0
    def question_answer_hub(self, question_str):
        """
        问答总控,基于aiml构建问题匹配器
        :param question_str:问句输入
        :return:
        """
        g = rdfPrepare.load_graph()
        question_replaced, entity_dict = entityMatch2.match_and_replace_all(question_str,g)
        print(entity_dict)

        #navi_g = rdfPrepare.load_navi_graph()

        #for room in g.subjects(RDF.type, rdflib.URIRef("http://www.libot.org/room")):
            #print(room)
        #for s, p, o in g:
        #    print((s, p, o))
        #print(g.serialize(format='n3'))
        #navi_question_replaced, navi_entity_dict = entityMatch2.match_and_replace_all(question_str, navi_g)
        '''
        if multiwheelUnit.get_value('business') == "办理读书卡":
            if "answer" not in multiwheelUnit.get_value('step'):
                question_replaced += "读卡"
            else:
                multiwheelUnit.set_value('business',None)
                multiwheelUnit.set_value('step', None)
        '''
        #aiml_respons = self._aiml_kernal.respond(question_replaced)
        aiml_respons = self._aiml_kernal.respond(question_replaced)
        print(aiml_respons)
        '''
        if 'multiwheeltask_'in aiml_respons:
            print("aiml_respons: ", str(aiml_respons))
            graph_respons = rdfBotMul.task_response(aiml_respons, entity_dict, question_str, g)
            return graph_respons
        '''
        if 'task_' in aiml_respons:
            #if aiml_respons == 'task_room_pos':

            #    return
                #graph_respons = rdfBot.task_response(aiml_respons, navi_entity_dict, question_str, navi_g)
                #graph_respons = rdfBot.task_response(aiml_respons, test_entity_dict, question_str, test_g)
            #else:
            graph_respons = rdfBot.task_response(aiml_respons,entity_dict,question_str,g)

            return graph_respons
        else:
            return aiml_respons
Example #2
0
    def question_answer_hub(self, question_str):
        """
        问答总控,基于aiml构建问题匹配器
        :param question_str:问句输入
        :return:
        """
        g = rdfPrepare.load_graph()
        question_replaced, entity_dict = entityMatch2.match_and_replace_all(
            question_str, g)
        # question_replaced, entity_dict = entityMatch.match_and_replace_all(question_str)

        arr = []
        if len(entity_dict['room']) > 0:
            for i in entity_dict['room']:
                if len(i) == 0:
                    continue
                index = question_str.find(i[0])
                arr.append(index)
            # print(arr)
            arr_index = np.argsort(np.array(arr))
            # print(arr_index)
            entity_dict2 = []
            for i in entity_dict['room']:
                if len(i) == 0:
                    continue
                entity_dict2.append(i)

            for i in range(len(entity_dict['room'])):
                if len(entity_dict['room'][i]) == 0:
                    continue
                # print(arr_index[i],entity_dict2[arr_index[i]])
                entity_dict['room'][i] = entity_dict2[arr_index[i]]

        aiml_respons = self._aiml_kernal.respond(question_replaced)
        if 'task_' in aiml_respons:
            print("aiml_respons: ", str(aiml_respons))
            print("entity_dict: ", str(entity_dict))
            graph_respons = rdfBot.task_response(aiml_respons, entity_dict, g)
            return graph_respons
        else:
            return aiml_respons
Example #3
0
                    question_str = question_str.replace(
                        varname, replace_entity_mark['floor'])
        return question_str, entities

    @classmethod
    def match_and_replace_all(cls, question_str, graph):
        """
        对问句进行所有类型实体匹配与替换
        :param question_str:
        :return:
        """
        entity_dict = {}  #分类存放匹配到的各类实体
        var_list = DictMatch2.var_dict_list(graph)
        question_str, entity_list = cls.room_dict_match(
            question_str, var_list, graph)
        entity_dict['room'] = entity_list
        question_str, entity_list = cls.resource_dict_match(
            question_str, var_list, graph)
        entity_dict['res'] = entity_list
        question_str, entity_list = cls.floor_dict_match(
            question_str, var_list, graph)
        entity_dict['floor'] = entity_list
        return question_str, entity_dict


if __name__ == "__main__":
    g = rdfPrepare.load_graph()
    question_str, entity_list = entityMatch2.match_and_replace_all(
        "日本出版物文库阅览室在总馆南区四层吗", g)
    print(question_str)
    print(entity_list)
Example #4
0
    def question_answer_hub(self, question_str):
        """
        问答总控,基于aiml构建问题匹配器
        :param question_str:问句输入
        :return:
        """
        g = rdfPrepare.load_graph()
        question_replaced, entity_dict = entityMatch2.match_and_replace_all(
            question_str, g)
        #print(question_replaced,entity_dict)

        navi_g = rdfPrepare.load_navi_graph()
        navi_question_replaced, navi_entity_dict = entityMatch2.match_and_replace_all(
            question_str, navi_g)
        #print(navi_question_replaced, navi_entity_dict)
        # question_replaced, entity_dict = entityMatch.match_and_replace_all(question_str)
        '''
        arr = []
        if len(entity_dict['room']) > 0:
            for i in entity_dict['room']:
                if len(i) == 0:
                    continue
                index = question_str.find(i[0])
                arr.append(index)
            # print(arr)
            arr_index = np.argsort(np.array(arr))
            # print(arr_index)
            entity_dict2 = []
            for i in entity_dict['room']:
                if len(i) == 0:
                    continue
                entity_dict2.append(i)

            for i in range(len(entity_dict['room'])):
                if len(entity_dict['room'][i]) == 0:
                    continue
                # print(arr_index[i],entity_dict2[arr_index[i]])
                entity_dict['room'][i] = entity_dict2[arr_index[i]]
        '''
        if multiwheelUnit.get_value('business') == "办理读书卡":
            if "answer" not in multiwheelUnit.get_value('step'):
                question_replaced += "读卡"
            else:
                multiwheelUnit.set_value('business', None)
                multiwheelUnit.set_value('step', None)

        aiml_respons = self._aiml_kernal.respond(question_replaced)

        if 'multiwheeltask_' in aiml_respons:
            print("aiml_respons: ", str(aiml_respons))
            # print("entity_dict: ", str(entity_dict))
            graph_respons = rdfBotMul.task_response(aiml_respons, entity_dict,
                                                    question_str, g)
            return graph_respons
        elif 'task_' in aiml_respons:
            print("aiml_respons: ", str(aiml_respons))
            #print("entity_dict: ", str(entity_dict))
            if aiml_respons == 'task_room_pos':
                graph_respons = rdfBot.task_response(aiml_respons,
                                                     navi_entity_dict,
                                                     question_str, navi_g)
            else:
                graph_respons = rdfBot.task_response(aiml_respons, entity_dict,
                                                     question_str, g)

            return graph_respons
        else:
            return aiml_respons