Skip to content

Instantly share code, notes, and snippets.

@pdtyreus
Created January 1, 2017 19:58
Show Gist options
  • Save pdtyreus/b599e40b3a94fba3b80ca5fdd63f11b3 to your computer and use it in GitHub Desktop.
Save pdtyreus/b599e40b3a94fba3b80ca5fdd63f11b3 to your computer and use it in GitHub Desktop.
Example console-based test program for conversation-kit
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.synclab.conversationkit.builder.JsonGraphBuilder;
import com.synclab.conversationkit.impl.DirectedConversationEngine;
import com.synclab.conversationkit.impl.MapBackedState;
import com.synclab.conversationkit.model.IConversationSnippet;
import com.synclab.conversationkit.model.IConversationState;
import com.synclab.conversationkit.model.SnippetContentType;
import com.synclab.conversationkit.model.SnippetType;
import com.synclab.conversationkit.model.UnmatchedResponseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.slf4j.LoggerFactory;
public class ConsoleChat {
private static class BasicUnmatchedSnippet<S extends IConversationState> implements IConversationSnippet<S> {
@Override
public String renderContent(S state) {
return "I'm sorry, I didn't understand your response '" + state.getCurrentResponse() + "'.";
}
@Override
public SnippetType getType() {
return SnippetType.STATEMENT;
}
@Override
public Iterable<String> getSuggestedResponses(S state) {
return null;
}
@Override
public SnippetContentType getContentType() {
return SnippetContentType.TEXT;
}
}
public static void main(String[] args) {
Logger logbackRoot = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logbackRoot.setLevel(Level.INFO);
System.setProperty("java.util.logging.SimpleFormatter.format"," %4$s: %5$s%6$s%n");
BufferedReader br = null;
BasicUnmatchedSnippet<MapBackedState> unmatchedSnippet = new BasicUnmatchedSnippet();
try {
JsonGraphBuilder<MapBackedState> builder = new JsonGraphBuilder();
Reader reader = new InputStreamReader(ConsoleChat.class.getResourceAsStream("/conversation.json"));
DirectedConversationEngine<MapBackedState> tree = builder.readJsonGraph(reader);
MapBackedState state = new MapBackedState();
String[] possibleSymptoms = {"Sneezing", "Itchy Eyes", "Runny Nose"};
state.set("name", "Daniel");
state.set("possibleSymptoms", possibleSymptoms);
state.setCurrentNodeId(1);
br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
Iterable<IConversationSnippet> nodes = tree.startConversationFromState(state);
for (IConversationSnippet node : nodes) {
System.out.println("$ " + node.renderContent(state));
if ((node.getType() == SnippetType.QUESTION) && (node.getSuggestedResponses(state) != null)) {
System.out.println("$ " + "[ " + String.join(" | ", node.getSuggestedResponses(state)) + " ]");
}
}
System.out.print("> ");
String response = br.readLine();
try {
tree.updateStateWithResponse(state, response);
} catch (UnmatchedResponseException e) {
if ("quit".equals(response)) {
System.exit(0);
} else {
System.out.println("$ " + unmatchedSnippet.renderContent(state));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
{
"graph": {
"directed": true,
"label": "Directed Conversation",
"nodes": [
{
"id": "1",
"type": "Handlebars",
"label": "Ok, sounds like you're experiencing some allergy symptoms.",
"metadata": {
"snippetType": "STATEMENT",
"contentType": "TEXT"
}
},
{
"id": "2",
"type": "Handlebars",
"label": "Are your symptoms today mild or severe?",
"metadata": {
"snippetType": "QUESTION",
"contentType": "TEXT",
"suggestedResponses":"mild|severe|no symptoms"
}
},
{
"id": "3",
"type": "Handlebars",
"label": "What's bothering you the most?",
"metadata": {
"snippetType": "QUESTION",
"contentType": "TEXT",
"suggestedResponses":"{{#each possibleSymptoms}}{{this}}|{{/each}}"
}
},
{
"id": "4",
"type": "Handlebars",
"label": "That's great news {{name}}!",
"metadata": {
"snippetType": "STATEMENT",
"contentType": "TEXT"
}
}
],
"edges":[
{
"source": "1",
"target": "2",
"directed": true,
"label":"",
"type":"Statement",
"metadata": {
}
},
{
"source": "2",
"target": "3",
"directed": true,
"label":"mild",
"type":"Regex",
"metadata": {
"pattern":"(?i)mild",
"stateKey":"severity",
"stateValue":"1"
}
},
{
"source": "2",
"target": "3",
"directed": true,
"label":"mild",
"type":"Regex",
"metadata": {
"pattern":"(?i)severe",
"stateKey":"severity",
"stateValue":"2"
}
},
{
"source": "2",
"target": "4",
"directed": true,
"label":"mild",
"type":"Regex",
"metadata": {
"pattern":"(?i)none|no symptoms",
"stateKey":"0"
}
}
]
}
}
@pdtyreus
Copy link
Author

pdtyreus commented Jan 1, 2017

This is a simple testing / debugging program I made while working on conversation-kit. It's the beginnings changing my Hayfever app into a conversational bot.

It will generate interactive output similar to this:

  INFO: Created 4 nodes
  INFO: Created 4 edges
  INFO: edge 'StatementEdge {true}' matches
$ Ok, sounds like you're experiencing some allergy symptoms.
$ Are your symptoms today mild or severe?
$ [ mild | severe | no symptoms ]
> severe
  INFO: response 'severe' matches edge 'RegexEdge {(?i)severe}'
$ What's bothering you the most?
$ [ Sneezing | Itchy Eyes | Runny Nose ]
> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment