Beispiel #1
0
class Bee1(toga.App):
    def startup(self):
        #alternative icon embedding
        #path=os.path.dirname(os.path.abspath(__file__))
        #brutus_icon=os.path.join(path,"icons","brutus.icns")
        #Eliza module
        self.partner=Eliza()

        self.chat=toga.DetailedList(
            data=[
                {
                    'icon':toga.Icon('icons/ubs-logo.png'),

                    'title': 'Bot',
                    'subtitle': 'Hello. How are you feeling today?'
                }
            ]
            ,style=Pack(flex=1))

        self.text_input=toga.TextInput(style=Pack(flex=1))
        
        send_button=toga.Button('Send',style=Pack(padding_left=5),on_press=self.handle_input)

        input_box=toga.Box(
            children=[self.text_input, send_button],
            style=Pack(direction=ROW, alignment=CENTER, padding=5)
        )
#alignment='CENTER',
        # Create a main window with a name matching the app
        self.main_window = toga.MainWindow(title=self.name)

        # Create a main content box
        #main_box = toga.Box()

        # Add the content on the main window
        self.main_window.content = toga.Box(
            children=[self.chat,input_box],
            style=Pack(direction=COLUMN)
        )

        # Show the main window
        self.main_window.show()

    def handle_input(self,widget,**kwargs):
        input_text=self.text_input.value
        self.chat.data.append(
            icon=toga.Icon('icons/user2.png'),
            title='User',
            subtitle=input_text
        )
        self.text_input.value=''
        self.chat.scroll_to_bottom()

        yield random.random()*2        
        response=self.partner.respond(input_text)
        self.chat.data.append(
            icon=toga.Icon('icons/ubs-logo.png'),
            title='Bot',
            subtitle=response
        )
Beispiel #2
0
class ElizaConsole:
    def __init__(self, config_file):
        self.HISTORY_FILENAME = os.path.expanduser('~/.eliza_history')
        self.init_history(self.HISTORY_FILENAME)
        self.eliza = Eliza(config_file)

    def run(self):
        print(self.eliza.initial())

        while True:
            try:
                sent = input('> ')
            except EOFError:
                break

            output = self.eliza.respond(sent)
            if output is None:
                break

            print(output)

        print(self.eliza.final())

    def init_history(self, histfile):
        readline.parse_and_bind("tab: complete")
        if hasattr(readline, "read_history_file"):
            try:
                readline.read_history_file(histfile)
            except IOError:
                pass
            atexit.register(self.save_history, histfile)

    def save_history(self, histfile):
        readline.set_history_length(1000)
        readline.write_history_file(histfile)
Beispiel #3
0
from eliza import Eliza

eliza = Eliza(lang='id')
print(eliza.respond('aku bosan'))
Beispiel #4
0
#!/usr/bin/env python3
from eliza import Eliza

eliza = Eliza()
eliza.load('doctor-fr.txt')

print(eliza.initial())
while True:
    said = input('> ')
    response = eliza.respond(said)
    if response is None:
        break
    print(response)
print(eliza.final())