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: 608 / 1158
Version history: 2 change(s)
Referenced in: [show references]