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

112
LINES

< > BotCompany Repo | #1002143 // These Are Bot

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

Libraryless. Click here for Pure Java version (2269L/15K/50K).

!752

static O mainBot;

// TODO: make a PersistentMultiMap?

static new MultiMap<S, S> lists;

p {
  load("lists");
  makeBot("These Are Bot.");
}

synchronized answer {
  L<S> tok = javaTok(s);
  if (eq(get(tok, 1), "these") && eqic(get(tok, 3), "are") && eqic(get(tok, 7), ":")) {
    S kind = unq(get(tok, 5));
    kind = singular(simplify(kind));
    L<S> rest = subList(tok, 8);
    boolean comma = rest.contains(",");
    new L<S> items;
    if (comma)
      for (L<S> item : splitListBy(rest, ","))
        items.add(trim(join(simpleSpaces(item))));
    else
      items.addAll(codeTokensOnly(rest));
    printFormat("Adding items * to kind *", items, kind);
    lists.addAll(kind, items);
    save("lists");
    ret format("OK, now " + l(lists.get(kind)) + " items in kind *", kind);
  }
  
  if (match("name a *", s, m)) {
    S kind = singular(simplify(m.unq(0)));
    L<S> items = lists.get(kind);
    if (empty(items))
      ret "I don't know any " + m.unq(0);
    ret randomOne(items);
  }
  
  if (match("name * *", s, m)) {
    S n = m.get(0);
    if (isInteger(n)) {
      int _n = min(100, parseInt(n));
      S kind = singular(simplify(m.unq(1)));
      L<S> items = cloneList(lists.get(kind));
      if (l(items) < _n)
        ret "I don't know that many " + m.unq(1);
      new L<S> l;
      for (int i = 0; i < _n; i++) {
        S x = randomOne(items);
        l.add(x);
        items.remove(x);
      }
      ret join(", ", l);
    }
  }
  
  S userName = cast callOpt(mainBot, "getUserName");
  
  if (match("am i a *", s, m)) {
    if (empty(userName))
      ret "Well I don't know who you are! Please join slack.com";
    S kind = m.unq(0);
    boolean result = containsIgnoreCase(lists.get(kind), userName)
      || containsIgnoreCase(lists.get(kind), dropPrefix("@", userName));
    if (result)
      ret format("Yes, you are a *!", kind);
    else
      ret format("As far as I know, you are not a *.", kind);
  }
  
  if (match("is * a *", s, m)) {
    S instance = m.unq(0);
    S kind = singular(simplify(m.unq(1)));
    boolean result = containsIgnoreCase(lists.get(kind), instance);
    if (result)
      ret format("Yes, * is a *", instance, kind);
    else
      ret format("I don't know that * is a *", instance, kind);
  }
  
  if (match("Are there any * other than *?", s, m)) {
    S kind = singular(simplify(m.unq(0)));
    S instance = m.unq(1);
    L<S> items = cloneList(lists.get(kind));
    removeIgnoreCase(items, instance);
    if (empty(items))
      ret format("No, * is the only *.", instance, kind);
    else
      ret format("Yes, for example *", randomOne(items));
  }
  
  if (match("list all *", s, m)) {
    S kind = singular(simplify(m.unq(0)));
    L<S> items = lists.get(kind);
    if (empty(items))
      ret format("I don't know any *", kind);
    else
      ret join(", ", items);
  }
}

static S simplify(S s) {
  ret toLower(s);
}

static S singular(S s) {
  if (s.endsWith("ies"))
    ret dropSuffix("ies", s) + "y";
  ret dropSuffix("s", s);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002143
Snippet name: These Are Bot
Eternal ID of this version: #1002143/1
Text MD5: d23ea2215f9432013cce20eaec9c8fd5
Transpilation MD5: 8f3b081b80f295119e25457edfd2ec3f
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-22 01:10:24
Source code size: 3122 bytes / 112 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 736 / 1200
Referenced in: [show references]