Skip to content

Commit d119a96

Browse files
committed
Changed custom answer handling and implemented in RestaurantDemo
1 parent 2c4996b commit d119a96

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

src/main/java/net/b07z/sepia/sdk/main/MyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void main(String[] args) {
2121
//Define class, language and sentence (optional)
2222
ServiceInterface service = new RestaurantDemo();
2323
Language language = Language.EN;
24-
String testSentence = "reserve a table for 2"; //we use the ones included in the service
24+
String testSentence = null; //"reserve a table for 2"; //we use the ones included in the service
2525

2626
//Upload
2727
log.info("");

src/main/java/net/b07z/sepia/sdk/services/uid1007/HelloWorld.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import java.util.TreeSet;
44

5+
import org.json.simple.JSONObject;
6+
7+
import net.b07z.sepia.server.assist.data.Card;
8+
import net.b07z.sepia.server.assist.data.Card.ElementType;
59
import net.b07z.sepia.server.assist.interpreters.NluResult;
610
import net.b07z.sepia.server.assist.services.ServiceBuilder;
711
import net.b07z.sepia.server.assist.services.ServiceInfo;
@@ -11,10 +15,12 @@
1115
import net.b07z.sepia.server.assist.services.ServiceInfo.Type;
1216
import net.b07z.sepia.server.core.assistant.ACTIONS;
1317
import net.b07z.sepia.server.core.data.Language;
18+
import net.b07z.sepia.server.core.tools.JSON;
1419
import net.b07z.sepia.server.core.tools.Sdk;
1520

1621
/**
17-
* "Hello World" custom service that just returns a "Hello" answer and a button that links to the SDK.
22+
* "Hello World" custom service that just returns a "Hello" answer,
23+
* a demo button that links to the SDK and a demo card that links to SEPIA website.
1824
*
1925
* @author Florian Quirin
2026
*
@@ -80,7 +86,8 @@ public ServiceInfo getInfo(String language) {
8086
@Override
8187
public ServiceResult getResult(NluResult nluResult) {
8288
//initialize result
83-
ServiceBuilder api = new ServiceBuilder(nluResult, getInfo(nluResult.language));
89+
ServiceBuilder api = new ServiceBuilder(nluResult,
90+
getInfoFreshOrCache(nluResult.input, this.getClass().getCanonicalName()));
8491

8592
//get required parameters
8693
//NONE
@@ -94,6 +101,19 @@ public ServiceResult getResult(NluResult nluResult) {
94101
api.putActionInfo("url", "https://github.com/SEPIA-Framework/sepia-sdk-java");
95102
api.putActionInfo("title", "SDK info");
96103

104+
//... and we also add a demo card
105+
Card card = new Card(Card.TYPE_SINGLE);
106+
JSONObject linkCard = card.addElement(
107+
ElementType.link,
108+
JSON.make("title", "S.E.P.I.A." + ":", "desc", "Hello World!"),
109+
null, null, "",
110+
"https://sepia-framework.github.io/",
111+
"https://sepia-framework.github.io/img/icon.png",
112+
null, null
113+
);
114+
JSON.put(linkCard, "imageBackground", "#000"); //more options like CSS background
115+
api.addCard(card.getJSON());
116+
97117
//all good
98118
api.setStatusSuccess();
99119

src/main/java/net/b07z/sepia/sdk/services/uid1007/RestaurantDemo.java

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.json.simple.JSONObject;
66

7+
import net.b07z.sepia.server.assist.answers.ServiceAnswers;
78
import net.b07z.sepia.server.assist.assistant.LANGUAGES;
89
import net.b07z.sepia.server.assist.data.Parameter;
910
import net.b07z.sepia.server.assist.interpreters.NluResult;
@@ -22,6 +23,7 @@
2223
import net.b07z.sepia.server.assist.services.ServiceInfo.Content;
2324
import net.b07z.sepia.server.assist.services.ServiceInfo.Type;
2425
import net.b07z.sepia.server.core.assistant.PARAMETERS;
26+
import net.b07z.sepia.server.core.data.Answer;
2527
import net.b07z.sepia.server.core.data.Language;
2628
import net.b07z.sepia.server.core.tools.Debugger;
2729
import net.b07z.sepia.server.core.tools.JSON;
@@ -33,28 +35,15 @@
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

Comments
 (0)