import user.*; import system.*; import se.chalmers.cs.gf.util.Pair; import se.chalmers.cs.gf.dialogutil.*; import se.chalmers.cs.gf.dialogutil.sr.*; import se.chalmers.cs.gf.dialogutil.tts.*; import java.util.List; /** * A very simple demo of a single-modality multilingual dialog * system using GF in Java. */ public class SimpleDemo { private TextListenerList listeners = new TextListenerList(); private UserMain user; private SystemMain system; public SimpleDemo(UserMain user, SystemMain system) { this.user = user; this.system = system; } /** * Parses the user input, creates system output by calling answer(), * linearizes the system output in the language that the user used * and outputs the answer. */ private void handleInput(String text) { System.err.println("Input: " + text); // parse the input with all available languages List> inputs = user.parseInputWithAll(text); // disambiguate the input, do nothing if that fails Pair p = disambiguate(inputs); if (p == null) return; // get the unambiguous input String inputLang = p.fst; Input input = p.snd; System.err.println("Parsed: " + input + " (" + inputLang + ")"); // figure out answer to user input String outputLang = getOutputLang(inputLang); Output output = answer(input); System.err.println("Output: " + output); // linearize and output answer String outputText = system.linearizeOutput(outputLang, output); System.err.println("Saying: " + outputText); listeners.fireTextEvent(outputText); } /** * Disambiguate parse results. * @return The single umabiguous parse result, if any. Otherwise * null. */ private Pair disambiguate(List> is) { if (is.size() == 0) { System.err.println("No parse"); return null; } else if (is.size() > 1) { System.err.println("Ambiguous parse:"); for (Pair i : is) System.err.println(i.fst + ": " + i.snd); return null; } return is.get(0); } /** * Calculates a system output from a user input. */ private Output answer(Input input) { return input.accept(new Oracle(), null); } private class Oracle implements Input.Visitor { public Output visit(user.Thanks p, Object arg) { return new Welcome(); } public Output visit(HereYouGo p, Object arg) { return new system.Thanks(); } } /** * Gets the output language to use for a given input language. */ private String getOutputLang(String inputLang) { String lang = inputLang.substring(inputLang.length()-3); return "System" + lang; } /** * Add a source of user input, e.g. a dialog box or a speech * recognition engine. */ public void addInputSource(TextInput inputSource) { inputSource.addTextListener(new TextListener() { public void textEvent(TextEvent e) { handleInput(e.getText()); } }); } /** * Adds a sink to which system output will be sent. */ public void addOutputSink(TextListener l) { listeners.add(l); } public static void main(String [] args) throws java.io.IOException { UserMain user = new UserMain("user.properties"); SystemMain system = new SystemMain("system.properties"); SimpleDemo demo = new SimpleDemo(user, system); // Get input from a simple dialog demo.addInputSource(new DialogInput()); // For Nuance OAA text input //Recognizer recog = new Recognizer("simpledemo", args); //demo.addInputSource(new RecognizerInput(recog)); // Print output to System.err demo.addOutputSink(new ConsoleOutput()); // Speak output with a JSAPI speech synthesizer demo.addOutputSink(new JavaSpeechOutput()); } }