Esempio n. 1
0
class dialogServer(object):
    def __init__(self):
        self.wordSegmentor = wordSegment()  # 生成 分词器
        self.sentFilter = sentenceFilter()  # 生成 过滤器
        self.NLUModel = NLU()  # 生成 对话理解器
        self.dialogueManager = Manage()  # 生成 对话管理器
        self.Interactor = InteractWithKB()  # 生成 知识交互器
        self.answerGenerator = AnswerGenerator()  # 生成 回答产生器

        self.isFirst = True  # 是一轮对话的开始
        self.isShow = "0"  # 0表示不显示图,1表示显示图

    def setIsShow(self, intent):
        if intent == "query_location":
            self.isShow = "1"
        else:
            self.isShow = "0"

    def dialogComplete(self):
        pass
        # 这里是 对话完成后,将一些状态置0

    def processSentecne(self, input_x):
        sentCuted = self.wordSegmentor.cut_sentence(input_x)  # 分词

        self.sentFilter.filtSent(sentCuted)  # 过滤器处理句子

        if self.wordSegmentor.hasSentence == True:  # 保证有内容
            # 过滤器先放上吧
            # if self.sentFilter.state == True:
            if True == True:
                # 说明是任务相关的句子

                sentence, intent, slot = self.NLUModel.nluSentence(
                    sentCuted)  # 理解句子

                if self.isFirst == True:  # 表明是新一轮开始
                    self.strCode = self.dialogueManager.firstmanage(
                        intent, slot, sentence)

                if self.dialogueManager.state == 2:  # 只有是完成状态才会产生答案
                    ans = self.Interactor.information_to_answer(
                        sentence, self.dialogueManager.intentHold,
                        self.dialogueManager.slotStrHold,
                        self.dialogueManager.slotDictHold)

                    input_x = self.dialogueManager.newSentence()

                    answerSentence = self.answerGenerator.generateAnswer(
                        input_x, ans, self.dialogueManager.intentHold,
                        self.dialogueManager.slotStrHold)

                    # 这里应该有归零操作,将一些状态置为初始状态,可是是哪些状态置0呢???值得思考
                    self.isFirst = True
                    self.setIsShow(self.dialogueManager.intentHold)

                    return answerSentence

                elif self.dialogueManager.state == 1:  # 开启多轮对话状态
                    querySentence = self.dialogueManager.querySentence(
                        intent, self.strCode)  # 产生问句
                    self.isFirst = False
                    self.isShow = "0"
                    return querySentence

                elif self.dialogueManager.state == 3:  # 处于多轮对话中
                    print("3333slot", slot)
                    print("33333sentence", sentence)
                    self.dialogueManager.informationFuse(
                        self.dialogueManager.intentHold, slot,
                        sentence)  # 将新旧信息融合
                    # strCode = dialogueManager.calState(intent,dialogueManager.slotStrHold)
                    self.dialogueManager.str2State(
                        self.dialogueManager.intentHold,
                        self.dialogueManager.slotStrHold)  # 改变对话管理器状态
                    self.dialogueManager.buildSlotDict(
                        self.dialogueManager.intentHold, slot, sentence)

                    if self.dialogueManager.state != 2:
                        print("不等于2222 ", self.dialogueManager.state)

                        querySentence = self.dialogueManager.querySentence(
                            self.dialogueManager.intentHold,
                            self.dialogueManager.slotStrHold)  # 产生问句
                        self.isFirst = False
                        self.isShow = "0"
                        return querySentence

                    else:  # 完成状态
                        print("这里!!!!!")

                        ans = self.Interactor.information_to_answer(
                            sentence, self.dialogueManager.intentHold,
                            self.dialogueManager.slotStrHold,
                            self.dialogueManager.slotDictHold)

                        input_x = self.dialogueManager.newSentence()
                        print("完成状态", self.dialogueManager.slotStrHold)
                        answerSentence = self.answerGenerator.generateAnswer(
                            input_x, ans, self.dialogueManager.intentHold,
                            self.dialogueManager.slotStrHold)

                        print("self.dialogueManager.answerState",
                              self.dialogueManager.answerState)
                        print("完成状态", answerSentence)

                        self.isFirst = True
                        self.setIsShow(self.dialogueManager.intentHold)

                        return answerSentence

            else:
                return "您说的句子我暂时无法处理,请点击结束对话,将问题汇报给管理员"

        else:
            return "请输入内容"
Esempio n. 2
0
def main():
    
    wordSegmentor = wordSegment() # 生成 分词器
    sentFilter = sentenceFilter()  # 生成 过滤器
    NLUModel = NLU()        # 生成 对话理解器
    dialogueManager = Manage()  # 生成 对话管理器
    Interactor = InteractWithKB()  # 生成 知识交互器
    answerGenerator = AnswerGenerator()  # 生成 回答产生器

    # input_x = "刘通是谁的学生"
    # input_x = "介绍一下邓华老师"
    # input_x = "邓华老师的研究方向是什么"
    input_x = "介绍一下机电工程学院"
    i = 0
    # data = loadFile()
    
    print()
    print("输入的句子是",input_x)

    sentCuted = wordSegmentor.cut_sentence(input_x)
    # sentFilter.filtSent(sentCuted)  # 先不加过滤器
    # try:

    if wordSegmentor.hasSentence == True:  # 保证有内容
        if True == True:  # 保证是任务相关的句子
            i+=1
            print("这是第",i,"句话")
        # input_x = "王老师的办公室在哪"
        # input_x = "王恒升老师的办公室在哪"

        # intent = "query_location"
        # slot = ["first_name","teacher","o","room","o","o"]
        # slot = ["name","teacher","o","room","o","o"]

        # sentence = ["王","老师","的","办公室","在","哪里"]
        # sentence = ["王恒升","老师","的","办公室","在","哪里"]

            # sentFilter.filtSent(sentCuted)
            # print("sentCuted",sentCuted)
            sentence,intent,slot = NLUModel.nluSentence(sentCuted)
            print("intent = ",intent)

            strCode = dialogueManager.firstmanage(intent,slot,sentence)
            # print("strCode",strCode)
            for _ in range(5):
                if dialogueManager.state == 2:  # 只有是完成状态才会产生答案
                    print("-----单轮对话----")
                    # print("strCode",strCode)
                    print("sentence",sentence)
                    print("dialogueManager.intentHold",dialogueManager.intentHold)
                    print("dialogueManager.slotStrHold",dialogueManager.slotStrHold)
                    print("dialogueManager.slotDictHold",dialogueManager.slotDictHold)

                    ans = Interactor.information_to_answer(sentence,
                                                            dialogueManager.intentHold,
                                                            dialogueManager.slotStrHold,
                                                            dialogueManager.slotDictHold)
                    # print("myManager.state",dialogueManager.state)
                    # print("Interactor.interactState",Interactor.interactState)
                    print("ans",ans)
                    input_x = dialogueManager.newSentence()
                    print("newSentence",input_x)
                    answerSentence = answerGenerator.generateAnswer(input_x,ans,intent,dialogueManager.answerState)
                    print("answerSentence",answerSentence)
                    print("----- 单轮结束-------")
                    print()

                elif dialogueManager.state == 1: # 开启多轮对话
                    print("----开启多轮对话----")
                    querySentence = dialogueManager.querySentence(intent,strCode)  # 产生问句
                    print("querySentence",querySentence)

                elif dialogueManager.state == 3: # 处于多轮对话中
                    print("-----处于多轮对话中-----")
                    # intent_new = "query_location"
                    # slot_new = ["name"]
                    # sentence_new = ["王恒升"]
                    # input_x = "询问一个老师的办公室"
                    input_x = input("请输入句子:--- ")
                    print("dialogueManager.slotStrHold",dialogueManager.slotStrHold)
                    sentCuted = wordSegmentor.cut_sentence(input_x)
                    sentence_new,intent_new,slot_new = NLUModel.nluSentence(sentCuted)
                    print("intent_new",intent_new)

                    dialogueManager.informationFuse(dialogueManager.intentHold,slot_new,sentence_new)  # 将新旧信息融合
                    # strCode = dialogueManager.calState(intent,dialogueManager.slotStrHold)
                    dialogueManager.str2State(dialogueManager.intentHold,dialogueManager.slotStrHold) # 确定对话管理器状态
                    dialogueManager.buildSlotDict(dialogueManager.intentHold,slot_new,sentence_new)

                    print("ialogueManager.state==3",dialogueManager.state)
                    # 不一定产生问句啊,有可能是完成状态,就不需要产生问句了
                    if dialogueManager.state==3:
                        print("sentence_new333 ",sentence_new)
                        print("dialogueManager.slotStrHold333 ",dialogueManager.slotStrHold)
                        querySentence = dialogueManager.querySentence(dialogueManager.intentHold,dialogueManager.slotStrHold)  # 产生问句
                        print("querySentence333 ",querySentence)

                    print("---------多轮对话中(结束)---------------------------------")
        else:
            print("您的请求不在本系统支持范围内")
                
        
    else:
        print("请输入内容")