Closed Royhowtohack closed 4 years ago
现仔细看看 https://github.com/neolee/wop-community 的 README,学会怎么贴代码。你这一大串代码真没法看啊。
import time
import random
from opencc import OpenCC
from time import sleep
from termcolor import colored
from simpleeval import simple_eval
class Bot:
def __init__(self, runtype = 'once'):
self.runtype = runtype
def _q(self):
return ''
def _run_once(self):
self._say(self._q())
a = input()
self._say(self._think(a))
def _run_loop(self):
self._say(self._q())
while True:
a = input()
if a.lower() in ['q', 'x', 'quit', 'exit']:
break
self._say(self._think(a))
def _think(self, s):
return s
def _format(self, s):
return colored(s, 'cyan')
def run(self):
if self.runtype == 'once':
self._run_once()
elif self.runtype == 'loop':
self._run_loop()
wait = 1
sleep(Bot.wait)
print(self._format(self.q))
self.a = input()
sleep(Bot.wait)
print(self._format(self._think(self.a)))
class HelloBot(Bot):
def _q(self):
return "Hi, what is your name?"
def _think(self, s):
return f"Hello {s}"
class GreetingBot(Bot):
def _q(self):
return "How are you today?"
def _think(self, s):
if 'good' in s.lower() or 'fine' in s.lower():
return "I'm feeling good too"
else:
return "Sorry to hear that"
class FavoriteColorBot(Bot):
def _q(self):
return "What's your favorite color?"
def _think(self, s):
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']
return f"You like {s.lower()}? My favorite color is {random.choice(colors)}"
class TranslateBot(Bot):
def _q(self):
return "我能翻译啦,简体汉字到繁体哦,你想试试吗?😘😘😘"
def _think(self, s):
cc = OpenCC('s2t')
to_convert = s
converted = cc.convert(to_convert)
result = converted
return f"{result} "
class CalcBot(Bot):
def _q(self):
s = "Through recent upgrade I can do calculation now. Input some arithmetic expression to try:"
if self.runtype == 'loop':
s += " (type x, q, exit or quit to quit)"
return s
def _think(self, s):
try:
result = f"Done.Result = {simple_eval(s)}"
except:
result = "Sorry I can't understand"
return result
class Garfield:
def __init__(self, wait=1):
Bot.wait = wait
self.bots = []
def add(self, bot_class, runtype = 'once'):
bot = bot_class(runtype)
self.bots.append(bot)
def _prompt(self, s):
print(s)
print()
def run(self):
self._prompt("This is Garfield dialog system. Let's talk.")
for bot in self.bots:
bot.run()
if __name__ == '__main__':
garfield = Garfield(1)
# garfield.add(HelloBot)
# garfield.add(GreetingBot)
# garfield.add(FavoriteColorBot)
garfield.add(CalcBot, 'loop')
garfield.add(TranslateBot, 'loop')
garfield.run()
_q()
是在 Bot
中定义的方法,Bot
类所有子类,也就是所有小机器人们都要实现这个方法,实现时返回一个字符串就行了,这个字符串会作为该机器人说的第一句话——正如 Bot
类的 run()
方法里实现的那样。
在你的 run()
方法中,它先根据 self.runtype
选择执行 _run_once()
或 _run_loop()
两个之一,而这两函数的实现都没问题;但你的问题在于,为啥后面还有一堆老代码没清除掉呢?这些老代码里没使用 _q()
方法,还在使用已经移除的 self.q
属性,那就自然要出问题了。
我试了把run里面后面的代码都注释,也报错了。
我没有找到self._say()这个函数在哪里定义的,返回去看了视频,还是没搞懂self._say()的作用。_q()的作用搞懂了。
哪里来的?上课讲过的啊,现场演示了的一个方法。
修改完以后报错了。看了两遍视频,没看懂 self.say()这个函数的意思。报错跟这个也有关。我把整个代码贴在下面。请李老师指教。(。ì í。)
import time import random from opencc import OpenCC from time import sleep from termcolor import colored from simpleeval import simple_eval
class Bot:
class HelloBot(Bot): def _q(self): return "Hi, what is your name?"
class GreetingBot(Bot): def _q(self): return "How are you today?"
class FavoriteColorBot(Bot): def _q(self): return "What's your favorite color?"
class TranslateBot(Bot):
class CalcBot(Bot): def _q(self): s = "Through recent upgrade I can do calculation now. Input some arithmetic expression to try:" if self.runtype == 'loop': s += " (type x, q, exit or quit to quit)" return s
class Garfield:
if name == 'main': garfield = Garfield(1)