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

78
LINES

< > BotCompany Repo | #1001602 // Multi Assoc Bot (works)

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

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

1  
!752
2  
3  
m {
4  
  static new MultiMap<S, S> map;
5  
  static S name = "Multi Assoc Bot";
6  
  static boolean chatEnabled = true;
7  
8  
  p {
9  
    readLocally("map");
10  
    makeAndroid3(name + " at " + getComputerID() + ".");
11  
    if (chatEnabled)
12  
      toChat();
13  
  }
14  
  
15  
  static void toChat() {
16  
    while (true) {
17  
      pcall {
18  
        for (ChatLine l : suckHumans(name))
19  
          sayInWebChat(name, answer(l.text));
20  
      }
21  
      sleep(1000);
22  
    }
23  
  }
24  
  
25  
  static L<S> lookup(S s) {
26  
    ret map.get(simplify(s));
27  
  }
28  
  
29  
  // should simplify a sentence neatly
30  
  static S simplify(S s) {
31  
    // TODO: probably leave case of quoted strings intact
32  
    S result = join(" ", codeTokensOnly(tokensToLowerCase(nlTok(s))));
33  
    print(s);
34  
    print("=> " + result);
35  
    ret result;
36  
  }
37  
  
38  
  static synchronized S answer(S input) {
39  
    L<S> tok = nlTok(input);
40  
    int idx = tok.indexOf("=");
41  
    if (idx >= 0) {
42  
      S l = join(tok.subList(0, idx)).trim();
43  
      S r = join(tok.subList(idx+1, l(tok))).trim();
44  
      l = simplify(l);
45  
      L<S> values = map.get(l);
46  
      int i = indexOfSentence(values, r);
47  
      
48  
      // standard case (new value)
49  
      if (i < 0) {
50  
        map.put(l, r);
51  
        saveLocally("map");
52  
        ret "ok, added";
53  
      }
54  
      
55  
      // already latest value
56  
      if (i == values.size()-1)
57  
        ret "yup, i know :)";
58  
        
59  
      // reordering case
60  
      values.add(r);
61  
      values.remove(i);
62  
      saveLocally("map");
63  
      ret "ok, moving value to end";
64  
    }
65  
    
66  
    L<S> values = lookup(input);
67  
    if (values.isEmpty()) ret null;
68  
    ret input + "=" + join(", ", values);
69  
  }
70  
  
71  
  static int indexOfSentence(L<S> l, S sentence) {
72  
    sentence = simplify(sentence);
73  
    for (int i = 0; i < l(l); i++)
74  
      if (eq(simplify(l.get(i)), sentence))
75  
        ret i;
76  
    ret -1;
77  
  }
78  
}

Author comment

Began life as a copy of #1001581

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: #1001602
Snippet name: Multi Assoc Bot (works)
Eternal ID of this version: #1001602/1
Text MD5: 6d8624a352cd128ad660a96aa7a693f8
Transpilation MD5: d7b8f4f120cfda6d64e12c96682fa4f7
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-13 23:19:14
Source code size: 1869 bytes / 78 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 622 / 1184
Referenced in: [show references]