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

90
LINES

< > BotCompany Repo | #1002362 // Understander Bot (xth attempt)

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

Libraryless. Click here for Pure Java version (1542L/9K/32K).

!752

static class Source {
  S username, slackTS;
  long time;
}

static class Rel {
  Source source;
  
  S a, b; // the things to relate
  S name; // e.g. "is a"
  
  int score; // -1 (no), 0 (unknown), 1 (yes)
}

static new L<Rel> relations;

p {
  load("relations");
}

answer {
  if "* is a *" {
    saveRel(m.unq(0), m.unq(1), "is a", 1);
    ret "OK, relation saved.";
  }
  
  if "* is not a *" {
    saveRel(m.unq(0), m.unq(1), "is a", -1);
    ret "OK, anti-relation saved.";
  }
  
  if "number of relations"
    ret str(l(relations));
    
  if "list all relations"
    ret structure(relations);
    
  if "is * a *" {
    S a = m.unq(0), b = m.unq(1);
    int score = relScore(a, b, "is a");
    S answer = score < 0 ? "No, * is not a *"
      : score > 0 ? "Yes, * is a *"
      : "I'm not sure if * is a *.";
    ret format(answer, a, b);
  }
  
}

static void saveRel(S a, S b, S name, int score) {
  new Rel r;
  r.source = currentSource();
  r.a = a;
  r.b = b;
  r.name = name;
  r.score = score;
  relations.add(r);
  save("relations");
}

static Source currentSource() {
  new Source s;
  s.username = getUserName();
  s.slackTS = getSlackTS();
  s.time = now();
  ret s;
}

static int relScore(S a, S b, S name) {
  L<Rel> l = findRels(a, b, name);
  ret listScore(l);
}

static L<Rel> findRels(S a, S b, S name) {
  new L<Rel> l;
  for (Rel r : relations)
    if (eq(r.a, a) && eq(r.b, b) && eq(r.name, name))
      l.add(r);
  ret l;
}

static int listScore(L<Rel> l) {
  int min = 1, max = -1;
  for (Rel r : l) {
    min = min(min, r.score);
    max = max(max, r.score);
  }
  ret min != max ? 0 : min; // default to unknown
}

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: #1002362
Snippet name: Understander Bot (xth attempt)
Eternal ID of this version: #1002362/1
Text MD5: 445b48139db3188ae7c2221af1575457
Transpilation MD5: 88c89ac2280c2181f8a8e5e6e622dd73
Author: stefan
Category: eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-09 19:04:40
Source code size: 1742 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 591 / 867
Referenced in: [show references]