Skip to content

Commit

Permalink
Merge pull request #18 from SEPIA-Framework/dev
Browse files Browse the repository at this point in the history
Update to support SEPIA-Home v2.5.1
  • Loading branch information
fquirin authored Oct 23, 2020
2 parents a97b779 + 3a6df66 commit 96e0107
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 26 deletions.
Binary file removed libs/sepia-assist-v2.5.0-javadoc.jar
Binary file not shown.
Binary file added libs/sepia-assist-v2.5.1-javadoc.jar
Binary file not shown.
Binary file not shown.
Binary file removed libs/sepia-chat-v1.3.0-javadoc.jar
Binary file not shown.
Binary file removed libs/sepia-chat-v1.3.0.jar
Binary file not shown.
Binary file added libs/sepia-chat-v1.3.1-javadoc.jar
Binary file not shown.
Binary file added libs/sepia-chat-v1.3.1.jar
Binary file not shown.
Binary file removed libs/sepia-core-tools-v2.2.6-javadoc.jar
Binary file not shown.
Binary file added libs/sepia-core-tools-v2.2.7-javadoc.jar
Binary file not shown.
Binary file not shown.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>net.b07z.sepia.sdk</groupId>
<artifactId>sepia-sdk-java</artifactId>
<version>0.9.22</version>
<version>0.9.23</version>
<name>SEPIA SDK</name>
<description>Tools to develop services for the SEPIA framework</description>
<url>https://sepia-framework.github.io</url>
Expand All @@ -29,11 +29,11 @@
<dependency>
<groupId>net.b07z.sepia.server.assist</groupId>
<artifactId>sepia-assist-API</artifactId>
<version>2.5.0</version>
<version>2.5.1</version>
</dependency>
-->

<!-- ... THIS IS WHAT WE USE FOR NOW -->
<!-- ... THIS IS WHAT WE USE FOR NOW -->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
Expand Down Expand Up @@ -97,9 +97,9 @@
<dependency>
<groupId>net.b07z.sepia.server.core</groupId>
<artifactId>sepia-core-tools</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>
<scope>system</scope>
<systemPath>${basedir}/libs/sepia-core-tools-v2.2.6.jar</systemPath>
<systemPath>${basedir}/libs/sepia-core-tools-v2.2.7.jar</systemPath>
</dependency>

<dependency>
Expand All @@ -115,9 +115,9 @@
<dependency>
<groupId>net.b07z.sepia.websockets</groupId>
<artifactId>sepia-websockets</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<scope>system</scope>
<systemPath>${basedir}/libs/sepia-chat-v1.3.0.jar</systemPath>
<systemPath>${basedir}/libs/sepia-chat-v1.3.1.jar</systemPath>
</dependency>

<dependency>
Expand All @@ -138,9 +138,9 @@
<dependency>
<groupId>net.b07z.sepia.server.assist</groupId>
<artifactId>sepia-assist-API</artifactId>
<version>2.5.0</version>
<version>2.5.1</version>
<scope>system</scope>
<systemPath>${basedir}/libs/sepia-assist-v2.5.0.jar</systemPath>
<systemPath>${basedir}/libs/sepia-assist-v2.5.1.jar</systemPath>
</dependency>
<!-- -->

Expand All @@ -150,7 +150,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
106 changes: 101 additions & 5 deletions src/main/java/net/b07z/sepia/sdk/main/TestServiceLocally.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package net.b07z.sepia.sdk.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.b07z.sepia.sdk.services.uid1007.RestaurantDemo;
import net.b07z.sepia.sdk.substitutes.AnswerLoaderEmpty;
import net.b07z.sepia.server.assist.answers.AnswerLoaderFile;
import net.b07z.sepia.server.assist.answers.DefaultReplies;
import net.b07z.sepia.server.assist.assistant.LANGUAGES;
import net.b07z.sepia.server.assist.data.Parameter;
import net.b07z.sepia.server.assist.interpreters.NluInput;
import net.b07z.sepia.server.assist.interpreters.NluKeywordAnalyzer;
import net.b07z.sepia.server.assist.interpreters.NluResult;
import net.b07z.sepia.server.assist.interpreters.Normalizer;
import net.b07z.sepia.server.assist.interviews.AbstractInterview;
import net.b07z.sepia.server.assist.interviews.Interview;
import net.b07z.sepia.server.assist.interviews.InterviewInterface;
import net.b07z.sepia.server.assist.interviews.InterviewResult;
import net.b07z.sepia.server.assist.interviews.InterviewServicesMap;
import net.b07z.sepia.server.assist.parameters.ParameterConfig;
import net.b07z.sepia.server.assist.parameters.ParameterHandler;
import net.b07z.sepia.server.assist.server.Config;
import net.b07z.sepia.server.assist.server.ConfigTestServer;
import net.b07z.sepia.server.assist.services.ServiceInterface;
import net.b07z.sepia.server.assist.services.ServiceResult;
import net.b07z.sepia.server.core.assistant.PARAMETERS;
import net.b07z.sepia.server.core.tools.JSONWriter;

/**
* Demonstrates how to test a service locally before using the upload function.
* Demonstrates how to test a service locally before using the upload function and offers methods to help with testing.
*
* @author Florian Quirin
*
Expand All @@ -41,25 +53,59 @@ public static void main(String[] args) {
ServiceInterface service = new RestaurantDemo();
String lang = LANGUAGES.EN;

//Run
//Get a result using the regular-expressions defined inside custom service
setup();
NluResult result = testServiceWithRegExpTrigger(service, text, lang);

//Print result
log.info(JSONWriter.getPrettyString(result.getBestResultJSON()));

//Build a parameter - result is added to given NluResult
/*
Interview interviewApi = new Interview(result);
Parameter p1 = buildParameter(interviewApi, PARAMETERS.NUMBER); //global name or ...
Parameter p2 = buildParameter(interviewApi, PARAMETERS.TIME);
Parameter p3 = buildParameter(interviewApi, new RestaurantDemo.ReservationName()); //... parameter handler
//Print individual results
log.info(JSONWriter.getPrettyString(p1.getData()));
log.info(JSONWriter.getPrettyString(p2.getData()));
log.info(JSONWriter.getPrettyString(p3.getData()));
*/

//Get service result from NLU result
ServiceResult sr = getServiceResultFromNluResult(service, result);

//Print result
log.info(JSONWriter.getPrettyString(sr.getResultJSONObject()));
}

/**
* Setup some components of SEPIA.
* Setup some default components of SEPIA.
*/
public static void setup() {
setup(false);
}
/**
* Setup some default components of SEPIA and include the answers-file loader.<br>
* NOTE: It will try to load answer files from "answers/" folder (may be useful if you want to include your custom files).
*/
public static void setup(boolean includeAnswersFromFile){
//setup answers
//Config.setAnswerModule(new AnswerLoaderFile()); //choose txt-file answers-module
//DefaultReplies.setupDefaults(); //setup default question mapping for parameters and stuff
if (includeAnswersFromFile){
Config.answersPath = "answers/"; //new folder to copy answer files to
Config.setAnswerModule(new AnswerLoaderFile()); //choose txt-file answers-module
}else{
Config.setAnswerModule(new AnswerLoaderEmpty());
}
DefaultReplies.setupDefaults(); //setup default question mapping for parameters and stuff

//setup commands and parameters
InterviewServicesMap.load(); //services connected to interviews
ParameterConfig.setup(); //connect parameter names to handlers and other stuff

//reduce DB access for default test user (we are not running a DB in this test-mode)
ConfigTestServer.reduceDatabaseAccess(ConfigTestServer.getFakeUserId(null));
}

/**
Expand Down Expand Up @@ -95,7 +141,10 @@ public static NluResult testServiceWithRegExpTrigger(ServiceInterface service, S

int bestScoreIndex = 0; //for the test we assume that index 0 is our goal
log.info("Primary score: " + possibleScore.get(bestScoreIndex));

NluResult result = new NluResult(possibleCMDs, possibleParameters, possibleScore, bestScoreIndex);
result.input = input; //TODO: is this enough for simple testing?

return result;
}

Expand All @@ -108,4 +157,51 @@ private static String normalize(String text, String languageCode) {
return normText;
}

/**
* Very basic method to execute parameter 'build' process.
* @param interview - {@link Interview} object initialized with given {@NluResult}
* @param parameterName - name of a parameter, usually defined in {@link PARAMETERS}
*/
public static Parameter buildParameter(Interview interview, String parameterName){
Parameter p = new Parameter(parameterName);
return buildParameter(interview, p);
}
/**
* Very basic method to execute parameter 'build' process.
* @param interview - {@link Interview} object initialized with given {@NluResult}
* @param parameterHandler - {@link ParameterHandler} e.g. your parameter from a custom service
*/
public static Parameter buildParameter(Interview interview, ParameterHandler parameterHandler){
Parameter p = new Parameter(parameterHandler);
return buildParameter(interview, p);
}
/**
* Very basic method to execute parameter 'build' process.
*/
private static Parameter buildParameter(Interview interview, Parameter p){
interview.getParameterInput(p);
interview.buildParameterOrComment(p, null);
return interview.nluResult.getOptionalParameter(p.getName(), null);
}

/**
* Build a service result for a given {@link ServiceInterface} from previously created {@link NluResult}.<br>
* NOTE: Depending on how your answers-module is set up (default is empty) you will only get the answer-keys not the "real" answer.
* @param service - your custom service, e.g. "new RestaurantDemo()"
* @param nluResult - previously created {@link NluResult}, e.g. via {@link #testServiceWithRegExpTrigger}
* @return
*/
public static ServiceResult getServiceResultFromNluResult(ServiceInterface service, NluResult nluResult){
//ServiceResult answer;
List<ServiceInterface> services = Arrays.asList(service);
InterviewInterface interview = new AbstractInterview();
interview.setCommand(nluResult.getCommand());
interview.setServices(services);
InterviewResult iResult = interview.getMissingParameters(nluResult);
if (iResult.isComplete()){
return interview.getServiceResults(iResult);
}else{
return iResult.getApiComment();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ public ServiceAnswers getAnswersPool(String language) {
//Build German answers
if (language.equals(LANGUAGES.DE)){
answerPool
.addAnswer(successAnswer, 0, "Hier sind die aktuellen, weltweiten Corona Zahlen des ECDC. "
+ "Fälle insgesamt: <2>. Tote insgesamt: <3>.")
.addAnswer(successWithCountry, 0, "Hier sind die aktuellen Corona Zahlen des ECDC für <1>. "
+ "Fälle insgesamt: <2>. Tote insgesamt: <3>. Neue Fälle: <4>.")
.addAnswer(successAnswer, 0, "Hier sind die aktuellen, weltweiten Corona Zahlen des ECDC. "
+ "Fälle insgesamt: <2>. Tote insgesamt: <3>.")
.addAnswer(successWithCountry, 0, "Hier sind die aktuellen Corona Zahlen des ECDC für <1>. "
+ "Fälle insgesamt: <2>. Tote insgesamt: <3>. Neue Fälle: <4>.")
.addAnswer(okAnswer, 0, "Die Anfrage ist angekommen aber ich kann sie nicht bearbeiten.")
;
return answerPool;

//Or default to English
}else{
answerPool
.addAnswer(successAnswer, 0, "Here are the recent, worldwide Corona numbers from the ECDC. "
+ "Total cases: <2>. Total deaths: <3>.")
.addAnswer(successWithCountry, 0, "Here are the recent ECDC Corona numbers for <1>. "
+ "Total cases: <2>. Total deaths: <3>. New cases: <4>.")
.addAnswer(successAnswer, 0, "Here are the recent, worldwide Corona numbers from the ECDC. "
+ "Total cases: <2>. Total deaths: <3>.")
.addAnswer(successWithCountry, 0, "Here are the recent ECDC Corona numbers for <1>. "
+ "Total cases: <2>. Total deaths: <3>. New cases: <4>.")
.addAnswer(okAnswer, 0, "Message received but I could not fulfill your request.")
;
return answerPool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public ServiceResult getResult(NluResult nluResult) {
ServiceBuilder service = new ServiceBuilder(nluResult);
service.answer = Answers.getAnswerString(nluResult, followUpFinish);
service.status = "success";
/*boolean wasSent =*/ service.sendFollowUpMessage(nluResult.input, service.buildResult());
/*boolean wasSent =*/ service.sendFollowUpMessage(service.buildResult());
return;
}).getJson());
//Interval minutes
Expand All @@ -220,7 +220,7 @@ public ServiceResult getResult(NluResult nluResult) {
ServiceBuilder service = new ServiceBuilder(nluResult);
service.answer = Answers.getAnswerString(nluResult, followUpIntervalMinutes, halfTimeMinutes);
service.status = "success";
/*boolean wasSent =*/ service.sendFollowUpMessage(nluResult.input, service.buildResult());
/*boolean wasSent =*/ service.sendFollowUpMessage(service.buildResult());
return;
}).getJson());
}
Expand All @@ -231,7 +231,7 @@ public ServiceResult getResult(NluResult nluResult) {
ServiceBuilder service = new ServiceBuilder(nluResult);
service.answer = Answers.getAnswerString(nluResult, followUpIntervalSeconds, seconds);
service.status = "success";
/*boolean wasSent =*/ service.sendFollowUpMessage(nluResult.input, service.buildResult());
/*boolean wasSent =*/ service.sendFollowUpMessage(service.buildResult());
return;
}).getJson());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.b07z.sepia.sdk.substitutes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.b07z.sepia.server.assist.answers.AnswerLoader;
import net.b07z.sepia.server.assist.interpreters.NluResult;
import net.b07z.sepia.server.core.data.Answer;

/**
* Substitution for a proper {@link AnswerLoader} that simply returns the answer key (or direct answer).
*/
public class AnswerLoaderEmpty implements AnswerLoader {

//stores the references to all answers in different languages
Map<String, Map<String, List<Answer>>> answers = new HashMap<>();

@Override
public void setupAnswers(){}

@Override
public void updateAnswerPool(Map<String, Map<String, List<Answer>>> answersPool){
this.answers = answersPool;
}

@Override
public String getAnswer(NluResult nluResult, String key){
return key;
}
@Override
public String getAnswer(NluResult nluResult, String key, Object... wildcards){
return key;
}
@Override
public String getAnswer(Map<String, List<Answer>> memory, NluResult nluResult, String key){
return key;
}
@Override
public String getAnswer(Map<String, List<Answer>> memory, NluResult nluResult, String key, Object... wildcards){
return key;
}
}

0 comments on commit 96e0107

Please sign in to comment.