Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一次课程大练习程序优化 #1516

Open
pyz610173878 opened this issue Oct 23, 2022 · 1 comment
Open

第一次课程大练习程序优化 #1516

pyz610173878 opened this issue Oct 23, 2022 · 1 comment

Comments

@pyz610173878
Copy link

再正则表达式Eliza 模块中,发现只要输入quit,程序就可以终止。于是想把此功能加入到对话机器人中,但尝试一番无法加入。以下是思考过程。

  1. 先理解了Eliza 模块中是如何做到的。使用循环只要输入不等于quit,就把值传入respnd方法,匹配正则表达式,并随机替换相应的字符串。
def command_interface():
    print('Therapist\n---------')
    print(
        'Talk to the program by typing in plain English, using normal upper-')
    print('and lower-case letters and punctuation.  Enter "quit" when done.')
    print('=' * 72)
    print('Hello.  How are you feeling today?')
    s = ''
    therapist = Eliza()
    while s != 'quit':
        try:
            s = input('> ')
        except EOFError:
            s = 'quit'
        while s[-1] in '!.':
            s = s[:-1]
        print(therapist.respond(s))
        

if __name__ == "__main__":
    command_interface()
  1. 一开始想当然的以为可以直接照搬此方式,但验证了很多次都无法正常运行,然后尝试简化需求,做到停止一个子类Bot。但我想一个能停止,肯定都能停止,但发现两者之间的逻辑貌似不相同。
  2. 进一步在在Garfield类中添加判断条件,来终止程序。尝试一番还是不行。

老师能否给点思路。

@neolee
Copy link
Owner

neolee commented Oct 25, 2022

没明白你的难点在哪里,把你尝试过的(完整)代码发出来看看?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants