Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simple Sample of the Watson Dialog Service in Bluemix

DZone's Guide to

Simple Sample of the Watson Dialog Service in Bluemix

Developers can use IBM Watson's Dialog service to build apps with natural language processing capable of answering user questions. Check out this fun example.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

With the IBM Watson Dialog service on Bluemix developers can build applications using natural language to automatically respond to user questions, for example functionality to help users to reset their passwords. Below is a simple sample demonstrating how this service works.

Via the dialog users can enter a topic they are interested in to see recent tweets with a positive or negative sentiment. The tweets are received via the Insights for Twitter service.

dialogsimple

The dialog is defined via XML. There is a good tutorial describing how this works. In my sample I use two variables to store the input values from users. Via concepts you can also define synonyms and you can use wildcards to react to different types of user input.

<?xml version="1.0" encoding="UTF-8"?>
<dialog xsi:noNamespaceSchemaLocation="WatsonDialogDocument_1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <flow>
        <folder label="Main">
            <output>
                <prompt selectionType="RANDOM">
                    <item>Hi, I'll show you the latest buzz around a topic of your choice. What topic are you interested in?</item>
                </prompt>
                <goto ref="getUserInput_2442994"/>
            </output>
            <output>
                <prompt selectionType="RANDOM">
                    <item>Bye</item>
                </prompt>
                <getUserInput id="getUserInput_2442994">
                    <search ref="folder_2442998"/>
                </getUserInput>
            </output>
        </folder>
        <folder label="Library">
            <folder label="Live Content" id="folder_2447777">
                <output>
                    <prompt selectionType="RANDOM">
                        <item>Alright. Open this URL to see the tweets: http://insights-search.mybluemix.net/api/1/messages/search?q={Topic}%20AND%20posted%3A2015-07-01%20AND%20sentiment%3A{Sentiment}</item>
                    </prompt>
                </output>
            </folder>
            <folder label="Live Content" id="folder_2442998">
                <input>
                    <grammar>
                        <item>*</item>
                    </grammar>
                    <action varName="Topic" operator="SET_TO_USER_INPUT"/>
                    <output>
                        <prompt selectionType="SEQUENTIAL">
                            <item>Are you interested in positive or negative tweets?</item>
                        </prompt>
                            <getUserInput>
                                <input>
                                    <grammar>
                                        <item>positive</item>
                                    </grammar>
                                    <action varName="Sentiment" operator="SET_TO">positive</action>
                                    <goto ref="folder_2447777"/>
                                </input>
                                <input>
                                    <grammar>
                                        <item>negative</item>
                                    </grammar>
                                    <action varName="Sentiment" operator="SET_TO">negative</action>
                                    <goto ref="folder_2447777"/>
                                </input>
                                <input>
                                    <grammar>
                                        <item>*</item>
                                    </grammar>
                                    <action varName="Sentiment" operator="SET_TO">nothing</action>
                                    <goto ref="folder_2442998"/>
                                </input>
                            </getUserInput>
                    </output>
                </input>
            </folder>
            <folder label="Storage"/>
        </folder>
        <folder label="Global"/>
        <folder label="Concepts">
            <concept>
                <grammar>
                    <item>positive</item>
                    <item>good</item>
                </grammar>
            </concept>
        </folder>
    </flow>
    <entities>
    </entities>
    <constants>
    </constants>
    <variables>
        <var_folder name="Home">
            <var name="Topic" type="TEXT"/>
            <var name="Sentiment" type="TEXT"/>
        </var_folder>
    </variables>
    <settings>
    </settings>
    <specialSettings>
    </specialSettings>
</dialog>

In order to test your dialog there is a dialog tool that can be run either locally or deployed to Bluemix. The tool helps testing the dialog and it also discovers syntax errors. To deploy your dialog you can use the Java or Node.js sample applications.

To find out more read the documentation, the API documentation or try the online pizza ordering sample.

In the simple sample I only display the URL that shows the tweets as JSON. There is a more advanced sample that finds certain movies based on user input. The movies are queried via a REST API provided by themoviedb.org. In order to do this the dialog service returns JSON with all filters to a Java application.

<prompt selectionType="RANDOM">
   <item>"{Search_Now:"{Search_Now}", Recency:"{Recency_Preference}", Rating:"{Certification_Preference}", Genre:"{Genre_Preference}", Index:"{Current_Index}", Page:"{Page}"}"</item>
</prompt>

When the dialog service returns this JSON to the Java application, the movies are queried and returned to the user.

Conversation conversation = dialogService.converse(converseParams);
wdsMessage = StringUtils.join(conversation.getResponse(), " ");
processedText = matchSearchNowPattern(wdsMessage);
...
SearchTheMovieDbProxyResource tmdb = new SearchTheMovieDbProxyResource();
conversationPayload = tmdb.discoverMovies(UtilityFunctions.getPropValue(paramsObj, "Genre"),
   UtilityFunctions.getPropValue(paramsObj, "Rating"),
   UtilityFunctions.getPropValue(paramsObj, "Recency"),
   currentIndex, pageNum, nextSearch || newSearch);

The post Simple Sample of the Watson Dialog Service in Bluemix appeared first onNiklas Heidloff.

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
virtual assistant ,dialogs ,ibm bluemix

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}