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

136
LINES

< > BotCompany Repo | #1002240 // These Are Bot 2 (with deleted items)

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

Libraryless. Click here for Pure Java version (2631L/17K/58K).

!752

static S dataProgID = "#1002143"; // use data dir of "These Are Bot" v1 (don't use both bots at once...)

// TODO: make a PersistentMultiMap?

static new MultiMap<S, S> lists;
static new MultiMap<S, S> notLists;

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

synchronized answer {
  L<S> tok = javaTok(s);
  if (eqic(get(tok, 1), "these") && eqic(get(tok, 3), "are") && eqic(get(tok, 7), ":")) {
    S kind = unquote(get(tok, 5));
    L<S> rest = subList(tok, 8);
    ret addThem(kind, rest, true);
  }
  
  if (eqic(get(tok, 1), "these") && eqic(get(tok, 3), "are") && eqic(get(tok, 5), "not") && eqic(get(tok, 9), ":")) {
    S kind = unquote(get(tok, 7));
    L<S> rest = subList(tok, 10);
    ret addThem(kind, rest, false);
  }
  
  if (match("name a *", s, m)) {
    S kind = singular(simplify(m.unq(0)));
    L<S> items = lists.get(kind);
    if (empty(items))
      ret null; //"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 null;
      //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 null; // format("I don't know any *", kind);
    else
      ret join(", ", items);
  }
  
  if (match("list all ex *", s, m)) {
    S kind = singular(simplify(m.unq(0)));
    L<S> items = notLists.get(kind);
    if (empty(items))
      ret null; // format("I don't know any ex *", 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);
}

static S addThem(S kind, L<S> rest, boolean plus) {
  kind = singular(simplify(kind));
  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((plus ? "Adding" : "Removing") + " items * to/from kind *", items, kind);
  (plus ? lists : notLists).addAllIfNotThere(kind, items);
  (plus ? notLists : lists).removeAll(kind, items);
  save(dataProgID, "lists");
  save(dataProgID, "notLists");
  ret format("OK, now " + l(lists.get(kind)) + " items in kind *", kind);
}

Author comment

Began life as a copy of #1002143

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002240
Snippet name: These Are Bot 2 (with deleted items)
Eternal ID of this version: #1002240/1
Text MD5: e1cbd7464c54907733747704d03fa759
Transpilation MD5: 02215eb903786f5f8960a305cc8c5555
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-17 20:17:10
Source code size: 4051 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 692 / 1713
Referenced in: #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)