Skip to content

Commit

Permalink
Improved 'TestServiceLocally' class to offer more options for quick-t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
fquirin committed Oct 23, 2020
1 parent 6433b2a commit 3a6df66
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 5 deletions.
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
@@ -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 3a6df66

Please sign in to comment.