{"id":603,"date":"2018-11-24T19:40:26","date_gmt":"2018-11-24T19:40:26","guid":{"rendered":"http:\/\/ai.intelligentonlinetools.com\/ml\/?p=603"},"modified":"2018-11-26T01:53:30","modified_gmt":"2018-11-26T01:53:30","slug":"chatbots-examples-chatterbot","status":"publish","type":"post","link":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/","title":{"rendered":"Chatbots Examples with ChatterBot &#8211; How to Add Logic"},"content":{"rendered":"<div class=\"dbwpy69ef9355e029c\" ><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.dbwpy69ef9355e029c {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.dbwpy69ef9355e029c {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.dbwpy69ef9355e029c {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.dbwpy69ef9355e029c {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.dbwpy69ef9355e029c {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.dbwpy69ef9355e029c {\r\ndisplay: block;\r\n}\r\n}\r\n<\/style>\r\n<p>In the previous 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> I wrote how to deploy ChatterBot on pythonanywhere hosting site with Django webfamework. In this post we will look at few useful chatbots examples for implementing logic in our chatbot. This chatbot was developed in the previous post and is based on ChatterBot python library. <\/p>\n<h2>Making Chatbot Start Conversation with Specific Question<\/h2>\n<p>Suppose we want to start conversation with specific sentence that chatbot needs to show.  For example, when the user open website, the chatbot can start with the specific question: &#8220;Did you find what you were looking for?&#8221;<br \/>\nOr, if you are building chatbot for conversation about previous day\/week work, you probably want to start with &#8220;How was you previous day\/week in terms of progress to goal?&#8221;   How can we do this with ChatterBot framework?<\/p>\n<figure id=\"attachment_652\" aria-describedby=\"caption-attachment-652\" style=\"width: 471px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-content\/uploads\/2018\/11\/chatterbot.png\" alt=\"\" width=\"481\" height=\"331\" class=\"size-full wp-image-652\" srcset=\"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-content\/uploads\/2018\/11\/chatterbot.png 481w, http:\/\/ai.intelligentonlinetools.com\/ml\/wp-content\/uploads\/2018\/11\/chatterbot-300x206.png 300w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><figcaption id=\"caption-attachment-652\" class=\"wp-caption-text\">Conversation diagram<\/figcaption><\/figure>\n<p>It turns out, that ChatterBot has several logic adapters that allow to build conversation per different requirements.<\/p>\n<p>Here is how I used logic adapter <b>SpecificResponseAdapter<\/b> for chatbot to start with initial predefined question:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n chatbot = ChatBot(&quot;mybot&quot;,\r\n       logic_adapters=[\r\n       {\r\n            'import_path': 'chatterbot.logic.SpecificResponseAdapter',\r\n            'input_text': 'prev_day_disk',\r\n            'output_text': 'How much did you do toward your goal on previous day?'\r\n        }\r\n        .....\r\n<\/pre>\n<p>In views.py that was created in prev. post[1],  I put input &#8220;prev_day_disk&#8221; to runbot instead of  blank string. Because in the beginning of chat there is no user input and I used this to enter input_text and get desired output as specified in output_text.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef press_my_buttons(request):\r\n    resp=&quot;&quot;\r\n    conv=&quot;&quot;\r\n    if request.POST:\r\n        conv=request.POST.get('conv', '')\r\n        user_input=request.POST.get('user_input', '')\r\n\r\n        userid=request.POST.get('userid', '')\r\n        if (userid == &quot;&quot;):\r\n            userid=uuid.uuid4()\r\n\r\n        resp=runbot(user_input, request, userid)\r\n\r\n     \r\n        conv=conv + &quot;&quot; + str(user_input) + &quot;\\n&quot; + &quot;BOT:&quot;+ str(resp) + &quot;\\n&quot;\r\n    else:\r\n        resp=runbot(&quot;prev_day_disk&quot;, request, &quot;&quot;)\r\n        conv =  &quot;BOT:&quot;+ str(resp) + &quot;\\n&quot;;\r\n   \r\n    return render(request, 'my_template.html', {'conv': conv })\r\n<\/pre>\n<p><b>SpecificResponseAdapter<\/b>  can be used also in other places of conversation (not just in the beginning).  For example we could use criteria if there is no input from user during 15 secs and user is not typing anything (not sure yet how easily it is to check if user typing or not) then switch conversation to new topic by making chatbot app send new question. <\/p>\n<h2>How to Add Intelligence to Chatbot App<\/h2>\n<p>After the user replied to response how was his\/her week, I want chatbot to be able to recognize the response as belonging to one of the 3 groups: bad, so-so, good.  This is a machine learning classification problem.  However here we will not create text classification algorithm, instead we will use built in functionality.<\/p>\n<p>We will use another logic adapter, called <b>BestMatch<\/b>.<br \/>\nWith this adapter we need specify statement_comparison_function    and  response_selection_method :<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nchatbot = ChatBot(&quot;mybot&quot;,\r\n       logic_adapters=[\r\n       {\r\n            'import_path': 'chatterbot.logic.SpecificResponseAdapter',\r\n            'input_text': 'How much did you do toward your goal on previous day?',\r\n            'output_text': 'How much did you do toward your goal on previous day?'\r\n        },\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<\/pre>\n<p><b>Best Match Adapter<\/b> &#8211; is a logic adapter that returns a response based on known responses to the closest matches to the input statement. [2]<\/p>\n<p>The best match adapter uses a function to compare the input statement to known statements. Once it finds the closest match to the input statement, it uses another function to select one of the known responses to that statement.<\/p>\n<p>To use this adapter for the above example I need at minimum create 1 samples per each group. In the below example I used following for testing of  chatbot on 2 groups (skipped so-so).<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n if (train_bot == True):\r\n  chatbot.train([\r\n    &quot;I did not do much this week&quot;,\r\n    &quot;Did you run into the problems with programs or just did not have time?&quot;\r\n  ])\r\n\r\n\r\n  chatbot.train([\r\n    &quot;I did a lot of progress&quot;,\r\n    &quot;Fantastic! Keep going on&quot;\r\n  ])\r\n<\/pre>\n<p>After the training, if the user enters something close to &#8220;I did not do much this week&#8221; the chatbot will respond with &#8220;Did you run into the problems with programs or just did not have time?&#8221;, and if user enters something like &#8220;Did a lot of progress&#8221; the bot response will be &#8220;Fantastic! Keep going on&#8221; even if the input is slightly different from training. <\/p>\n<p>So we looked how to build a chatbot with logic that makes chatbot able to ask questions as needed or classify user input in several buckets and respond to user input accordingly.  The code for this post is provided at the link listed below[3] <\/p>\n<p><strong>References<\/strong><br \/>\n1. <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><br \/>\n2. <a href=\"https:\/\/chatterbot.readthedocs.io\/en\/stable\/logic\/index.html\" target=\"_blank\">ChatterBot &#8211; Logic<\/a><br \/>\n3.<a href=\"https:\/\/ai.intelligentonlinetools.com\/ml\/python-chatterbot-example-with-added-logic\/\" target=\"_blank\">Python Chatterbot Example with Added Logic<\/a>  &#8211; source code<\/p>\n<div class=\"ebksm69ef9355e02cd\" ><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.ebksm69ef9355e02cd {\r\nmargin: 5px; padding: 0px;\r\n}\r\n@media screen and (min-width: 1201px) {\r\n.ebksm69ef9355e02cd {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.ebksm69ef9355e02cd {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.ebksm69ef9355e02cd {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.ebksm69ef9355e02cd {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.ebksm69ef9355e02cd {\r\ndisplay: block;\r\n}\r\n}\r\n<\/style>\r\n","protected":false},"excerpt":{"rendered":"<p>In the previous post How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web I wrote how to deploy ChatterBot on pythonanywhere hosting site with Django webfamework. In this post we will look at few useful chatbots examples for implementing logic in our chatbot. This chatbot was developed in the &#8230; <a title=\"Chatbots Examples with ChatterBot &#8211; How to Add Logic\" class=\"read-more\" href=\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/\" aria-label=\"More on Chatbots Examples with ChatterBot &#8211; How to Add Logic\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[47],"tags":[48],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Chatbots Examples with ChatterBot - How to Add Logic - 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\/chatbots-examples-chatterbot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chatbots Examples with ChatterBot - How to Add Logic - Text Analytics Techniques\" \/>\n<meta property=\"og:description\" content=\"In the previous post How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web I wrote how to deploy ChatterBot on pythonanywhere hosting site with Django webfamework. In this post we will look at few useful chatbots examples for implementing logic in our chatbot. This chatbot was developed in the ... Read more\" \/>\n<meta property=\"og:url\" content=\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/\" \/>\n<meta property=\"og:site_name\" content=\"Text Analytics Techniques\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-24T19:40:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-11-26T01:53:30+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-content\/uploads\/2018\/11\/chatterbot.png\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/\",\"url\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/\",\"name\":\"Chatbots Examples with ChatterBot - How to Add Logic - Text Analytics Techniques\",\"isPartOf\":{\"@id\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/#website\"},\"datePublished\":\"2018-11-24T19:40:26+00:00\",\"dateModified\":\"2018-11-26T01:53:30+00:00\",\"author\":{\"@id\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/832f10562faaa1c7ed668c1ab4388857\"},\"breadcrumb\":{\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chatbots Examples with ChatterBot &#8211; How to Add Logic\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/#website\",\"url\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/\",\"name\":\"Text Analytics Techniques\",\"description\":\"Text Analytics Techniques\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/832f10562faaa1c7ed668c1ab4388857\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chatbots Examples with ChatterBot - How to Add Logic - 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\/chatbots-examples-chatterbot\/","og_locale":"en_US","og_type":"article","og_title":"Chatbots Examples with ChatterBot - How to Add Logic - Text Analytics Techniques","og_description":"In the previous post How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web I wrote how to deploy ChatterBot on pythonanywhere hosting site with Django webfamework. In this post we will look at few useful chatbots examples for implementing logic in our chatbot. This chatbot was developed in the ... Read more","og_url":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/","og_site_name":"Text Analytics Techniques","article_published_time":"2018-11-24T19:40:26+00:00","article_modified_time":"2018-11-26T01:53:30+00:00","og_image":[{"url":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-content\/uploads\/2018\/11\/chatterbot.png"}],"author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/","url":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/","name":"Chatbots Examples with ChatterBot - How to Add Logic - Text Analytics Techniques","isPartOf":{"@id":"https:\/\/ai.intelligentonlinetools.com\/ml\/#website"},"datePublished":"2018-11-24T19:40:26+00:00","dateModified":"2018-11-26T01:53:30+00:00","author":{"@id":"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/832f10562faaa1c7ed668c1ab4388857"},"breadcrumb":{"@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/ai.intelligentonlinetools.com\/ml\/chatbots-examples-chatterbot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ai.intelligentonlinetools.com\/ml\/"},{"@type":"ListItem","position":2,"name":"Chatbots Examples with ChatterBot &#8211; How to Add Logic"}]},{"@type":"WebSite","@id":"https:\/\/ai.intelligentonlinetools.com\/ml\/#website","url":"https:\/\/ai.intelligentonlinetools.com\/ml\/","name":"Text Analytics Techniques","description":"Text Analytics Techniques","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ai.intelligentonlinetools.com\/ml\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/832f10562faaa1c7ed668c1ab4388857","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ai.intelligentonlinetools.com\/ml\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"_links":{"self":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/types\/post"}],"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=603"}],"version-history":[{"count":19,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":664,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/posts\/603\/revisions\/664"}],"wp:attachment":[{"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.intelligentonlinetools.com\/ml\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}