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

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

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