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

148
LINES

< > BotCompany Repo | #1002364 // Understander Bot (xth attempt, with theories)

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

Libraryless. Click here for Pure Java version (1645L/10K/34K).

!752

static class Line {
  S text;
  Source source;
  
  *(S *text, Source *source) {}
  *() {}
}

static class Theory {
  S name;
  new L<Rel> relations;
  new L<Line> allLines;
  
  *(S *name) {}
  *() {}
  
  int relScore(S a, S b, S name) {
    L<Rel> l = findRels(a, b, name);
    ret listScore(l);
  }

  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;
  }
  
  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
  }

  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("theories");
  }
  
  void saveLine(S text, Source source) {
    allLines.add(new Line(text, source));
    save("theories");
  }
}

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 Map<S, Theory> theories;

p {
  load("theories");
}

static S answer(S s) {
  new Matches m;
  
  if "number of theories"
    ret str(l(theories));
  
  if "list all theories"
    ret structure(keys(theories));
 
  if (!matchStart("theory *:", s, m)) ret null;
  
  S theoryName = m.unq(0);
  Theory theory = theories.get(theoryName);
  if (theory == null) {
    theories.put(theoryName, theory = new Theory(theoryName));
    save("theories");
  }
  s = m.rest().trim();
  
  // has theory now
  
  theory.saveLine(s, currentSource());

  if "* is a *" {
    theory.saveRel(m.unq(0), m.unq(1), "is a", 1);
    ret "OK, relation saved.";
  }
  
  if "* is not a *" {
    theory.saveRel(m.unq(0), m.unq(1), "is a", -1);
    ret "OK, anti-relation saved.";
  }
  
  if "a * is a *" {
    theory.saveRel(m.unq(0), m.unq(1), "a X is a Y", 1);
    ret "OK, relation saved.";
  }
  
  if "a * is not a *" {
    theory.saveRel(m.unq(0), m.unq(1), "a X is a Y", -1);
    ret "OK, anti-relation saved.";
  }
  
  if "number of relations"
    ret str(l(theory.relations));
    
  if "list all relations"
    ret structure(theory.relations);
    
  if "all lines"
    ret structure(theory.allLines);
    
  if "is * a *" {
    S a = m.unq(0), b = m.unq(1);
    int score = theory.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);
  }
  
  ret "Unknown theory command, saved literally: " + m.rest();
}

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

Author comment

Began life as a copy of #1002362

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: #1002364
Snippet name: Understander Bot (xth attempt, with theories)
Eternal ID of this version: #1002364/1
Text MD5: 27814f0fe45a099040f5acb1483a6946
Transpilation MD5: 61460afa3ac1d5106c88f7b2bb75a3cc
Author: stefan
Category: eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-09 20:21:53
Source code size: 3028 bytes / 148 lines
Pitched / IR pitched: No / No
Views / Downloads: 655 / 1121
Referenced in: #3000169 - Answer for stefanreich
#3000174 - Answer for stefanreich
#3000175 - Answer for stefanreich
#3000176 - Answer for stefanreich
#3000177 - Answer for stefanreich
#3000179 - Answer for stefanreich
#3000180 - Answer for stefanreich
#3000181 - Answer for stefanreich
#3000182 - Answer for stefanreich
#3000184 - Answer for stefanreich
#3000185 - Answer for stefanreich
#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)
#3000203 - Answer for stefanreich (>> timing hello 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)