将Eliza chatbot回复传递给kivy

时间:2017-05-30 06:31:46

标签: python regex kivy

我正在尝试与Kivy一起实施Eliza chatbot。 python代码工作正常但是当我尝试在Kivy中实现相同的时候,输出响应仍然被添加到python终端而不是kivy。

我是kivy的新手

这是我用过的代码:

import kivy
#kivy.require('1.5.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class kivyentrywidget(GridLayout):

    def __init__(self, **kwargs):
        super(kivyentrywidget, self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label(text='What do you want to print?'))
        self.text_input = TextInput(multiline=False)
        self.add_widget(self.text_input)
        self.printbutton = Button(text='Print')
        #self.printbutton.bind(on_press=callback)
        self.printbutton.bind(on_press=self.main)
        self.add_widget(self.printbutton)

    def callback(self,evt=None): 
        return self.add_widget(Label(text=self.text_input.text))
        #return self.add_widget(Label(text=self.analyze()))

    def reflect(fragment):
        tokens = fragment.lower().split()
        for i, token in enumerate(tokens):
            if token in reflections:
                tokens[i] = reflections[token]
        return ' '.join(tokens)


    def analyze(self,statement):
        for pattern, responses in pairs:
            match = re.match(pattern, statement.rstrip(".!"))
            if match:
                response = random.choice(responses)
                #return response.format(*[reflect(g) for g in match.groups()])
                return self.add_widget(Label(text= response.format(*[reflect(g) for g in match.groups()])))
    def main(self,evt=None):
        #print ("Hello. How are you feeling today?")
        self.add_widget(Label(text=self.text_input.text))
        while True:
            statement = raw_input("> ")
            self.analyze(statement)
            #print (analyze(statement))
            #return self.add_widget(Label(text=self.analyze(statement))) 
            if statement == "quit":
                break

class Firstapp(App):
    def build(self):
        return kivyentrywidget()

if __name__ == '__main__':
    Firstapp().run()

0 个答案:

没有答案