!759 // Example input: // If the available commands are "load", "save" and "quit", which command is "q" likely to mean? static S parsingRules = [[ case-insensitive if the available commands are , which command is likely to mean? = question = cmd = cmdlist = cmdlist , = and and = and ]]; // returns null if className is not a parsed main class static Explain explainFull(O parseResult, S className) { L exp = cast call(parseResult, "explainFull", className); if (exp == null) ret null; ret new Explain(parseResult, exp); } static S findBestMatch(S query, L cmds) { query = query.toLowerCase(); new Map map; for (S cmd : cmds) map.put(cmd, l(commonPrefix(cmd.toLowerCase(), query))); ret highest(map); } static O parser; p { parser = newParser(); } synchronized answer { // prematching to not fire up the parser every time... if (matchStart("If the available commands are", s)) { O parseResult = call(parser, "parse", s, parsingRules); Explain explain = explainFull(parseResult, "question"); if (explain != null) { Explain dehlist = explain.sub(0); L quotedCmds = dehlist.findAll("cmd"); S query = unquote(explain.sub(1).string()); new L cmds; for (S cmd : quotedCmds) cmds.add(unquote(cmd)); ret structure(findBestMatch(query, cmds)); } else ret "No parse: " + structure(parseResult); } }