44
55import org .json .simple .JSONObject ;
66
7+ import net .b07z .sepia .server .assist .answers .ServiceAnswers ;
78import net .b07z .sepia .server .assist .assistant .LANGUAGES ;
89import net .b07z .sepia .server .assist .data .Parameter ;
910import net .b07z .sepia .server .assist .interpreters .NluResult ;
2223import net .b07z .sepia .server .assist .services .ServiceInfo .Content ;
2324import net .b07z .sepia .server .assist .services .ServiceInfo .Type ;
2425import net .b07z .sepia .server .core .assistant .PARAMETERS ;
26+ import net .b07z .sepia .server .core .data .Answer ;
2527import net .b07z .sepia .server .core .data .Language ;
2628import net .b07z .sepia .server .core .tools .Debugger ;
2729import net .b07z .sepia .server .core .tools .JSON ;
3335 * @author Florian Quirin
3436 *
3537 */
36- public class RestaurantDemo implements ServiceInterface {
38+ public class RestaurantDemo implements ServiceInterface {
3739
38- //Command name
39- private static final String CMD_NAME = "restaurant_reservation" ; //Name tag of your service (will be combined with userId to be unique)
40-
41- //Answers and questions used
42- //NOTE: Since we only support English in this service and it's a demo we use <direct> sentences instead of links to the database.
43- // This is okay for development but has some disadvantages like there will be no variation on questions after a repeat
44- // ("sorry?" -> "sorry once more plz?" ...).
45- private static final String successAnswer = "<direct>Ok, I've reserved a table for the <1> for <2> people on the name <3>. "
46- + "Thank you and have a great meal." ;
47- private static final String okAnswer = "<direct>Sorry I could not reserve a table. Please try again later." ;
48- private static final String failAnswer = "error_0a" ;
49- private static final String askTimeAndDate = "<direct>When would you like to visit Luigis?" ;
50- private static final String askTimeAndDateMoreSpecific = "<direct>Sorry can you tell me the date and time of your visit again? I think I missed one of them." ;
51- private static final String askNumberOfPersons = "<direct>How many people will come?" ;
52- private static final String askReservationName = "<direct>On what name should I reserve the table?" ;
40+ //Command name of your service (will be combined with userId to be unique)
41+ private static final String CMD_NAME = "restaurant_reservation" ;
5342
5443 //Define some sentences for testing:
5544
5645 @ Override
57- public TreeSet <String > getSampleSentences (String lang ){
46+ public TreeSet <String > getSampleSentences (String lang ) {
5847 TreeSet <String > samples = new TreeSet <>();
5948 //GERMAN
6049 if (lang .equals (Language .DE .toValue ())){
@@ -67,6 +56,60 @@ public TreeSet<String> getSampleSentences(String lang){
6756 }
6857
6958 //Basic service setup:
59+
60+ //Overriding the 'getAnswersPool' methods enables you to define custom answers with more complex features.
61+ //You can build a pool of answers that can have multiple versions of the same answer used for different
62+ //situations like a repeated question (what was the time? -> sorry say again, what was the time? -> ...).
63+
64+ @ Override
65+ public ServiceAnswers getAnswersPool (String language ) {
66+ ServiceAnswers answerPool = new ServiceAnswers (language );
67+
68+ //Build English answers
69+ if (language .equals (LANGUAGES .EN )){
70+ answerPool
71+ .addAnswer (successAnswer , 0 , "Ok, I've reserved a table for the <1> for <2> people on the name <3>. "
72+ + "Thank you and have a great meal." )
73+ //example of how to use the 'shortcut' to add an answer
74+
75+ .addAnswer (okAnswer , 0 , "Sorry I could not reserve a table. Please try again later." )
76+
77+ .addAnswer (askTimeAndDate , 0 , "When would you like to visit Luigis?" )
78+ .addAnswer (askTimeAndDate , 1 , "Sorry say again please. At what day and time would you like to visit Luigis?" )
79+ .addAnswer (askTimeAndDate , 2 , "Sorry I still didn't get it. What is the time and date of your visit?" )
80+
81+ .addAnswer (askTimeAndDateMoreSpecific , 0 , "Sorry can you tell me the date and time of your visit again? "
82+ + "I think I missed one of them." )
83+ .addAnswer (askTimeAndDateMoreSpecific , 1 , "Sorry say again please?" )
84+ .addAnswer (askTimeAndDateMoreSpecific , 2 , "Sorry it looks like I've troubles understanding the date and time. "
85+ + "Try once more please." )
86+
87+ .addAnswer (askNumberOfPersons , 0 , "How many people will come?" )
88+ .addAnswer (askNumberOfPersons , 1 , "Sorry, how many people did you say will come?" )
89+ .addAnswer (askNumberOfPersons , 2 , "Sorry but I still did not get it. What is the number of people?" )
90+
91+ .addAnswer (askReservationName , 0 , "On what name should I reserve the table?" )
92+ .addAnswer (askReservationName , 1 , "Sorry can you give me the name again please?" )
93+ .addAnswer (new Answer (Language .from (language ), askReservationName , "Sorry I did not understant the name for "
94+ + "your reservation. Can you tell me once more please?" ,
95+ Answer .Character .neutral , 2 , 5 ))
96+ //example of how to use the 'complete' answer object
97+ ;
98+ return answerPool ;
99+
100+ //Other languages not yet supported
101+ }else {
102+ return null ;
103+ }
104+ }
105+ //We keep a reference here for easy access in getResult - Note that custom answers need to start with a certain prefix
106+ private static final String failAnswer = "error_0a" ;
107+ private static final String successAnswer = ServiceAnswers .ANS_PREFIX + "restaurant_success_0a" ;
108+ private static final String okAnswer = ServiceAnswers .ANS_PREFIX + "restaurant_still_ok_0a" ;
109+ private static final String askTimeAndDate = ServiceAnswers .ANS_PREFIX + "restaurant_ask_time_date_1a" ;
110+ private static final String askTimeAndDateMoreSpecific = ServiceAnswers .ANS_PREFIX + "restaurant_ask_time_date_1b" ;
111+ private static final String askNumberOfPersons = ServiceAnswers .ANS_PREFIX + "restaurant_ask_num_persons_2a" ;
112+ private static final String askReservationName = ServiceAnswers .ANS_PREFIX + "restaurant_ask_res_name_3a" ;
70113
71114 @ Override
72115 public ServiceInfo getInfo (String language ) {
@@ -99,7 +142,8 @@ public ServiceInfo getInfo(String language) {
99142 //The first 2 are already available by default:
100143 Parameter p1 = new Parameter (PARAMETERS .TIME )
101144 .setRequired (true )
102- .setQuestion (askTimeAndDate ); //NOTE: if you are looking for TIME and NUMBER use this parameter first to exclude dates in NUMBER
145+ .setQuestion (askTimeAndDate );
146+ //NOTE: if you are looking for TIME and NUMBER use this parameter first to exclude dates in NUMBER
103147 Parameter p2 = new Parameter (PARAMETERS .NUMBER )
104148 .setRequired (true )
105149 .setQuestion (askNumberOfPersons );
@@ -111,10 +155,12 @@ public ServiceInfo getInfo(String language) {
111155
112156 info .addParameter (p1 ).addParameter (p2 ).addParameter (p3 );
113157
114- //Answers (these are the default answers, you can trigger a custom answer at any point in the module with api.setCustomAnswer(..)):
158+ //Answers (these are the default answers, you can trigger a custom answer at any point in the module
159+ //with serviceBuilder.setCustomAnswer(..)):
115160 info .addSuccessAnswer (successAnswer )
116161 .addFailAnswer (failAnswer )
117162 .addOkayAnswer (okAnswer );
163+ //.addCustomAnswer("askTimeAndDate", askTimeAndDate); //optional, just for info
118164
119165 //Add answer parameters that are used to replace <1>, <2>, ... in your answers.
120166 //The name is arbitrary but you need to use the same one in getResult(...) later for api.resultInfoPut(...)
@@ -126,7 +172,9 @@ public ServiceInfo getInfo(String language) {
126172 @ Override
127173 public ServiceResult getResult (NluResult nluResult ) {
128174 //initialize result
129- ServiceBuilder api = new ServiceBuilder (nluResult , getInfo (nluResult .language ));
175+ ServiceBuilder api = new ServiceBuilder (nluResult ,
176+ getInfoFreshOrCache (nluResult .input , this .getClass ().getCanonicalName ()),
177+ getAnswersPool (nluResult .language ));
130178
131179 //get required parameters:
132180
@@ -231,8 +279,11 @@ public String responseTweaker(String input){
231279
232280 @ Override
233281 public String build (String input ){
282+ //anything extracted?
283+ if (input .isEmpty ()){
284+ return "" ;
234285 //any errors?
235- if (input .equals ("<user_data_unresolved>" )){
286+ } else if (input .equals ("<user_data_unresolved>" )){
236287 this .buildSuccess = false ;
237288 return "" ; //TODO: this probably should become something like 'Interview.ERROR_USER_DATA_ACCESS' in the future;
238289 }else {
0 commit comments