The realization of a Python intelligent chat robot for Valentine's Day

 

First package required by the project

import urllib.request
import urllib.parse
from tkinter import *
import time
PS:Many other people are learning Python In the process, I often give up because I can't solve the problem or I don't have a good tutorial. For this reason, I sorted out the basic python Script to web Development, reptile django,Data mining, etc[ PDF What you need can be entered Python Full stack development communication.Skirt: you can find it after a long time of fighting and thinking (homophony of numbers). It has the latest Python Tutorial project available,I don't understand that there are old drivers to solve the problems. We should supervise each other and make progress together

Function part
Note: call the server interface to realize non specific intelligent reply

def get_robot_replay(question):
    '''
    //Function function: answer specific questions and reply other non specific questions intelligently
    //Parameter Description:
    question: Chat content or questions
    //Return value: str, reply content
    '''
    if "What's your name?"in question:
        answer ="I am wandering."
    elif "How old are you?"in question:
        answer="18"
    elif "You are? GG still MM"in question:
        answer="MM"
    else:
        try:
            # Call NLP interface to realize intelligent reply
            params=urllib.parse.urlencode({'msg':question}).encode() #To convert str to byte type, the parameter interface needs to be URL encoded
            req = urllib.request.Request("http://api.itmojun.com/chat_robot",params,method="POST ") ා create request object
            answer=urllib.request.urlopen(req).read().decode()#Call interface (send HTTP request to target server)
        except Exception as e:
            answer="AI Robot failure!(Reason:%s)" % e
    return answer

Reply format and interface design

def msgsend():
    msg = 'I' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n'
    txt_msglist.insert(END, msg, 'green')  # Adding time
    txt_msglist.insert(END, txt_msgsend.get('0.0', END))  # Get send message, add text to message list
    msg1 = 'Travel big baby' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n'
    txt_msglist.insert(END, msg1, 'green')  # Adding time
    txt_msglist.insert(END,get_robot_replay(txt_msgsend.get('0.0', END)))
    txt_msgsend.delete('0.0', END)  # Clear send message

def cancel():
    txt_msgsend.delete('0.0', END)  # Cancel sending message, i.e. clear sending message


'''binding up key'''

def msgsendEvent(event):
    if event.keysym == 'Up':
        msgsend()

Chat interface design

tk = Tk()
tk.title('A secret chat with a big traveling baby')

'''Create partition'''
f_msglist = Frame(height=300, width=300)  # Create < message list partition >
f_msgsend = Frame(height=300, width=300)  # Create < send message partition >
f_floor = Frame(height=100, width=300)  # Create < button section >
f_right = Frame(height=700, width=100)  # Create < picture section >

'''Create controls'''
txt_msglist = Text(f_msglist)  # Create text control in message list partition
txt_msglist.tag_config('green', foreground='blue')  # Create label in message list partition
txt_msgsend = Text(f_msgsend)  # Create text control in send message partition
txt_msgsend.bind('<KeyPress-Up>', msgsendEvent)  # In the sending message partition, bind the "UP" key to send the message.


'''txt_right = Text(f_right) #Picture display partition create text control ''
button_send = Button(f_floor, text='Sendz', command=msgsend)  # Create button in button partition and bind send message function
button_cancel = Button(f_floor, text='Cancel', command=cancel)  # Create cancel button in partition and bind cancel function
'''Zoning layout'''
f_msglist.grid(row=0, column=0)  # Message list partition
f_msgsend.grid(row=1, column=0)  # Send message partition
f_floor.grid(row=2, column=0)  # Button partition
f_right.grid(row=0, column=1, rowspan=3)  # Picture display section
txt_msglist.grid()  # Message list text control load
txt_msgsend.grid()  # Message sending text control loading
button_send.grid(row=0, column=0, sticky=W)  # Send button control load
button_cancel.grid(row=0, column=1, sticky=W)  # Cancel button control loading


tk.mainloop()

Run screenshots

 

 

Not bad. In addition, many people often give up when they are learning Python because they can't solve problems or don't have good tutorials. So I sorted out the basic Python script, web development, crawler, django, data mining and other [PDF] needs that can be used in Python full stack development and communication : after a long time, you can find the latest Python tutorial project under the conversion of "flow and think" (homophony of numbers). If you don't understand the problem, you can solve it by the old driver. Let's supervise each other and make progress together
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Keywords: Python Web Development Django

Added by sidney on Fri, 14 Feb 2020 18:06:02 +0200