Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1002418 // A Simple Command Finding Bot

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

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

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: 702 / 1287
Version history: 2 change(s)
Referenced in: #1002420 - Parser Test
#3000169 - Answer for stefanreich
#3000174 - Answer for stefanreich
#3000175 - Answer for stefanreich
#3000176 - Answer for stefanreich
#3000177 - Answer for stefanreich
#3000179 - Answer for stefanreich
#3000180 - Answer for stefanreich
#3000181 - Answer for stefanreich
#3000182 - Answer for stefanreich
#3000184 - Answer for stefanreich
#3000185 - Answer for stefanreich
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000203 - Answer for stefanreich (>> timing hello bot)
#3000208 - Answer for bgrgndz (>> list sub bots)
#3000209 - Answer for bgrgndz (>> list sub bots)
#3000238 - Answer for stefanreich (>> t power bot)
#3000369 - Answer for Stefan (>> list sub bots)
#3000376 - Answer for stefanreich (>> list sub bots)
#3000377 - Answer for stefanreich (>> timing eleu?)
#3000380 - Answer for stefanreich (>> list sub bots)
#3000381 - Answer for speedforce (>> list sub bots)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)