字典中的函数

2024-06-24 13:53:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为K-pop粉丝制作基于规则库的聊天机器人。你知道吗

场景聊天机器人必须使用大量的条件语句。你知道吗

为了最小化这个问题,我使用了一个有方法的字典,但是这个方法一直在执行,尽管我只是定义了字典没有运行。你知道吗

帮帮我,我不知道为什么会这样。我使用的是python2.7

我的密码是

#_*_coding:utf-8_*_
# 한글입력을 가능하게 해준다.
class chatbot_builder():
    def __init__(self,bot= 'Rang_i: ',client=raw_input('Enter your name: ')):
        self.bot=bot
        self.client=client

    def search(self,mem):
        while True:
            print self.bot + ' Which member do you want to know?'
            res=raw_input(self.client+': ')
            #입력받은 맴버 찾자!
            for id in mem:
                if id in res.upper():
                    print self.bot + '%s\'s birth is %s'%(mem[id][0],mem[id][1])
            #대화종료
            if 'another group' in res.lower():
                break

    def greeting(self):
        print self.bot + 'Hello %s!' % self.client
        raw_input(self.client + ': ')
        print self.bot + 'How are you?'
        raw_input(self.client + ': ')
        print self.bot + 'I like listening K-pop and searching info of Korean pop idol ! '
        raw_input(self.client + ': ')

    def idol_Wanna_one(self):
        print self.bot + 'I love into Wanna one! How about you %s?' % self.client
        res=raw_input(self.client + ': ')
        print self.bot + 'I know all memebers\'s birth date !'
        res=raw_input(self.client + ': ')
        mem={'daniel':['KANG DANIEL', '12.10.96'],'hoon':['PARK JI HOON','5.29.99'],'hwi':['LEE DAE HWI','1.29.01'],'hwan':['KIM JAE HWA'],'wu':['ONG SEONG WU','8.25.95'],
             'jin':['PARK WOO JIN', '11.2.99'],'lin':['LAI KUAN LIN','9.23.01'],'sung':['YOON JI SUNG','3.8.91'],'hyun':['HWANG MIN HYUN','8.9.95'],
             'young':['BAE JIN YOUNG','5.10.00'],'woon':['HA SUNG WOON','3.22.94']}

        chatbot_builder.search(self,mem)

    def idol_EXO(self):
        print self.bot + 'I love into EXO! EXO debuted in 4.8.2012~ How about you %s?' % self.client
        res=raw_input(self.client + ': ')
        print self.bot + 'I know all memeber\'s birth date !'
        res=raw_input(self.client + ': ')
        mem={'SUHO':['SUHO','5.22.91'],'YEOL':['CHAN  YEOL','11.27.92'],'KAI':['KAI','1.14.94'],
             'DO':['D.O.','1.12.93'],'HYUN':['BAEK HYUN'],'HUN':['SE HUN','4.12.94'],'XI':['XIUMIN','3.26.90'],
             'CHEN':['CHEN','9.21.92'],'LAY':['LAY','10.7.91'],'TAO':['TAO','5.2.93'],'LUHAN':['LUHAN','4.20.90'],
             'KRIS':['KRIS','11.6.90']}
        chatbot_builder.search(self,mem)

    def idol_REDVELVET(self):
        print self.bot + 'I love into RED VELVET! RED VELVET debuted in 8.1.2014~ How about you %s?' % self.client
        res=raw_input(self.client + ': ')
        print self.bot + 'I know all memeber\'s birth date !'
        res=raw_input(self.client + ': ')
        mem={'WEN':['WENDY','2,21,94'],'SEU':['SEULGI','2.10.94'],'YERI':['YERI','3.5.99'],
             'IRENE':['IRENE','3.29.91']}
        chatbot_builder.search(self,mem)

    def idol_TWICE(self):
        print self.bot + 'I love into TWICE! TWICE debuted in 10.20.2015~ How about you %s?' % self.client
        res=raw_input(self.client + ': ')
        print self.bot + 'I know all memebers\'s birth date !'
        res=raw_input(self.client + ': ')
        mem={'MOMO':['Momo','11.9.96'],'SANA':['Sana','12.29.96'],'DAHYUN':['Dahyun','5.28.98'],
             'CHAEYOUNG':['Chaeyoung','4.23.99'],'MINA':['Mina','3.24.97'],'TZUYU':['Tzuyu','6.14.99'],'NAYEON':['Nayeon','9.22.95'],
             'JIHYO':['Jihyo','2.1.97']}

        chatbot_builder.search(self, mem)

    def idol_conversation(self):
        chatbot_builder.greeting(self)
        idol_dict = {'EXO': chatbot_builder.idol_EXO(self), 'WANNAONE': chatbot_builder.idol_Wanna_one(self),
                     'TWICE': chatbot_builder.idol_TWICE(self), 'VELVET': chatbot_builder.idol_REDVELVET(self)}
        while True:
            print self.bot + 'What is your favorite group?'
            res = raw_input(self.client+ ': ')
            for idol in idol_dict:
                if idol in res.upper():
                    idol_dict[idol]
            if 'bye' in res:
                print self.bot + 'Bye %s ! See you later'%(self.client)
                break

# 인스턴스 생성
first_bot=chatbot_builder()
# idol_conversation 매서드 싫행 -> 챗봇과 대화 시작
first_bot.idol_conversation()

问题出在def-idol\u会话的idol\u dict={'EXO':chatbot中_建筑商.idol\u EXO(自我)~。这个措辞是可执行的。你知道吗


Tags: inselfclientyouinputrawdefbot
1条回答
网友
1楼 · 发布于 2024-06-24 13:53:23

在代码之外,您可能希望在idol_dict中将文本输入映射到类方法:

idol_dict = {
    'EXO': self.idol_EXO,
    'WANNAONE': self.idol_Wanna_one,
    'TWICE': self.idol_TWICE,
    'VELVET': self.idol_REDVELVET,
}

然后像这样调用匹配方法:

idol_dict[idol]()

相关问题 更多 >