Libraryless. Click here for Pure Java version (2462L/17K/53K).
1 | !759 |
2 | |
3 | // Example input: |
4 | // If the available commands are "load", "save" and "quit", which command is "q" likely to mean? |
5 | |
6 | static S parsingRules = [[ |
7 | |
8 | case-insensitive |
9 | |
10 | if the available commands are <cmdlist>, which command is <cmd> likely to mean? = question |
11 | |
12 | <quoted> = cmd |
13 | <cmd> = cmdlist |
14 | <cmd> <and> <cmdlist> = cmdlist |
15 | , = and |
16 | and = and |
17 | |
18 | ]]; |
19 | |
20 | // returns null if className is not a parsed main class |
21 | static Explain explainFull(O parseResult, S className) { |
22 | L exp = cast call(parseResult, "explainFull", className); |
23 | if (exp == null) ret null; |
24 | ret new Explain(parseResult, exp); |
25 | } |
26 | |
27 | static S findBestMatch(S query, L<S> cmds) { |
28 | query = query.toLowerCase(); |
29 | new Map<S, Integer> map; |
30 | for (S cmd : cmds) |
31 | map.put(cmd, l(commonPrefix(cmd.toLowerCase(), query))); |
32 | ret highest(map); |
33 | } |
34 | |
35 | static O parser; |
36 | |
37 | p { |
38 | parser = newParser(); |
39 | } |
40 | |
41 | synchronized answer { |
42 | // prematching to not fire up the parser every time... |
43 | if (matchStart("If the available commands are", s)) { |
44 | O parseResult = call(parser, "parse", s, parsingRules); |
45 | Explain explain = explainFull(parseResult, "question"); |
46 | if (explain != null) { |
47 | Explain dehlist = explain.sub(0); |
48 | L<S> quotedCmds = dehlist.findAll("cmd"); |
49 | S query = unquote(explain.sub(1).string()); |
50 | new L<S> cmds; |
51 | for (S cmd : quotedCmds) cmds.add(unquote(cmd)); |
52 | ret structure(findBestMatch(query, cmds)); |
53 | } else |
54 | ret "No parse: " + structure(parseResult); |
55 | } |
56 | } |
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
Snippet ID: | #1002418 |
Snippet name: | A Simple Command Finding Bot |
Eternal ID of this version: | #1002418/3 |
Text MD5: | f29295ce4d74f1334ed9dcb8c9cebb3f |
Transpilation MD5: | 7c6032fe5788520726525e260f852e08 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-06-09 03:35:29 |
Source code size: | 1533 bytes / 56 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 703 / 1288 |
Version history: | 2 change(s) |
Referenced in: | [show references] |