<?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 Harvinder Singh on Medium]]></title>
        <description><![CDATA[Stories by Harvinder Singh on Medium]]></description>
        <link>https://medium.com/@harvinder809?source=rss-2d24315f2dd6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*t-rFw9_Mk22kxA2Y</url>
            <title>Stories by Harvinder Singh on Medium</title>
            <link>https://medium.com/@harvinder809?source=rss-2d24315f2dd6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 23:02:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harvinder809/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[Use Pandas Apply Method Like a Pro!]]></title>
            <link>https://medium.com/analytics-vidhya/use-pandas-apply-method-like-a-pro-bcdccf1210f?source=rss-2d24315f2dd6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcdccf1210f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pandas-dataframe]]></category>
            <dc:creator><![CDATA[Harvinder Singh]]></dc:creator>
            <pubDate>Sun, 14 May 2023 17:47:17 GMT</pubDate>
            <atom:updated>2023-05-14T17:47:17.797Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever faced difficulty while using the pandas apply method? If Yes, then this tutorial is for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AP5MoNNBc63g0oVC" /><figcaption>Photo by <a href="https://unsplash.com/@adamkool?utm_source=medium&amp;utm_medium=referral">Adam Kool</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>The <strong>apply()</strong> method is a powerful tool that allows you to apply a custom function to every element of a DataFrame or Series, making it easier to perform complex data manipulations and calculations.</blockquote><p>In this article, We are going to learn how to use Series.apply() &amp; DataFrame.apply() method. What is the difference between Series.apply() and DataFrame.apply() method. How to use them with functions with arguments and finally when to use one.</p><h3>Table of Contents</h3><ol><li><a href="#8965">Series.apply() Method</a></li></ol><p>2. <a href="#fcae">Use of arguments in Series.apply() method</a></p><p>3. <a href="#eec5">Use of keyword arguments in Series.apply() method</a></p><p>4. <a href="#a8e1">Difference Between DataFrame.apply() and Series.apply() method</a></p><p>5. <a href="#7e39">DataFrame.apply() method</a></p><p>6. <a href="#ba79">Use of arguments in DataFrame.apply() method</a></p><p>7. <a href="#e0d5">Conclusion</a></p><h3>1. Series.apply() method</h3><p>First, we will talk about how to use <strong>apply()</strong> method with <strong>Pandas Series. </strong>Let’s create one Pandas Series to work with.</p><p>Here I have created one simple Pandas Series with 5 values i.e. 10,20,30,40 and 50.</p><pre>#Create a Series<br>s=pd.Series([10,20,30,40,50])<br>print(s)</pre><p><strong>Output:</strong></p><pre>0    10<br>1    20<br>2    30<br>3    40<br>4    50<br>dtype: int64p</pre><p>Now I want to apply some function, for example, <strong>square root</strong>, to each &amp; every value of the Series. I can do this with <strong>Series.apply()</strong> method. All I need to do is to call <strong>apply()</strong> method with the <a href="https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html"><strong>np.sqrt</strong></a> function.</p><pre>s.apply(np.sqrt)</pre><p><strong>Output:</strong></p><p>Here we can see that square root is applied to each row value and we can see that in the output.</p><pre>0    3.162278<br>1    4.472136<br>2    5.477226<br>3    6.324555<br>4    7.071068<br>dtype: float64</pre><p>We can apply the same function using <strong>lambda</strong> as well. Let’s see how.</p><pre>s.apply(lambda x:np.sqrt(x))</pre><p>The output of the above line of the code will be the same as the previous one.</p><p>Output:</p><pre>0    3.162278<br>1    4.472136<br>2    5.477226<br>3    6.324555<br>4    7.071068<br>dtype: float64</pre><p>We can also use our custom function with apply method instead of the built-in one. Let’s see how can we do that.</p><p>Suppose We want to <strong>add 5</strong> to each row if the value of the row is <strong>less than 30 </strong>&amp; <strong>subtract 5</strong> from each row if the value is <strong>greater than or equal to 30</strong>. Let’s define one custom function for this.</p><pre>#define a custom function<br>def custom_add_subtract(x):<br>    if x&lt;30:<br>        x+=5<br>    else:<br>        x-=5<br>    return x</pre><p>Now call apply method with our custom function name as an argument.</p><pre>s.apply(custom_add_subtract)</pre><p><strong>Output:</strong></p><pre>0    15<br>1    25<br>2    25<br>3    35<br>4    45<br>dtype: int64</pre><h4>2. Use of arguments in Series.apply() method</h4><p>Now I have changed my mind. I want to add <strong>10</strong> instead of <strong>5</strong> when the value is less than 30 and subtract <strong>10</strong> when the value is greater than or equal to 30.</p><p>To implement this, I need to define a new custom function with a new value and then use the new function.</p><pre>#define new function for value 10<br>def custom_add_subtract_10(x):<br>    if x&lt;30:<br>        x+=10<br>    else:<br>        x-=10<br>    return x<br>#apply new custom function<br>s.apply(custom_add_subtract_10)</pre><p><strong>Output:</strong></p><pre>0    20<br>1    30<br>2    20<br>3    30<br>4    40<br>dtype: int64</pre><p>This process will be irritating if I need to change the value multiple times. That’s where the <strong>args </strong>argument of<strong> Series.apply() </strong>method comes into the picture.</p><p>To use this parameter, I need to define one positional argument <strong>value </strong>in our custom function definition.</p><pre>#define a custom function with one positional argument<br>def custom_add_subtract(x,value):<br>    if x&lt;30:<br>        x+=value<br>    else:<br>        x-=value<br>    return x</pre><p>Now we can use the <strong>args</strong> argument of <strong>apply</strong> method for any custom value to add and subtract from the row. Let’s see how to do it.</p><pre>s.apply(custom_add_subtract,args=(4,))</pre><p>Here we are adding &amp; subtracting 4 from the rows.</p><p><strong>Output:</strong></p><pre>0    14<br>1    24<br>2    26<br>3    36<br>4    46<br>dtype: int64</pre><p>If I want to change the value to 6, I can do it easily. I just need to change the value in <strong>args </strong>argument.</p><pre>s.apply(custom_add_subtract,args=(6,))</pre><p><strong>Output:</strong></p><pre>0    16<br>1    26<br>2    24<br>3    34<br>4    44<br>dtype: int64</pre><p><strong>Note</strong>: make sure you add a <strong>comma(,) </strong>in <strong>args=(6,)</strong> after entering the value otherwise you may get an error.</p><pre>s.apply(custom_add_subtract,args=(6))<br><br><br><br>TypeError: custom_add_subtract() argument after * must be an iterable, not int</pre><p>We can use functions with more than one argument as well. Suppose I want to make threshold value to add and subtract to be dynamic then I need to introduce one new positional argument <strong>threshold </strong>in our function definition. After adding one additional argument, I can also use this with<strong> args.</strong></p><p>Let’s see how?</p><pre>#new custom function with two positional arguments<br><br>def custom_add_subtract(x,value,threshold):<br>    if x&lt;threshold:<br>        x+=value<br>    else:<br>        x-=value<br>    return x   </pre><p>Now I need to just change the value of <strong>value</strong> and <strong>threshold</strong> while calling apply() method.</p><pre>s.apply(custom_add_subtract, args=(3,15,))</pre><p>Now It will <strong>add 3</strong> if the row’s value is <strong>less than 15</strong> and <strong>subtract 3</strong> if the row’s value is <strong>greater than or equal to 15</strong>.</p><p><strong>Output:</strong></p><pre>0    13<br>1    17<br>2    27<br>3    37<br>4    47<br>dtype: int64</pre><p>if I pass the <strong>args=(7,35,) </strong>while calling the apply method. I will get the following result.</p><pre>s.apply(custom_add_subtract, args=(7,35,))</pre><p><strong>Output:</strong></p><pre>0    17<br>1    27<br>2    37<br>3    33<br>4    43<br>dtype: int64</pre><p>This way we can use the custom functions with positional arguments while using <strong>apply()</strong> method.</p><h3>3. Use of keyword arguments in Series.apply() method</h3><p>Now We will see how to use keyword arguments <strong>(**kwargs)</strong> with <strong>Series.apply()</strong> method.</p><p>Let’s understand this with an example. Suppose I want to introduce one additional keyword argument <strong>m, </strong>which stands for <strong>multiply factor</strong> in our custom function definition. When provided, It will multiply the final output with <strong>m</strong>, if not provided, then it will give the output as it is.</p><p>Now we need to change our custom function definition for the use of multiply factor.</p><pre># Define custom function with keyword arguments<br>def custom_add_subtract(x,value,threshold,**kwargs): <br>    if x&lt;threshold:<br>        x+=value<br>    else:<br>        x-=value<br>        <br>    if kwargs.get(&#39;m&#39;) is not None:<br>        return x*kwargs[&#39;m&#39;]<br>    return x</pre><p>If we pass <strong>m=2 </strong>after the <strong>args</strong> parameter, It will multiply the output by <strong>2.</strong></p><pre>#Value with Multiply factor m=2<br>s.apply(custom_add_subtract, args=(9,25),m=2)</pre><p><strong>Output:</strong></p><pre>0    38<br>1    58<br>2    42<br>3    62<br>4    82<br>dtype: int64</pre><p>If we do not pass any value, then It will return the output without multiplying.</p><pre>#Value without Multiply factor<br>s.apply(custom_add_subtract, args=(9,25))</pre><p><strong>Output:</strong></p><pre>0    19<br>1    29<br>2    21<br>3    31<br>4    41<br>dtype: int64</pre><p>That’s how we can use keyword arguments with <strong>apply()</strong> method.</p><p>With this, Pandas <strong>Series.apply() </strong>method use cases are completed. We have learned how to use <strong>apply()</strong> method with a function, then how to use a function with one or multiple positional arguments, and then how to use a function with keyword arguments. Now We will see how to use Pandas <strong>DataFrame.apply()</strong> method.</p><h3><strong>4. Difference between Series.apply() and DataFrame.apply() method</strong></h3><p>Before diving deep into DataFrame.apply() method. First, let’s see how <strong>DataFrame.apply()</strong> is different from <strong>Series.Apply()</strong></p><ol><li><strong>Input and output</strong>: Series.apply() operates on each element of the Series, while DataFrame.apply() can operate on either each column or row of the DataFrame. The output of Series.apply() is always a Series, while the output of DataFrame.apply() can be a Series (if applied to a single column), or a DataFrame (if applied to multiple columns).</li><li><strong>Function arguments</strong>: The function passed to Series.apply() takes a single value as input and returns a single value as output. In contrast, the function passed to DataFrame.apply() can take a single column or row as input, or a DataFrame with multiple columns or rows, and can return a single value, a Series, or a DataFrame.</li><li><strong>Function scope</strong>: Series.apply() allows you to define a function that operates only on the values of the Series, while DataFrame.apply() allows you to define a function that can operate on values of one or more columns or rows.</li><li><strong>Axis parameter</strong>: The axis parameter in Series.apply() is always set to 0, as there is only one axis in a Series. However, the axis parameter in DataFrame.apply() can be set to either 0 or 1, to apply the function across rows or columns, respectively</li></ol><h3>5. DataFrame.apply() method</h3><p>While <strong>Series.apply()</strong> method only works on each element of the Series, whereas <strong>DataFrame.apply()</strong> method can work on the whole DataFrame, one or multiple columns, and rows. Let’s see how?</p><p>First of all, let’s create one DataFrame to work with.</p><pre>import pandas as pd<br><br># Create a DataFrame<br>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]})<br><br>df</pre><p><strong>Output:</strong></p><pre>  A B C<br>0 1 4 7<br>1 2 5 8<br>2 3 6 9</pre><p>Suppose we want to apply a square root to each and every element of DataFrame. We can do this by following the command.</p><pre>df.apply(np.sqrt)</pre><p><strong>Output:</strong></p><pre><br>  A        B        C<br>0 1.000000 2.000000 2.645751<br>1 1.414214 2.236068 2.828427<br>2 1.732051 2.449490 3.000000</pre><p>If you want to apply some function to each column of DataFrame then you need to specify the<strong> axis=0</strong> or <strong>axis=’index’ </strong>in apply() method.</p><p>Suppose I want to calculate the sum of each column in the DataFrame, to do this, after passing the function <strong>np.sum</strong>, I need to pass<strong> axis=0</strong> while calling <strong>apply()</strong> method on DataFrame.</p><pre>#Calculate sum for each column<br>df.apply(np.sum, axis=0)<br><br>#df.apply(np.sum, axis=&#39;index&#39;)</pre><p><strong>Output:</strong></p><pre>A     6<br>B    15<br>C    24<br>dtype: int64</pre><p>here, we are calculating the sum for each column:</p><p>A: 1+2+3 =6</p><p>B: 4+5+6=15</p><p>C: 7+8+9=24</p><p>Similarly, If you want to apply some function to each row of DataFrame then you need to specify the<strong> axis=1</strong> or <strong>axis=’columns’ </strong>in apply() method.</p><p>Now, I want to calculate the sum of each row of the DataFrame. I can do this by passing the <strong>axis=1</strong> or <strong>axis=’columns’</strong> in apply() method.</p><pre>#calculate sum of each row<br>df.apply(np.sum, axis=1)<br><br>#df.apply(np.sum, axis=&#39;columns&#39;)</pre><p><strong>Output:</strong></p><pre>0    12<br>1    15<br>2    18<br>dtype: int64</pre><p>here, we are calculating the sum of each row:</p><p>0: 1+4+7=12</p><p>1: 2+5+8=15</p><p>2: 3+6+9=18</p><p>By using the parameter<strong> axis</strong>, we can specify to apply a function on a column or a row.</p><h3>6. Use of arguments in DataFrame.apply() method</h3><p>Now we will see how to use arguments while using DataFrame.apply() method.</p><p>Suppose I want to calculate the sum of two columns, but the column name can be changed dynamically. To do this, let’s define one function.</p><pre>#Sum of two column<br>def column_sum(row,col1,col2)<br>    return row[col1]+row[col2]</pre><p>This function will take the row of DataFrame as its first argument and the name of columns as its second and third arguments. Then It will return the sum of two columns as an output.</p><p>First I will calculate the sum of columns <strong>A</strong> and <strong>B</strong>. To do this, I need to first pass our custom function <strong>column_sum</strong>, then I need to pass <strong>args=(‘A’, ‘B’)</strong> while calling <strong>apply()</strong> method on DataFrame. Also, we need to apply this function on each row, so we need to pass<strong> axis=1</strong> as well.</p><pre># Apply the function to each row<br>df.apply(column_diff, axis=1, args=(&#39;A&#39;, &#39;B&#39;))</pre><p><strong>Output:</strong></p><pre>0    5<br>1    7<br>2    9<br>dtype: int64</pre><p>Similarly, for calculating the sum of columns B and C:</p><pre># Apply the function to each row<br>df.apply(column_diff, axis=1, args=(&#39;B&#39;, &#39;C&#39;))</pre><p><strong>Output:</strong></p><pre>0    11<br>1    13<br>2    15<br>dtype: int64<br></pre><h3>7. Conclusion</h3><p>In this article, we have learned how to use Series.apply() method and DataFrame.apply() method, the difference between these two and how to use both with arguments and keyword arguments.</p><p>If you want to apply some function to each value of a column then its good to use Series.apply() and if you want to apply some function that involves two or more rows and columns then it&#39;s better to use DataFrame.apply() method.</p><p>Thank you for reading. Open to suggestions and feedback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcdccf1210f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/use-pandas-apply-method-like-a-pro-bcdccf1210f">Use Pandas Apply Method Like a Pro!</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a chatbot with IBM Watson Assistant]]></title>
            <link>https://medium.com/analytics-vidhya/how-to-build-a-chatbot-with-ibm-watson-assistant-63e8b0a256a?source=rss-2d24315f2dd6------2</link>
            <guid isPermaLink="false">https://medium.com/p/63e8b0a256a</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[watson-assistant]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ibm-watson]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Harvinder Singh]]></dc:creator>
            <pubDate>Wed, 07 Jul 2021 13:07:11 GMT</pubDate>
            <atom:updated>2023-05-13T10:25:40.500Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we are going to learn about how to build a working chatbot using IBM Watson Assistant. Watson Assistant is a service hosted on the IBM cloud for building chatbots which allows you to build a chatbot without any explicit programming.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gW1iFdihToD8sujIgJPd1A.jpeg" /><figcaption>Conversational Chatbot</figcaption></figure><p>Before we start building a chatbot, Let’s understand what is a chatbot and its use case.</p><blockquote>A chatbot is an <strong>artificial intelligence (AI)</strong> software that can simulate human conversation. Chatbot services are programmed to respond to textual, visual, or auditory inputs from users and leverage mediums such as SMS, website chat window, and social messaging services to receive and reply to messages</blockquote><p>A chatbot can be used in various services such as FAQ answering, 24*7 customer care support, booking a ticket, taking a pizza order, etc.</p><p>We will build a chatbot for a restaurant that can handle the following task :</p><ol><li><strong>Make a booking</strong> — Our chatbot can book a table for the end-user.</li><li><strong>Hours of Opening</strong> — Our chatbot can provide information on the working hours of the restaurant.</li></ol><p>You can add as many features as you want, but for the sake of simplicity and better understanding, we will focus on the above two features only.</p><p>Now we have set our intentions on what kind of tasks our chatbot can do, Let’s start building it.</p><h3>Building the chatbot</h3><p>You need to have an IBM Cloud account to use the IBM Watson Assistant service. Sign in to your IBM Cloud account. In case you don’t have one, you can sign up for free <a href="https://cloud.ibm.com/registration">here</a>.</p><h3>Step 1: Creating an instance of Watson Assistant Service</h3><p>Once you have signed in to your IBM Cloud Account, the below screen will appear on the dashboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UHbMBtXZAoiKDfggmGnXGA.jpeg" /></figure><p>Type “ Watson Assistant” in the search bar and create a service of Watson Assistant.</p><p>Once you have created the service of Watson Assistant, click on it and the following screen will appear.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*caCRvnluVlMuA-bPaNiMFw.jpeg" /><figcaption>Click on “Launch Watson Assistant”</figcaption></figure><p>Now click on “Launch Watson Assistant” to launch the service.</p><h3>Step 2: Add a Dialog skill to the Assistant</h3><p>After completing the first step, you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cStfWkNSSPm-fuv7p1_pGw.jpeg" /></figure><p>In this step, we will add the Dialog skill to our assistant. Dialog skills will help our assistant to perform the mentioned task.</p><p>To add a dialog skill to the assistant, click on the “ My first assistant”. You will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNfDh8K1_p8g4uJjfaKzuQ.jpeg" /><figcaption>Action skill linked to “ My first assistant”</figcaption></figure><p>Here you can see, there is an actions skill linked to the assistant. In some cases, there is an action skill linked to the assistant and in others, there is a dialog skill linked to the assistant.</p><p>If there is a dialog skill linked to the assistant then we don’t need to change anything but in our case, there is an action skill linked to the assistant &amp; we want to link the dialog skill. To do so, first, we remove the action skill from the assistant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*egMvqeoUlneu-OQlUTGlkQ.jpeg" /><figcaption>Removing an actions skill</figcaption></figure><p>To remove an existing action skill, click on the three dots in the upper right corner of <em>My first skill </em>and click on the <em>remove skill</em>. After successfully removing the action skill you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mz4Vff7Vq7giwtn5XDP-iA.jpeg" /><figcaption>Add a dialog skill</figcaption></figure><p>Now to add a dialog skill, click on “Add an action or dialog skill” and Create a new dialog skill with the name “ Restaurant Skill” and click on the <em>Create skill.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aud5Ixf5s2B8ZMF9sbx3Vg.jpeg" /><figcaption>Creating a new Dialog skill.</figcaption></figure><p>After completing this, a new dialog skill is added to our assistant. This completes step 2 of building the chatbot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jyg-hxVymb4EzLXRWuZFXg.jpeg" /><figcaption>Assistant with the Dialog Skill.</figcaption></figure><h3>Step 3: Create an Intent and Add to the Skill</h3><p>In this step, we will create an Intent and add it to the skill. But before that, we need to understand what is an intent?</p><p>According to Watson Assistant</p><blockquote>An intent is a collection of user statements that have the same meaning. By creating intents, you train your assistant to understand the variety of ways users express a goal.</blockquote><p><strong>An intent</strong> is simply a collection of user statements that have the same meaning. These statements can be the statement from which users want to express a goal, In our case, something like <em>Make a booking</em> for the table.</p><p><strong>What are the different ways a user can say to make a booking for a table in the restaurant?</strong></p><p><em>Make a booking for a table.</em></p><p><em>Reserve a table for me.</em></p><p><em>Make a reservation for a table in the restaurant.</em></p><p><em>Book a table.</em></p><p><em>I want to book a table for dinner.</em></p><p>All these examples show the intention of the user to book a table. By looking at these examples, we can easily say that the user wants to book a table. The same goes for the<em> Intent</em> in the chatbot.</p><p>Intent helps the chatbot to identify the goals that users want to accomplish. These goals can be to make a booking, getting information about operating hours, etc.</p><p>In our chatbot, We want to accomplish two main goals: making a booking and getting information about hours of operation. So we will create two intents, one for each.</p><p>First, We create an intent to understand the user’s intention to make a booking for a table.</p><p>To create an intent, first, click on the <em>Restaurant skill</em>. You will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uoZW6GKB8lL3vEs8D1wsOA.jpeg" /><figcaption>Create an Intent</figcaption></figure><p>Now click on the <em>Create Intent. </em>After that, type the name of the intent “ make_booking” and then click on the create intent. You will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uWjIRUAq4jhy7oXen3ZXjA.jpeg" /><figcaption>#make_booking intent created</figcaption></figure><p>Now we need to train the intent by giving at least 5 different examples in which a user can say to a chatbot to make a booking for the table. The examples can be the following as we have seen earlier.</p><p><em>Make a booking for a table.</em></p><p><em>Reserve a table for me.</em></p><p><em>Make a reservation for a table in the restaurant.</em></p><p><em>Book a table.</em></p><p><em>I want to book a table for dinner.</em></p><p>To add examples, just type the statement and then click on <em>add example. </em>Once you successfully added all the examples, you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ioF1EsGSfozV5Tn3gGnE_A.jpeg" /><figcaption>5 different examples added to the intent</figcaption></figure><p>Similarly, Create another intent for the <em>hours of operation</em> with the following examples.</p><p><em>What are your hours of operation?</em></p><p><em>Are you open on weekend?</em></p><p><em>Are you open on Sundays?</em></p><p><em>What are your working hours?</em></p><p><em>What is the timing of your restaurant?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMdk3l06wOXGemY4Z9wzHA.jpeg" /><figcaption>intent #hours_of_operation created</figcaption></figure><p>Till now, we have created an instance of Watson assistant service, added a dialog skill to the assistant, and created two intents. Let’s move to the next step.</p><h3>Step 4: Create Entity and Add to the Skill</h3><p>In this step, we will create an <em>Entity</em> and add it to the skill. But before that, let’s understand what an <em>Entity</em> is and how it can be used to build a chatbot?</p><p>According to Watson Assistant…</p><blockquote><strong><em>Entities</em> represent information in the user input that is relevant to the user’s purpose</strong>.</blockquote><p>If <em>intents</em> represent verbs (the action a user wants to do), <em>entities</em> represent nouns (the object of, or the context for, that action). For example, when the <em>intent</em> is to get a weather forecast, the relevant location and date <em>entities</em> are required before the application can return an accurate forecast.</p><p>In our example, the user wants to book a table in the restaurant. But to book a table, the chatbot will need to know at what <strong>time </strong>and<strong> date </strong>the user want to book a table. Here <strong>date</strong> and <strong>time</strong> are the <em>entities</em> that are required to make a booking of the table.</p><p>So we will add two entities, namely, <strong>date</strong> and <strong>time</strong> to the dialog skill. Luckily, we don’t need to create these two entities. In Watson Assistant, these two entities are predefined in <em>System Entities. </em>We just need to enable these two entities to use.</p><p>To enable these two <em>entities, </em>click on the “Entities” in the “Restuarant skill”. After clicking, You will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*exotZbOeuW4paVZWjxIbmw.jpeg" /></figure><p>In the Entities section, you will see two options namely <strong>My Entities</strong> and <strong>System Entities.</strong> Just click on the <strong>System Entities</strong> and enable the<em> sys-date</em> and <em>sys-time</em> entities to use it in the chatbot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FqS7mt9e1IT5FShrX_Sy-g.jpeg" /><figcaption>Enabling sys-date and sys-time entities.</figcaption></figure><p>Now you have enabled the <strong>sys-date</strong> and <strong>sys-time</strong> entities. Now you can use them in building our chatbot.</p><h3>Step 5: Create the Dialog</h3><p>Now we will build the dialog to give the responses to user input. But before that, let’s understand what a <strong>Dialog</strong> is?</p><p>According to Watson Assistant…</p><blockquote>A <strong>Dialog</strong> defines the flow of your conversation in the form of a logic tree. It matches intents (what users say) to responses (what your chatbot says back). Each node of the tree has a condition that triggers it, based on user input.</blockquote><p>To build a dialog, click on the “dialog” in the skill menu and you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K3nimgvT0eBRyfFxeIq6cw.jpeg" /><figcaption>Dialog having Welcome and Anything else node</figcaption></figure><p>Here you can see two dialog nodes that are created automatically.</p><ol><li><strong>Welcome: </strong>This contains a greeting that is displayed to your users when they first engage with the assistant.</li><li><strong>Anything else</strong>: Contains phrases that are used to reply to users when their input is not recognized.</li></ol><p>Now click on the <strong>Welcome</strong> node and replace the text with the following written text in the Assistant Responds section.</p><p><em>Hello! I am the virtual assistant of the restaurant. How can I help you?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sNv0BrSCCI-P3oARD6WSuQ.jpeg" /><figcaption>Replace the text</figcaption></figure><p>Whenever the user engages with the chatbot, first they will get the above-written message. Now we will need two more dialog nodes, one to give information about hours of operation and one to make a booking.</p><p>We start with <em>hours of operation</em>. To add a new node, click on the three-dot in the upper right corner of the <strong>Welcome</strong> node and click on the <em>Add node below</em>. After clicking it you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BEQp9oc5tQEkDAvNJN4reA.jpeg" /></figure><p>Now fill in the following detail in the newly created node.</p><ol><li><strong>Enter node name(optional)</strong>: Operating Hours</li><li><strong>Enter condition</strong>: start typing <em>#hours_of_operation</em> and then click on <em>#hours_of_operation.</em></li><li><strong>Enter response text</strong>: <em>The timing of the restaurant is from 10 AM to 6 PM on weekdays and from 10 AM to 8 PM on weekends</em>.</li></ol><p>Now click on the <em>cross</em> icon next to <strong>C<em>ustomize</em></strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtRIDw4EFle_RdVI03pOHQ.jpeg" /><figcaption>Operating Hours node added to the skill</figcaption></figure><p>Now whenever a user asks about the working hours of the restaurant, the <strong>Operating Hours </strong>node will be triggered and the user gets the following response:</p><blockquote><strong><em>The timing of the restaurant is from 10 AM to 6 PM on weekdays and from 10 AM to 8 PM on weekends</em>.</strong></blockquote><p>Now you can try the chatbot that we have built now. Just click on <strong>Try it </strong>in the<strong> </strong>upper right corner and type the following sentence.</p><p><strong><em>What are the hours of operation?</em></strong></p><p>You will get the following response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yjgfjecA0RyAD54XLpvahA.jpeg" /><figcaption>Trying the Chatbot</figcaption></figure><p>Till now, we have added the dialog node to give the information about the hours of operation. Now we will add our last dialog node to <strong>make a booking</strong> for the table in the restaurant.</p><p>To add a new dialog node, just click on the three-dots in the <strong>Operating Hours</strong> node and click on the <em>Add node above.</em></p><p>You will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H-LiEF01PO2fkDOogKwmZw.jpeg" /><figcaption>Dialog node for make a booking</figcaption></figure><p>Now fill in the following details in the newly created dialog node.</p><ol><li><strong>Enter node name(optional)</strong>: Make Booking</li><li><strong>Enter condition</strong>:start typing <em>#make_booking</em> and then click on <em>#make_booking.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XAbsc9Mzp50mtWp_9pMoAg.jpeg" /><figcaption>Node after making change.</figcaption></figure><p>We have entered the <strong>#<em>make_booking</em></strong> intent as a condition to recognize so whenever a user asks about booking a table, the chatbot will recognize the intent <strong>#<em>make_booking </em></strong>and this node will be triggered.</p><p>To book a table in the restaurant, the chatbot needs to ask the user two things.</p><ol><li><strong>What day would you like to come in? </strong>( date of the booking)</li><li><strong>What time do you want for the reservation? </strong>( time of the booking)</li></ol><p>The typical workflow of booking a table will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kAvE8_aTs7Fwb3RJTQJ-0g.png" /><figcaption>Workflow of Booking a table through the chatbot</figcaption></figure><p>To create this workflow, we will use the <strong>Slots</strong>. Slots provide a structured format through which you can ask for and save multiple pieces of information from a user within a single node. They are most useful when you have a specific task in mind and need key pieces of information from the user before you can perform it.</p><p>To use the <strong>slots, </strong>click on<strong> <em>Customize</em></strong> button in the upper right corner of<em> </em><strong><em>Make Booking</em></strong><em> node</em>, set the Slots switch to On, and then click <em>Apply</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vjf2fvh57XwYGfPQQu8FeQ.jpeg" /><figcaption>set the Slots switch to On and click Apply</figcaption></figure><p>In <strong><em>Then check for </em></strong><em>Section, </em>Add the following slots :</p><ol><li><strong>check for </strong>— @sys-date, <strong>save as</strong> — $date, <strong>If not present, ask </strong>— What day would you like to come in?</li><li><strong>check for</strong> — @sys-time, <strong>save as</strong> — $time, <strong>If not present, ask </strong>— What time do you want for the reservation?</li></ol><p>After adding slots, you will get the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhXfflsaWL63-MynvhZNXw.jpeg" /><figcaption>Adding slots to the node.</figcaption></figure><p>Now, In the <strong>Assistant responds</strong> section, enter the following text.</p><p><em>Thank you for booking on $date. We will be waiting for you at $time.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qPU_ACOfXOt_22r5dcNPtw.jpeg" /><figcaption>Adding text response to the node.</figcaption></figure><p>The chatbot is complete now. You can try it using the <strong>Try it</strong> option in the upper right corner.</p><p>Hope you find this article useful. If you have any doubt, feel free to ask in the comment section. You can connect with me on <a href="https://www.linkedin.com/in/harvinder-singh-35a89619b/">LinkedIn</a>.</p><p>Thank You!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63e8b0a256a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/how-to-build-a-chatbot-with-ibm-watson-assistant-63e8b0a256a">How to build a chatbot with IBM Watson Assistant</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>