import user.*; import system.*; import se.chalmers.cs.gf.util.Pair; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.List; /** * A very simple demo of a single-modality multilingual dialog * system using GF in Java. */ public class SimpleDemoText { private UserMain user; private SystemMain system; public SimpleDemoText(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 * returns outputs the answer. */ public String handleInput(String text) { System.err.println(" Concrete 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 null; // get the unambiguous input String inputLang = p.fst; Input input = p.snd; System.err.println(" Abstract input: " + input + " (" + inputLang + ")"); // figure out answer to user input String outputLang = getOutputLang(inputLang); Output output = answer(input); System.err.println(" Abstract output: " + output); // linearize and output answer String outputText = system.linearizeOutput(outputLang, output); System.err.println(" Concrete output: " + outputText); return 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; } public static void main(String [] args) throws java.io.IOException { UserMain user = new UserMain("user.properties"); SystemMain system = new SystemMain("system.properties"); SimpleDemoText demo = new SimpleDemoText(user, system); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("> "); System.out.flush(); String input = in.readLine(); if (input == null) break; String output = demo.handleInput(input); if (output == null) continue; System.out.println(output); } } }