<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by C.A.M. on Medium]]></title>
        <description><![CDATA[Stories by C.A.M. on Medium]]></description>
        <link>https://medium.com/@michi_yo?source=rss-975b94d38c8f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DhIW_V0nnYzZ69X7XUKbyg.png</url>
            <title>Stories by C.A.M. on Medium</title>
            <link>https://medium.com/@michi_yo?source=rss-975b94d38c8f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:13:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michi_yo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Chat Bot With Image Recognition and OCR]]></title>
            <link>https://medium.com/data-science/building-a-chat-bot-with-image-recognition-and-ocr-721ee7b2a70b?source=rss-975b94d38c8f------2</link>
            <guid isPermaLink="false">https://medium.com/p/721ee7b2a70b</guid>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[C.A.M.]]></dc:creator>
            <pubDate>Fri, 28 Dec 2018 06:23:19 GMT</pubDate>
            <atom:updated>2019-01-04T06:32:54.730Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a Chat Bot With Object Detection and OCR</h3><p>In part 1 of this series, we gave our bot the ability to detect sentiment from text and respond accordingly. But that’s about all it can do, and admittedly quite boring.</p><p>Of course, in a real chat, we often send a multitude of media: from text, images, videos, gifs, to anything else. So in this, our next step in our journey, let’s give our bot vision. The goal of this tutorial is to allow our bot to receive images, reply to them, and eventually give us a crude description of the main object in said image.</p><p>Let’s get started!</p><p>If you haven’t followed along, you can find the latest code here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/462e27e890aea0dd3b5264965fcc2935/href">https://medium.com/media/462e27e890aea0dd3b5264965fcc2935/href</a></iframe><p>So the code we want to modify is in our event response cycle method, here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d708f7830114d4a7661c48588e852bbb/href">https://medium.com/media/d708f7830114d4a7661c48588e852bbb/href</a></iframe><p>Our bot already responds to images, but it has no idea what they are and responds in a rather bland way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*-iZjBlCs6KSvIT_Hxs6rQA.gif" /></figure><p>We can try it out, and see for ourselves. Let’s fire up our server (and ngrok), and send our bot an image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*0QL5R3cq03ndDufJbVAqbw.png" /></figure><p>So far so good. Our bot at least knows when it receives an image.</p><p>In this series, we have been using google cloud APIs, so for our image detection, we’ll be using Google Cloud Vision. Follow the quick-start here to get your project all set up: <a href="https://cloud.google.com/vision/docs/quickstart-client-libraries">https://cloud.google.com/vision/docs/quickstart-client-libraries</a>. Remember to use the same project that we set up in part 1.</p><p>Once you have completed that, now it’s time to get back to coding. Let’s add the following to our Gemfile and run bundle install:</p><pre>gem &#39;google-cloud-vision&#39;</pre><p>Let’s require it in <strong>main.rb</strong> by adding the following:</p><pre>require ‘google/cloud/vision’</pre><p>Next, we want to create an instance of the cloud language API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e4f3be17526e5ca3f31ac4687309eac/href">https://medium.com/media/8e4f3be17526e5ca3f31ac4687309eac/href</a></iframe><p>The vision API feature that we want to use is called <a href="https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-vision/latest/Google/Cloud/Vision/Annotation.html"><strong>annotation</strong></a>. Given a <strong>file path</strong> to an image on your local machine, it will attempt to identify the image based on values we pass to the method call.</p><p>In the following example (from Google’s documentation):</p><pre>vision = <a href="https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-vision/latest/Google.html"><strong>Google</strong></a>::<a href="https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-vision/latest/Google/Cloud.html"><strong>Cloud</strong></a>::<a href="https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-vision/latest/Google/Cloud/Vision.html"><strong>Vision</strong></a>.<a href="https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-vision/latest/Google/Cloud/Vision.html#new-class_method">new</a><br>image = vision.image &quot;path/to/face.jpg&quot;<br><br>annotation = vision.annotate image, faces: <strong>true</strong>, labels: <strong>true</strong><br>annotation.faces.count <em>#=&gt; 1<br></em>annotation.labels.count <em>#=&gt; 4<br></em>annotation.text <em>#=&gt; nil</em></pre><p>We are telling the vision API attempt to recognize faces and labels. “Labels” are essentially objects that the API determines it has identified. Provided a picture of a dog, we would potentially be given the following labels:</p><pre>{<br>  &quot;responses&quot;: [<br>    {<br>      &quot;labelAnnotations&quot;: [<br>        {<br>          &quot;mid&quot;: &quot;/m/0bt9lr&quot;,<br>          &quot;description&quot;: &quot;dog&quot;,<br>          &quot;score&quot;: 0.97346616<br>        },<br>        {<br>          &quot;mid&quot;: &quot;/m/09686&quot;,<br>          &quot;description&quot;: &quot;vertebrate&quot;,<br>          &quot;score&quot;: 0.85700572<br>        },<br>        {<br>          &quot;mid&quot;: &quot;/m/01pm38&quot;,<br>          &quot;description&quot;: &quot;clumber spaniel&quot;,<br>          &quot;score&quot;: 0.84881884<br>        },<br>        {<br>          &quot;mid&quot;: &quot;/m/04rky&quot;,<br>          &quot;description&quot;: &quot;mammal&quot;,<br>          &quot;score&quot;: 0.847575<br>        },<br>        {<br>          &quot;mid&quot;: &quot;/m/02wbgd&quot;,<br>          &quot;description&quot;: &quot;english cocker spaniel&quot;,<br>          &quot;score&quot;: 0.75829375<br>        }<br>      ]<br>    }<br>  ]<br>}</pre><p>Let’s create the following method to utilize this functionality:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50cef764bc370ed6ee593c0a421025b1/href">https://medium.com/media/50cef764bc370ed6ee593c0a421025b1/href</a></iframe><p>The above (admittedly naive) method takes a file path and returns a string, based on the results of the Google cloud vision’s API. In the <strong>annotate</strong> method, we are passing a few parameters, which tells the API what we want to try to detect.</p><p>The return (response) for this method is a cascading short-circuit flow, first checking for famous landmarks, any text, and finally any objects (labels) it has detected. This flow is purely arbitrary and simplified for purposes of this tutorial (i.e. don’t email me about how it can be improved).</p><p>Let’s try it on the following picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*gYAKcwU81_4SmzwHZDIe8A.jpeg" /></figure><p>And the results (truncated):</p><pre>description: &quot;cuisine&quot;, score: 0.9247923493385315, confidence: 0.0, topicality: 0.9247923493385315, bounds: 0, locations: 0, properties: {}<br>description: &quot;sushi&quot;, score: 0.9149415493011475, confidence: 0.0, topicality: 0.9149415493011475, bounds: 0, locations: 0, properties: {}<br>description: &quot;food&quot;, score: 0.899940550327301, confidence: 0.0, topicality: 0.899940550327301, bounds: 0, locations: 0, properties: {}<br>description: &quot;japanese cuisine&quot;, score: 0.8769422769546509, confidence: 0.0, topicality: 0.8769422769546509, bounds: 0, locations: 0, properties: {}</pre><p>Since there were no landmarks or text, we have received the labels the API was able to detect. In this case, we see that it has been identified as “sushi.” In my experience with the label detection results, the second label (having the second highest topicality) tends to be how an average person would identify the picture.</p><p>Let’s give it another go on the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X43sWKcN7KaP9xNebEfHWQ.jpeg" /></figure><p>The output (again truncated):</p><pre>description: &quot;wildlife&quot;, score: 0.9749518036842346, confidence: 0.0, topicality: 0.9749518036842346, bounds: 0, locations: 0, properties: {}<br>description: &quot;lion&quot;, score: 0.9627781510353088, confidence: 0.0, topicality: 0.9627781510353088, bounds: 0, locations: 0, properties: {}<br>description: &quot;terrestrial animal&quot;, score: 0.9247941970825195, confidence: 0.0, topicality: 0.9247941970825195, bounds: 0, locations: 0, properties: {}</pre><p>And there we see it, with “lion” being the second hit.</p><p>Ok, another for good measure, let’s try some text extraction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hACGuAihwfxJaPzRYAswCw.png" /><figcaption>Just a screenshot of my text editor</figcaption></figure><p>And let&#39;s see what we get:</p><pre>2.4.2 :022 &gt; puts analyze_image(&quot;major_general.png&quot;)<br>I am the very model of a modern Major-General,<br>I&#39;ve information vegetable, animal, and mineral,<br>I know the kings of England, and I quote the fights historical<br>From Marathon to Waterloo, in order categorical;<br>I&#39;m very well acquainted, too, with matters mathematical,<br>I understand equations, both the simple and quadratical,<br>About binomial theorem I&#39;m teeming with a lot o&#39; news, (bothered for a rhyme)<br>With many cheerful facts about the square of the hypotenuse.<br> =&gt; nil<br>2.4.2 :023 &gt;</pre><p>Not bad.</p><p>Ok, last one for completeness’ sake. Let’s try a landmark:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OMPb_rz_MIs8RPpEl0JNIw.jpeg" /></figure><p>And our method gives us:</p><pre>2.4.2 :030 &gt; puts analyze_image(“statue_of_liberty.jpg”)<br>Statue of Liberty</pre><p>Ok, so our method is working as intended, now let’s actually use it with our chatbot.</p><p>When we send an image to our chatbot through our client (Line), the client returns the image data in the response body (along with other relevant information) to our callback. Because our image recognition method needs a file path, we will have to save the aforementioned image data to our local machine.</p><p>Let’s modify our method to do that. Change the relevant parts of your callback method to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35735d45115dc0cae886b9f4c0d211cb/href">https://medium.com/media/35735d45115dc0cae886b9f4c0d211cb/href</a></iframe><p>There is a bit going on here. First, we are creating a new <a href="https://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html">Tempfile</a>, and using the response body (image data) as its content. We’re then passing the tempfile’s path to the <strong>analye_image</strong> method we just tested in the console. Let’s try it with our bot, just as a sanity check.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*nQo4WdSfMrn0oAYDNZjXdA.png" /><figcaption>Such a nice bot…</figcaption></figure><p>And it was able to successfully identify a landmark for us.</p><p>Our bot is now just working as a glorified console print line, and that’s not very chatty at all. We want this thing to sound more natural, let’s clean up our method a bit to make it sound more “human”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*ZN1KndpvpwzpynGmVBUq7g.gif" /><figcaption>In fact, it is.</figcaption></figure><p>Let’s make the necessary changes to our code. We’ll be modifying an existing method <strong>analyze_image </strong>and creating a new method <strong>get_analyze_image_response</strong>. Here it is below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19313909ba6cc5a91768d1bd783dc8d3/href">https://medium.com/media/19313909ba6cc5a91768d1bd783dc8d3/href</a></iframe><p>Again, this is not a tutorial about Ruby, but rather concepts; however, let’s go over what we’ve just done. In <strong>analyze_image </strong>we simply removed the string reply and replaced it with our new method <strong>get_analyze_image_response. </strong>This method takes an annotation object, and based on the type of object identified in the image, builds a sentence (string) using the annotation object’s description values.</p><p>Let’s try it out!</p><p>A classic:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*4rRcUtJ2fET8WcRsi1KiOA.png" /><figcaption>It’s a bacon cheeseburger, but I’ll give you that one.</figcaption></figure><p>And now a landmark:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*LBsmC3PGn1LobLk9W2woPQ.png" /><figcaption>It indeed is!</figcaption></figure><p>And that’s it! Our bot now extracts text from images using optical character recognition, and also give us a basic description of objects it finds in any image we send it.</p><p>Currently, our bot can only reply to one-off messages. But what if it had a “memory”, and was able to actually able to have a real conversation? We will cover multi-step communication in Part 3.</p><p>Below is all of our code up to this point:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56d53a69cc13ce25a61ddc444f1f3c7b/href">https://medium.com/media/56d53a69cc13ce25a61ddc444f1f3c7b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=721ee7b2a70b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-a-chat-bot-with-image-recognition-and-ocr-721ee7b2a70b">Building a Chat Bot With Image Recognition and OCR</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Sentiment Detection Bot with Google Cloud, a Chat Client, and Ruby.]]></title>
            <link>https://medium.com/data-science/building-a-sentiment-detection-bot-with-google-cloud-a-chat-client-and-ruby-cc280c61c6f0?source=rss-975b94d38c8f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc280c61c6f0</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[japan]]></category>
            <dc:creator><![CDATA[C.A.M.]]></dc:creator>
            <pubDate>Fri, 26 Oct 2018 15:25:12 GMT</pubDate>
            <atom:updated>2019-06-15T12:02:56.232Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this series, I’ll explain how to create a chatbot that is capable of detecting sentiment, analyzing images, and finally having the basis of an evolving personality. This is part 1 of that series.</p><h3>The Pieces</h3><ol><li>Ruby</li><li>Sinatra</li><li>Google Cloud APIs</li><li>Line (a chat client)</li></ol><p>Since I live in <strong>Japan:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3gFH3kSelDAPWVqdlbc4cQ.jpeg" /></figure><p>I’ll be using the popular chat service <a href="https://line.me/en/">Line</a> as our client. For sentiment detection, we will be using <a href="https://cloud.google.com/natural-language/">Google Cloud Natural Language API</a>. And the language of choice will be Ruby (because, why not), and the micro framework <a href="http://sinatrarb.com/">Sinatra</a> as our server. Ok, let’s get this going!</p><h3>Setting Up The Chat Client</h3><p>Line is an extremely popular chat service here; however, you don’t need to actually live here to sign up. So you should be able to follow along with no issues.</p><p>Once you have downloaded the app and made an account, you can sign up as a developer here: <a href="https://developers.line.me/en/">Line Developers Page</a>.</p><p>We’ll need to create a new “Provider”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v8V4diUD2OzD4MGkMipXXQ.png" /></figure><p>and a Messaging API “Channel”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7L0LVK32MOU9RsYtYWJ4g.png" /></figure><p>Now we have to give our channel a name (which will be our bot’s display name), and app icon (display picture). Since this is a sentiment detection bot, let’s go with the apt: “Sentiment Bot”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Id1b331zPSyJbZzwfVaaWA.png" /><figcaption>Just a bit creepy…</figcaption></figure><p>Upon creation take note of the following values:</p><ol><li>channel secret</li><li>channel access token</li></ol><p>Also, make the following changes to the settings:</p><ol><li>Use webhooks: <strong>enabled</strong></li><li>Auto-reply messages: <strong>disabled</strong></li><li>Greeting messages: <strong>disabled</strong></li></ol><p>Finally, we have to set our “Webhook URL”: this is the entry point to bot and client communication. Whenever a message is sent to this channel (our bot), it will be relayed to whatever endpoint we designate in this field. Since we don’t have that yet, our next step is to create one.</p><h3>Setting Up The Sever</h3><p>The server is the key element in our bot example. For all intents and purposes, it is the “bot”. Since we are using a 3rd party chat client (Line) as our main communication channel, we need to define an endpoint or webhook, for data transmission.</p><p>A webhook, in this context, is an API endpoint that the chat client will post to on receipt of a message. Then it’s up to you (your bot) to parse the message and extract the necessary parts for processing. Once processed, we send this information back to our chat client via their messaging API, for eventual receipt by the end user (i.e. the person chatting with your soon to be bot).</p><p>In this example, we’ll be using Ruby along with the micro-framework Sinatra.</p><p>Let’s go ahead and create our app:</p><pre>mkdir sentiment_bot &amp;&amp; cd $_</pre><pre>bundle init</pre><pre>touch main.rb</pre><p>Please note that these file and directory names are completely arbitrary, and are chosen for simplicity and consistency’s sake.</p><p>Alright, let’s go ahead and install Sinatra (our web framework), by adding the following to the Gemfile.</p><pre>gem &#39;sinatra&#39;</pre><p>As a sanity check, let&#39;s create a barebones endpoint that returns one response when we hit it. As a take on “Hello World”, this is going to be our “こんにちは world”. Ok, let’s get going, add the following method to main.rb.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f153c9fe880d8b7b2039668d945207b/href">https://medium.com/media/8f153c9fe880d8b7b2039668d945207b/href</a></iframe><p>Fire up the server:</p><pre>ruby main.rb</pre><p>and visit<a href="/service/localhost:4567/hello"> localhost:4567/hello</a>. We should see our greeting coming through loud and clear. All is right in the world. Now, lets set up the endpoint that will be used as the communication layer between our chat client, and the bot. Per Line’s requirements, and as a good practice, this route will be a “Post”, and as for a name: “callback” is as good as any.</p><p>Add the following to main.rb:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76b9cc07a432e767de10c5e552b640c0/href">https://medium.com/media/76b9cc07a432e767de10c5e552b640c0/href</a></iframe><p>It’s fine to leave this as an empty route for now. Back in Line, our webhook needs to be a publicly accessible https URL. However, our server is running locally. One way we can do this is to push our code to a <a href="https://en.wikipedia.org/wiki/Platform_as_a_service">PaaS</a> such as <a href="https://www.heroku.com/">Heroku</a>, but that’s a bit orthogonal the scope of this tutorial. For now, let’s just use the amazing service <a href="https://ngrok.com/">Ngrok</a>, for HTTP tunneling. After you have <a href="https://ngrok.com/download">installed</a> ngrok and fired up your web server, you can run the following command to get a public URL to our local server:</p><pre>ngrok http 4567</pre><p>This tells the ngrok service to create a URL for the local port 4567 (which is Sinatra’s default port). Upon running this, we should see two URLs printed to the console, one http, and the other https. We will be using the https URL. It should be something similar to:</p><pre><a href="https://some_hex_string.ngrok.io/">https://some_hex_string.ngrok.io</a></pre><p>Copy it, and enter it as your bots webhook url, as:</p><pre><a href="https://some_hex_string.ngrok.io/callback">https://some_hex_string.ngrok.io/callback</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*57Lo3zDCBYkSbNe2WNEpFA.png" /></figure><p>The additional path “callback” refers to the “post” method we just added in main.rb.</p><p>Once you’ve entered the URL, you should see a button to “verify”. Upon clicking, we should see a hit to our local server, and on Line, some confirmation of success.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*EYYAWLnrOxRudhfcWZHB0Q.png" /><figcaption>Upon Successful Verification</figcaption></figure><p>The next step is to add our bot as a friend on Line, and try to talk to it. We haven’t given it any functionality, so our goal here is simply to see if everything is communicating correctly: i.e. when a message is sent to our bot, our callback url is actually triggered.</p><p>Scan the QR code provided for your bot using your Line mobile client. Once you have added your bot, send it a message. You should again see a hit to our server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*6loRM2389_Rmy3F_MDHdRw.png" /><figcaption>Sure, go ahead and add my bot.</figcaption></figure><p>We have verified everything is all connected, but a chatbot is pretty boring if it doesn’t actually talk. Now off to the fun part.</p><h3>Setting up Line SDK</h3><p>Of course, we can write our own Line API tools using their docs, but this is time-consuming and beyond the scope of this tutorial. Also, Line just happens to have an official SDK.</p><p>Add the following to your Gemfile and run bundle install:</p><pre>gem &#39;line-bot-api&#39;</pre><p>Now that the gem is installed, we need to set up our credentials (which we can get from our dev console in Line).</p><p>Add the following import to main.rb</p><pre>require &#39;line/bot&#39;</pre><p>And the following as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1078078bb1606063721cf94729891830/href">https://medium.com/media/1078078bb1606063721cf94729891830/href</a></iframe><p>This method will allow us to initiate authenticated requests to our bot/channel. Each bot you make will have a unique secret and access token — you should not make this information public, or check it into your version control system.</p><p>Let’s go ahead and set the environment variables we received from Line:</p><pre>export LINE_CHANNEL_SECRET=&quot;you_channel_secret&quot;<br>export LINE_CHANNEL_ACCESS_TOKEN=&quot;you_access_token&quot;</pre><p>Now, let’s change our ‘callback’ function to the following (taken from Line’s sdk docs):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e24b4974f51bdac1175fc2250ada5cbc/href">https://medium.com/media/e24b4974f51bdac1175fc2250ada5cbc/href</a></iframe><p>Now, our bot should act as an echo server, let’s test it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zXuUYbzirHZUjS89KozhHA.png" /><figcaption>Rude…</figcaption></figure><p>Ok, everything is looking good! We’ve named our bot “SentimentBot”, so it should probably be able to detect sentiment. Sentiment, in the context of natural language processing, measures whether a given input is negative or positive in relation to human emotion (i.e. bad or good). An input of “I love you!” would be regarded as positive, while “I hate you!” would be considered negative.</p><h3>Setting Up Google Natural Language</h3><p>If this is your first time using Google Cloud and thus, Google’s latest authentication system, then you can check out their detailed documentation here: <a href="https://cloud.google.com/natural-language/docs/quickstart-client-libraries">https://cloud.google.com/natural-language/docs/quickstart-client-libraries</a>.</p><p>Once you have completed your project setup and have your auth all sorted, we can go ahead and add the ruby client to our Gemfile and of course run bundle install:</p><pre>gem &#39;google-cloud-language&#39;</pre><p>If you have set up your service account correctly and installed the client SDK, then copy the sample code from the <a href="https://github.com/GoogleCloudPlatform/ruby-docs-samples/blob/master/language/quickstart.rb">docs</a> and run it. You should something similar to the following output if all is well:</p><pre>Overall document sentiment: (0.30000001192092896)<br>Sentence level sentiment:<br>Hello world: (0.30000001192092896)</pre><p>Once this is all working, let’s integrate it into our bot server code. Currently, our bot only mimics any text or image that is sent to it, but now with the power of Google on our side, let’s make it a bit fancier. Our next steps will be the following:</p><ol><li>Read any text sent to the bot</li><li>Send text to google for sentiment analysis</li><li>Return the sentiment score to the user via our bot</li></ol><p>First, let’s make a function that detects and returns the sentiment of its input. We can do that with the following methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03a432306062baa910f80cf9a46c7a54/href">https://medium.com/media/03a432306062baa910f80cf9a46c7a54/href</a></iframe><p>These methods are quite straightforward. “<strong>google_language_client</strong>” simply instantiates the client, and “<strong>get_sentiment</strong>” uses said client and returns the sentiment analysis of any viable text input.</p><p>Let&#39;s change our “/post” route to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c81c0cf46d56e18f46f02a96a3fe5e69/href">https://medium.com/media/c81c0cf46d56e18f46f02a96a3fe5e69/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AoJT8F0ZVkqCWHtyGsEg1g.png" /><figcaption>That settles it…</figcaption></figure><p>Given all of your external services are set properly, you should be able to chat with your bot, and it returns the sentiment score of your text. Let’s try it out:</p><p>Just returning a sentiment score is pretty boring for anyone unfortunate enough to interact with such a bot. Let’s give it a bit of personality.</p><p>Let’s modify our main.rb file to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/462e27e890aea0dd3b5264965fcc2935/href">https://medium.com/media/462e27e890aea0dd3b5264965fcc2935/href</a></iframe><p>We made a few changes here. First, we added some constants to hold our text responses, and we also added a new method “<strong>get_sentiment_response</strong>” which checks the sentiment score and returns an appropriate reply. Let’s try it out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YSu3rViNoBpIiPKPcWdkVA.png" /></figure><p>And there we have it! Our bot can detect sentiment in a message, albeit with a limited vocabulary, and respond accordingly.</p><p>In the next post in this series, we will give our bot the ability to understand the images we send it, by utilizing Google Cloud Vision API.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc280c61c6f0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-a-sentiment-detection-bot-with-google-cloud-a-chat-client-and-ruby-cc280c61c6f0">Building a Sentiment Detection Bot with Google Cloud, a Chat Client, and Ruby.</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>