I. Preface
I just learned some python file reading and writing, so I'll run over to the whole job first. By the way, I'm reviewing what I've learned before.
import time doc_local='D:\learning_folder\' def iRead(): fr = open(doc_local, 'r') message=() return message def iWrite(message): fw = open(doc_local, 'w') (message) () def dTime(num): a=() b=() while (b-a)<num: b=() return None answer={'i am temmie':'you are temmie? who am i?','temmie':'temmie!!!','wang!':'wangwangwang!!!'} iWrite('test begin') message0 = iRead() print(message0) while True: dTime(1.0000000) mes = iRead() if mes!=message0: v=(mes) if v==None: if mes=='q' : print('Exit Run') break else: print('message:', mes, end='\t') a_out=('temmie say again:{0}'.format(mes)) message0 = a_out iWrite(a_out) print(a_out) else: print('message:', mes, end='\t') b_out=('{0}'.format((mes))) message0 =b_out iWrite(b_out) print('answer:',b_out)
The effect, is that you write something in the notepad in this location and save it, it will reply to your message, if what you say is in the required dictionary, it will reply to your preset words. If it's not preset, it repeats your words (repeater).
I've exported each input and return
Second, the effect of the demonstration
You can even get crepper with it, or the whole idiom solitaire if you're idle enough.
III. Complete code
Three functions are defined to read the content, write the content, and with the timing
def iRead(): fr = open(doc_local, 'r') message=() return message def iWrite(message): fw = open(doc_local, 'w') (message) () def dTime(num): a=() b=() while (b-a)<num: b=() return None
ANSWER is a dictionary of pre-defined answers.
Write the contents first, clear the previous text, and prompt the program to begin. Reading the contents of the file is 'test begin'
iWrite('test begin') message0 = iRead()
Then it's a dead loop that doesn't quit until you type q.
Read the contents of the file every 1 second, if the contents of the file does not change as no input, the file content changes then the dictionary is searched, return None to prove that there is no corresponding answer, then repeat the reading. If a value is returned then the value is output.
while True: dTime(1.0000000) mes = iRead() if mes!=message0: v=(mes) if v==None: if mes=='q' : print('Exit Run') break else: print('message:', mes, end='\t') a_out=('temmie say again:{0}'.format(mes)) message0 = a_out iWrite(a_out) print(a_out) else: print('message:', mes, end='\t') b_out=('{0}'.format((mes))) message0 =b_out iWrite(b_out) print('answer:',b_out)
To this point this article on the Python combat live chatbot article is introduced to this, more related python chatbot content please search my previous articles or continue to browse the following related articles I hope you will support me more in the future!