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

!752

m {
  static new MultiMap<S, S> map;
  static S name = "Multi Assoc Bot";
  static boolean chatEnabled = true;

  p {
    readLocally("map");
    makeAndroid3(name + " at " + getComputerID() + ".");
    if (chatEnabled)
      toChat();
  }
  
  static void toChat() {
    while (true) {
      pcall {
        for (ChatLine l : suckHumans(name))
          sayInWebChat(name, answer(l.text));
      }
      sleep(1000);
    }
  }
  
  static L<S> lookup(S s) {
    ret map.get(simplify(s));
  }
  
  // should simplify a sentence neatly
  static S simplify(S s) {
    // TODO: probably leave case of quoted strings intact
    S result = join(" ", codeTokensOnly(tokensToLowerCase(nlTok(s))));
    print(s);
    print("=> " + result);
    ret result;
  }
  
  static synchronized S answer(S input) {
    L<S> tok = nlTok(input);
    int idx = tok.indexOf("=");
    if (idx >= 0) {
      S l = join(tok.subList(0, idx)).trim();
      S r = join(tok.subList(idx+1, l(tok))).trim();
      l = simplify(l);
      L<S> values = map.get(l);
      int i = indexOfSentence(values, r);
      
      // standard case (new value)
      if (i < 0) {
        map.put(l, r);
        saveLocally("map");
        ret "ok, added";
      }
      
      // already latest value
      if (i == values.size()-1)
        ret "yup, i know :)";
        
      // reordering case
      values.add(r);
      values.remove(i);
      saveLocally("map");
      ret "ok, moving value to end";
    }
    
    L<S> values = lookup(input);
    if (values.isEmpty()) ret null;
    ret input + "=" + join(", ", values);
  }
  
  static int indexOfSentence(L<S> l, S sentence) {
    sentence = simplify(sentence);
    for (int i = 0; i < l(l); i++)
      if (eq(simplify(l.get(i)), sentence))
        ret i;
    ret -1;
  }
}

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: 623 / 1184
Referenced in: #1001603 - Multi Assoc Bot Clone
#3000195 - Answer for stefanreich (>> y)
#3000196 - Answer for stefanreich (>> y)
#3000197 - Answer for stefanreich (>> program data sizes)
#3000198 - Answer for stefanreich (>> program data sizes)
#3000199 - Answer for stefanreich (>> program data sizes)
#3000200 - Answer for stefanreich (>> program data sizes)
#3000201 - Answer for stefanreich (>> list all files)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)