Libraryless. Click here for Pure Java version (2462L/17K/53K).
!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 <cmdlist>, which command is <cmd> likely to mean? = question <quoted> = cmd <cmd> = cmdlist <cmd> <and> <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<S> cmds) { query = query.toLowerCase(); new Map<S, Integer> 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<S> quotedCmds = dehlist.findAll("cmd"); S query = unquote(explain.sub(1).string()); new L<S> cmds; for (S cmd : quotedCmds) cmds.add(unquote(cmd)); ret structure(findBestMatch(query, cmds)); } else ret "No parse: " + structure(parseResult); } }
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment