heidloff.net - Building is my Passion
Post
Cancel

Simple Sample of the Watson Dialog Service in Bluemix

Update November 2016: The dialog service has been deprecated and replaced with the Conversation service.

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.

image

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?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.

1
2
3
<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.

1
2
3
4
5
6
7
8
9
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);
Featured Blog Posts
Disclaimer
The postings on this site are my own and don’t necessarily represent IBM’s positions, strategies or opinions.
Trending Tags