{"id":556,"date":"2018-10-18T11:52:32","date_gmt":"2018-10-18T11:52:32","guid":{"rendered":"http:\/\/ai.intelligentonlinetools.com\/ml\/?page_id=556"},"modified":"2018-11-25T17:50:14","modified_gmt":"2018-11-25T17:50:14","slug":"python-chatbot-code-example","status":"publish","type":"page","link":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/","title":{"rendered":"Python Chatbot Code Example"},"content":{"rendered":"<div class=\"zkmnk6a99c6f209790\" ><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<!-- Text analytics techniques 728_90 horizontal top -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:728px;height:90px\"\n     data-ad-client=\"ca-pub-3416618249440971\"\n     data-ad-slot=\"2926649501\"><\/ins>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div><style type=\"text\/css\">\r\n.zkmnk6a99c6f209790 {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.zkmnk6a99c6f209790 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.zkmnk6a99c6f209790 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.zkmnk6a99c6f209790 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.zkmnk6a99c6f209790 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.zkmnk6a99c6f209790 {\r\ndisplay: block;\r\n}\r\n}\r\n<\/style>\r\n<p>This is the code for the post <a href=\"http:\/\/ai.intelligentonlinetools.com\/ml\/how-to-create-a-chatbot-with-chatbot-open-source\/\" target=\"_blank\">How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web<\/a><\/p>\n<p>The example here is showing how to use  Python library ChatterBot to create your own chatbot. In this example we train chatbot with few predefined conversations and with existing corpus chatterbot.corpus.english.greetings, chatterbot.corpus.english.conversations  and then we test chatbot.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom chatterbot import ChatBot\r\n\r\nchatbot = ChatBot(&quot;mybot&quot;,\r\n        logic_adapters=[\r\n        {\r\n            &quot;import_path&quot;: &quot;chatterbot.logic.BestMatch&quot;,\r\n            &quot;statement_comparison_function&quot;: &quot;chatterbot.comparisons.levenshtein_distance&quot;,\r\n            &quot;response_selection_method&quot;: &quot;chatterbot.response_selection.get_first_response&quot;\r\n        },\r\n        {\r\n            'import_path': 'chatterbot.logic.LowConfidenceAdapter',\r\n            'threshold': 0.65,\r\n            'default_response': 'I am sorry, but I do not understand.'\r\n        }\r\n    ],\r\n    trainer='chatterbot.trainers.ListTrainer')\r\n\r\n\r\n########################################\r\n###   TRAINING - run this just one time\r\n########################################\r\n\r\n\r\nchatbot.train([\r\n    'Good morning!',\r\n    'Good morning!',\r\n    'How are you today?',\r\n    'I am fine',\r\n    'Do you like machine learning?',\r\n    'Yes, I like machine learning'\r\n])\r\n\r\n\r\nchatbot.train([\r\n    'Good morning!',\r\n    'Good morning!'\r\n \r\n])\r\n\r\nchatbot.train([\r\n    'Hello',\r\n    'Hi there!'\r\n \r\n])\r\n\r\n\r\nchatbot.train([\r\n    'Let us talk about current activities',\r\n    'What are you working on now?',\r\n    'I am just browsing Internet for news',\r\n    'What a waste of time! Dont you have any other things to do?',\r\n    'I am working on python script to make new chatbot',\r\n    'This is great. Keep working on this'\r\n])\r\n\r\n\r\nchatbot.train(\r\n    &quot;chatterbot.corpus.english.greetings&quot;\r\n \r\n)\r\n\r\nchatbot.train(\r\n   &quot;chatterbot.corpus.english.conversations&quot;\r\n)\r\n\r\n\r\n\r\nfrom chatterbot.trainers import ListTrainer\r\n\r\nconversation = [\r\n    &quot;Hello&quot;,\r\n    &quot;Hi there!&quot;,\r\n    &quot;How are you doing?&quot;,\r\n    &quot;I'm doing great.&quot;,\r\n    &quot;That is good to hear&quot;,\r\n    &quot;Thank you.&quot;,\r\n    &quot;You're welcome.&quot;\r\n]\r\n\r\nchatbot.set_trainer(ListTrainer)\r\nchatbot.train(conversation)\r\n\r\n########################################\r\n###  END of  TRAINING\r\n########################################\r\n\r\n\r\n\r\nprint (&quot;USER: How are you doing?&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;How are you doing?&quot;)\r\nprint(&quot;BOT:&quot; + str(response))\r\nprint (&quot;USER: Hello&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;Hello&quot;)\r\nprint(&quot;BOT:&quot; + str(response))\r\nprint (&quot;USER: Good morning!&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;Good morning!&quot;)\r\nprint(&quot;BOT:&quot; + str(response))\r\n\r\n\r\n\r\nprint (&quot;USER: Do you like machine learning?&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;Do you like machine learning?&quot;)\r\nprint (&quot;BOT:&quot; + str(response))\r\nprint (&quot;USER: How do I make a neural network?&quot;)\r\n\r\nresponse = chatbot.get_response('How do I make a neural network?')\r\nprint(&quot;BOT&quot; + str(response))\r\nprint (&quot;USER: Let us talk about current activities&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;Let us talk about current activities&quot;)\r\nprint(&quot;BOT:&quot;+str(response))\r\n\r\nprint (&quot;USER: I am just browsing Internet for news&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;BOT: I am just browsing Internet for news&quot;)\r\nprint(&quot;BOT:&quot; + str(response))\r\n\r\nprint (&quot;USER: I am working on python script to make new chatbot&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;I am working on python script to make new chatbot&quot;)\r\nprint(&quot;BOT:&quot;+str(response))\r\n\r\n\r\nprint (&quot;USER: Bye&quot;)\r\n\r\nresponse = chatbot.get_response(&quot;Bye&quot;)\r\nprint(&quot;BOT:&quot; + str(response))\r\n\r\n<\/pre>\n<div class=\"mmpem6a99c6f2097c2\" ><center>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<!-- Text analytics techniques link ads horizontal Medium after content -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:468px;height:15px\"\n     data-ad-client=\"ca-pub-3416618249440971\"\n     data-ad-slot=\"5765984772\"><\/ins>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-3416618249440971\"\n     data-ad-slot=\"3903486841\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/center><\/div><style type=\"text\/css\">\r\n.mmpem6a99c6f2097c2 {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.mmpem6a99c6f2097c2 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.mmpem6a99c6f2097c2 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.mmpem6a99c6f2097c2 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.mmpem6a99c6f2097c2 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.mmpem6a99c6f2097c2 {\r\ndisplay: block;\r\n}\r\n}\r\n<\/style>\r\n","protected":false},"excerpt":{"rendered":"<p>This is the code for the post How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web The example here is showing how to use Python library ChatterBot to create your own chatbot. In this example we train chatbot with few predefined conversations and with existing corpus chatterbot.corpus.english.greetings, chatterbot.corpus.english.conversations and &#8230; <a title=\"Python Chatbot Code Example\" class=\"read-more\" href=\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\" aria-label=\"More on Python Chatbot Code Example\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/pages\/556"}],"collection":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":8,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/pages\/556\/revisions"}],"predecessor-version":[{"id":626,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/pages\/556\/revisions\/626"}],"wp:attachment":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/media?parent=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}