Monday, April 19, 2010

A VERY SIMPLE CHATBOX IN PYTHON

A VERY SIMPLE CHATBOX IN PYTHON

A naive chatbot program. No parsing, no cleverness, just a training file and output.


It first trains itself on a text and then later uses the data from that training to generate responses to the interlocutor's input. The training process creates a dictionary where each key is a word and the value is a list of all the words that follow that word sequentially anywhere in the training text. If a word features more than once in this list then that reflects and it is more likely to be chosen by the bot, no need for probabilistic stuff just do it with a list.

The bot chooses a random word from your input and generates a response by choosing another random word that has been seen to be a successor to its held word. It then repeats the process by finding a successor to that word in turn and carrying on iteratively until it thinks it's said enough. It reaches that conclusion by stopping at a word that was prior to a punctuation mark in the training text. It then returns to input mode again to let you respond, and so on.

It isn't very realistic but I hereby challenge anyone to do better in 71 lines of code !! This is a great challenge for any budding Pythonists, and I just wish I could open the challenge to a wider audience than the small number of visitors I get to this blog. To code a bot that is always guaranteed to be grammatical must surely be closer to several hundred lines, I simplified hugely by just trying to think of the simplest rule to give the computer a mere stab at having something to say.

Its responses are rather impressionistic to say the least ! Also you have to put what you say in single quotes.

I used War and Peace for my "corpus" which took a couple of hours for the training run, use a shorter file if you are impatient...

here is the trainer

#lukebot-trainer.py
import pickle
b=open('war&peace.txt')
text=[]
for line in b:
for word in line.split():
text.append (word)
b.close()
textset=list(set(text))
follow={}
for l in range(len(textset)):
working=[]
check=textset[l]
for w in range(len(text)-1):
if check==text[w] and text[w][-1] not in '(),.?!':
working.append(str(text[w+1]))
follow[check]=working
a=open('lexicon-luke','wb')
pickle.dump(follow,a,2)
a.close()


here is the bot

#lukebot.py
import pickle,random
a=open('lexicon-dict','rb')
successorlist=pickle.load(a)
a.close()
def nextword(a):
if a in successorlist:
return random.choice(successorlist[a])
else:
return 'the'
speech=''
while speech!='quit':
speech=input('>')
s=random.choice(speech.split())
response=''
while True:
neword=nextword(s)
response+=' '+neword
s=neword
if neword[-1] in ',?!.':
break
print response


You tend to get an uncanny feeling when it says something that seems partially to make sense. I think this is a universal syndrome with chatbots and shows us a lot about our expectations and hopes for machine intelligence. The tendency to attribute some kind of surreal cosmic wisdom or irony to the responses is common.

TALKING TO TOLSTOY ! ...... ;)

Sample output:( is this like talking to Tolstoy? Not really but still...)

>>>
>'hello'
the human affairs he thought.
>'oh those affairs were great'
God,
>'what -surprised?'
do you value but she is what had the new country,
>'i do value but i do not blame the war on her'
so alarming them with those around with particular chair by all those who caressed me.
>'i never get anyone caressing me!'
allowed to talk about in a really so.
>'oh do not be prudish I am allowed to mention it'
should she suddenly heard of a duty for reason was handed him very hot.
>'you like these women a lot to talk about them so much'
advantage if your father?
>'my father approves of my romantic side'
love my son's having obeyed in the causes historic events that circle they didn't you know a force producing two Emperors with a certain prince.
>'oh now it is princes is it'
God,
>'do you believe in Him?'
the insistence of incongruities giving you married at his squadron--that is,
>' i don't understand you sir'
>'i do not understand you sir'
the bridge which,
>'i crossed'
himself,
>'ok ok... well 71 lines still is not bad'
the work at Smolensk?
>'yes'
or that of War.
>'no, war'
was gradually that millions of married in which is Dolokhov?


REFERENCES

(1) Pythomism - Luke's website

40 comments:

Dipanwita said...

This is a most interesting blog post. I look forward to more such posts. python training in Chennai

gowsalya said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
Digital Marketing online training

full stack developer training in pune

full stack developer training in annanagar

Mounika said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
python training institute in chennai
python training in Bangalore
python training in pune
python training institute in chennai
python training in velachery
python online training

Unknown said...

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

Blueprism training in btm

Blueprism online training

Unknown said...

Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.

Data Science training in kalyan nagar | Data Science training in OMR
selenium training in chennai | Data Science training in chennai
Data science training in velachery | Data science online training

sathya shri said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
angularjs Training in btm

angularjs Training in electronic-city

angularjs online Training

angularjs Training in marathahalli

angularjs interview questions and answers

priya rajesh said...

Your blog is amazing and informative. Keep sharing more like this.
Python Training in Chennai
Python classes in Chennai
ccna Training in Chennai
Angularjs Training in Chennai
RPA Training in Chennai
UiPath Training in Chennai
Blue Prism Training in Chennai
R Programming Training in Chennai

Anbarasan14 said...

Thanks to the admin for sharing this blog with us. The info in this blog was really helpful to me.
german classes in mulund
german language classes in mulund
german classes in mulund west
German Course in Mulund East
French Classes in Mulund
French Classes in Mulund East
French Classes in Mulund West
French Language Classes in Mulund

Calla said...

Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up.python training in Mumbai

priya said...

You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.

Microsoft Azure online training
Selenium online training
Java online training
Java Script online training
Share Point online training

Aishwarya said...

I am not sure the place you are getting your information, however good topic. I needs to spend some time studying more or understanding more. Thank you for wonderful information I was in search of this info for my mission.
Chatbot Company in India
Chatbot Company in Chennai
Chatbot Development Company in Chennai
Chatbot in Chennai
Chatbot Development Company in India

Softlogicseo said...


And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
Advanced C C++ Training in Chennai |Best C C++ Training course in Chennai
Advanced linux Training in Chennai | Best linux Training in Chennai
Advanced Unix Training in Chennai | Best Unix Training in Chennai
Advanced uipath training in chennai | Best uipath training in chennai
Advanced Rprogramming Training in Chennai | Best Rprogramming Training in Chennai

Unknown said...

I am really thankful for posting such useful information. It really made me understand lot of important concepts in the topic. Keep up the good work!
Oracle Training in Chennai | Oracle Course in Chennai

manisha said...

Thanks for sharing such a great blog Keep posting..
Python Training in Delhi
Python Course in Delhi

Vijiaajith said...

Really informative...


freeinplanttrainingcourseforECEstudents
internship-in-chennai-for-bsc
inplant-training-for-automobile-engineering-students
freeinplanttrainingfor-ECEstudents-in-chennai
internship-for-cse-students-in-bsnl
application-for-industrial-training

raju said...

nice post.................!
inplant training in chennai
inplant training in chennai for it.php
panama web hosting
syria hosting
services hosting
afghanistan shared web hosting
andorra web hosting
belarus web hosting
brunei darussalam hosting
inplant training in chennai

ammu said...

nice...
inplant training in chennai
inplant training in chennai
inplant training in chennai
brunei darussalam web hosting
costa rica web hosting
costa rica web hosting
hong kong web hosting
jordan web hosting
turkey web hosting
gibraltar web hosting

shiv said...

nice...................
inplant training in chennai
inplant training in chennai
inplant training in chennai for it.php
algeeria hosting
angola hostig
shared hosting
bangladesh hosting
botswana hosting
central african republi hosting
shared hosting

shree said...

very nice....
inplant training in chennai
very nice..
inplant training in chennai
inplant training in chennai for it.php
namibia web hosting
norway web hosting
rwanda web hosting
spain hosting
turkey web hosting
venezuela hosting
vietnam shared web hosting

preethi minion said...

excellent
inplant training in chennai
inplant training in chennai
inplant training in chennai for it.php
italy web hosting
afghanistan hosting
angola hosting
afghanistan web hosting
bahrain web hosting
belize web hosting
india shared web hosting

shri said...

nice....
internship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai


Trishana said...

Thank you for sharing this blog, it is very useful for understanding and learning python.
Even we also provide a tutorial to this topic.

nizam said...

very good article...


https://www.acte.in/angular-js-training-in-chennai
https://www.acte.in/angular-js-training-in-annanagar
https://www.acte.in/angular-js-training-in-omr
https://www.acte.in/angular-js-training-in-porur
https://www.acte.in/angular-js-training-in-tambaram
https://www.acte.in/angular-js-training-in-velachery

nizam said...

Nice information. keep sharing on.
AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery



latchu kannan said...

You are providing an valuable information. keep on doing.

AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery

aarthi said...

Gained a lot of technical knowledge.An impressive article.
Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training

RGB Web Tech said...

This article is amazingly helpful and intersting, Thanks for sharing this wonderful informative article here. I appreciate your hard work.

- Web Design, Web Development, Digital Marketing & App Development Company
- 24 HTML Editors and WYSIWYG Editors for Web Design & Development
- Top 19 Front-End Frameworks for Responsive Web Design
- Social Bookmarking Website List
- Directory Submission Website List

ganesh said...

This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot.
Angular js Training in Chennai

Angular js Training in Velachery

Angular js Training in Tambaram

Angular js Training in Porur

Angular js Training in Omr

Angular js Training in Annanagar



harini said...

Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog. This is a most interesting blog post. I look forward to more such posts
Selenium Training in Chennai

Selenium Training in Velachery

Selenium Training in Tambaram

Selenium Training in Porur

Selenium Training in Omr

Selenium Training in Annanagar

Hussey said...

Really nice blog. thanks for sharing
python training centre in chennai
best python institute in chennai


Reshma said...

This post is so interactive and informative.keep update more information...
Selenium Training in Bangalore
Selenium Training in Pune

manasha said...

Great post. keep sharing such a worthy information.
Google Analytics Training In Chennai
Google Analytics Online Course

Back linker said...

I'm grateful to you for sharing this plenty of valuable assessment. I found this asset most extreme helpful for me. much thanks to you a lot for better do whatever it takes. Edius X Crack

Back linker said...

I could following to appreciation for the undertakings you have made recorded as a printed copy this leaflet. I'm trusting the thesame fine take diversion doings from you inside the thinking of as capably. thankful to you...
Bitdefender Total Security Crack

jahanzaib33 said...


How we are not any more together will not at whatever point keep away from the way that you will ceaselessly be astounding for my heart.Ex Boyfriend Birthday Wishes

milka said...

Great post. keep sharing such a worthy information.
Python Institute In Chennai

Jennifer said...

Certainly a great deal more education is needed. For me, this is extremely significant.
Valhalla DSP Valhalla

iteducationcentre said...

That's an innovative blog post. Very interesting.
Python training in Nagpur

SHIVAM SHARMA said...

great, valuable post , i have learned many intresting thing about sql from your blog, thanks . keep posting and checking out my blog if your intresting in python course in satara

CRACKI said...

Only parameters active in a specific MODE are visible in the GUI. The STYLE button is one of Valhalla Delay’s most powerful features. It controls the ratio between the left and right delay channels and the number of delay voices in each channel. get at https://cracki.net/valhalla-reverb-crack/