{"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=\"yhgoz69f23954a6f74\" ><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.yhgoz69f23954a6f74 {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.yhgoz69f23954a6f74 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.yhgoz69f23954a6f74 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.yhgoz69f23954a6f74 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.yhgoz69f23954a6f74 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.yhgoz69f23954a6f74 {\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=\"cahhu69f23954a6fa5\" ><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.cahhu69f23954a6fa5 {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.cahhu69f23954a6fa5 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.cahhu69f23954a6fa5 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.cahhu69f23954a6fa5 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.cahhu69f23954a6fa5 {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.cahhu69f23954a6fa5 {\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":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Chatbot Code Example - Text Analytics Techniques<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Chatbot Code Example - Text Analytics Techniques\" \/>\n<meta property=\"og:description\" content=\"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 ... Read more\" \/>\n<meta property=\"og:url\" content=\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Text Analytics Techniques\" \/>\n<meta property=\"article:modified_time\" content=\"2018-11-25T17:50:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\",\"url\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\",\"name\":\"Python Chatbot Code Example - Text Analytics Techniques\",\"isPartOf\":{\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/#website\"},\"datePublished\":\"2018-10-18T11:52:32+00:00\",\"dateModified\":\"2018-11-25T17:50:14+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Chatbot Code Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/#website\",\"url\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/\",\"name\":\"Text Analytics Techniques\",\"description\":\"Text Analytics Techniques\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Chatbot Code Example - Text Analytics Techniques","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/","og_locale":"en_US","og_type":"article","og_title":"Python Chatbot Code Example - Text Analytics Techniques","og_description":"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 ... Read more","og_url":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/","og_site_name":"Text Analytics Techniques","article_modified_time":"2018-11-25T17:50:14+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/","url":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/","name":"Python Chatbot Code Example - Text Analytics Techniques","isPartOf":{"@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/#website"},"datePublished":"2018-10-18T11:52:32+00:00","dateModified":"2018-11-25T17:50:14+00:00","breadcrumb":{"@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/python-chatbot-code-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/ai.intelligentonlinetools.com\/ml\/"},{"@type":"ListItem","position":2,"name":"Python Chatbot Code Example"}]},{"@type":"WebSite","@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/#website","url":"http:\/\/ai.intelligentonlinetools.com\/ml\/","name":"Text Analytics Techniques","description":"Text Analytics Techniques","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/ai.intelligentonlinetools.com\/ml\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"}]}},"_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}]}}