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).

1  
!752
2  
3  
static S dataProgID = "#1002143"; // use data dir of "These Are Bot" v1 (don't use both bots at once...)
4  
5  
// TODO: make a PersistentMultiMap?
6  
7  
static new MultiMap<S, S> lists;
8  
static new MultiMap<S, S> notLists;
9  
10  
p {
11  
  load(dataProgID, "lists");
12  
  load(dataProgID, "notLists");
13  
  makeBot("These Are Bot.");
14  
}
15  
16  
synchronized answer {
17  
  L<S> tok = javaTok(s);
18  
  if (eqic(get(tok, 1), "these") && eqic(get(tok, 3), "are") && eqic(get(tok, 7), ":")) {
19  
    S kind = unquote(get(tok, 5));
20  
    L<S> rest = subList(tok, 8);
21  
    ret addThem(kind, rest, true);
22  
  }
23  
  
24  
  if (eqic(get(tok, 1), "these") && eqic(get(tok, 3), "are") && eqic(get(tok, 5), "not") && eqic(get(tok, 9), ":")) {
25  
    S kind = unquote(get(tok, 7));
26  
    L<S> rest = subList(tok, 10);
27  
    ret addThem(kind, rest, false);
28  
  }
29  
  
30  
  if (match("name a *", s, m)) {
31  
    S kind = singular(simplify(m.unq(0)));
32  
    L<S> items = lists.get(kind);
33  
    if (empty(items))
34  
      ret null; //"I don't know any " + m.unq(0);
35  
    ret randomOne(items);
36  
  }
37  
  
38  
  if (match("name * *", s, m)) {
39  
    S n = m.get(0);
40  
    if (isInteger(n)) {
41  
      int _n = min(100, parseInt(n));
42  
      S kind = singular(simplify(m.unq(1)));
43  
      L<S> items = cloneList(lists.get(kind));
44  
      if (l(items) < _n)
45  
        ret "I don't know that many " + m.unq(1);
46  
      new L<S> l;
47  
      for (int i = 0; i < _n; i++) {
48  
        S x = randomOne(items);
49  
        l.add(x);
50  
        items.remove(x);
51  
      }
52  
      ret join(", ", l);
53  
    }
54  
  }
55  
  
56  
  S userName = cast callOpt(mainBot, "getUserName");
57  
  
58  
  if (match("am i a *", s, m)) {
59  
    if (empty(userName))
60  
      ret "Well I don't know who you are! Please join slack.com";
61  
    S kind = m.unq(0);
62  
    boolean result = containsIgnoreCase(lists.get(kind), userName)
63  
      || containsIgnoreCase(lists.get(kind), dropPrefix("@", userName));
64  
    if (result)
65  
      ret format("Yes, you are a *!", kind);
66  
    else
67  
      ret format("As far as I know, you are not a *.", kind);
68  
  }
69  
  
70  
  if (match("is * a *", s, m)) {
71  
    S instance = m.unq(0);
72  
    S kind = singular(simplify(m.unq(1)));
73  
    boolean result = containsIgnoreCase(lists.get(kind), instance);
74  
    if (result)
75  
      ret format("Yes, * is a *", instance, kind);
76  
    else
77  
      ret null;
78  
      //ret format("I don't know that * is a *", instance, kind);
79  
  }
80  
  
81  
  if (match("Are there any * other than *?", s, m)) {
82  
    S kind = singular(simplify(m.unq(0)));
83  
    S instance = m.unq(1);
84  
    L<S> items = cloneList(lists.get(kind));
85  
    removeIgnoreCase(items, instance);
86  
    if (empty(items))
87  
      ret format("No, * is the only *.", instance, kind);
88  
    else
89  
      ret format("Yes, for example *", randomOne(items));
90  
  }
91  
  
92  
  if (match("list all *", s, m)) {
93  
    S kind = singular(simplify(m.unq(0)));
94  
    L<S> items = lists.get(kind);
95  
    if (empty(items))
96  
      ret null; // format("I don't know any *", kind);
97  
    else
98  
      ret join(", ", items);
99  
  }
100  
  
101  
  if (match("list all ex *", s, m)) {
102  
    S kind = singular(simplify(m.unq(0)));
103  
    L<S> items = notLists.get(kind);
104  
    if (empty(items))
105  
      ret null; // format("I don't know any ex *", kind);
106  
    else
107  
      ret join(", ", items);
108  
  }
109  
}
110  
111  
static S simplify(S s) {
112  
  ret toLower(s);
113  
}
114  
115  
static S singular(S s) {
116  
  if (s.endsWith("ies"))
117  
    ret dropSuffix("ies", s) + "y";
118  
  ret dropSuffix("s", s);
119  
}
120  
121  
static S addThem(S kind, L<S> rest, boolean plus) {
122  
  kind = singular(simplify(kind));
123  
  boolean comma = rest.contains(",");
124  
  new L<S> items;
125  
  if (comma)
126  
    for (L<S> item : splitListBy(rest, ","))
127  
      items.add(trim(join(simpleSpaces(item))));
128  
  else
129  
    items.addAll(codeTokensOnly(rest));
130  
  printFormat((plus ? "Adding" : "Removing") + " items * to/from kind *", items, kind);
131  
  (plus ? lists : notLists).addAllIfNotThere(kind, items);
132  
  (plus ? notLists : lists).removeAll(kind, items);
133  
  save(dataProgID, "lists");
134  
  save(dataProgID, "notLists");
135  
  ret format("OK, now " + l(lists.get(kind)) + " items in kind *", kind);
136  
}

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: 693 / 1714
Referenced in: [show references]