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

1  
!752
2  
3  
static class Source {
4  
  S username, slackTS;
5  
  long time;
6  
}
7  
8  
static class Rel {
9  
  Source source;
10  
  
11  
  S a, b; // the things to relate
12  
  S name; // e.g. "is a"
13  
  
14  
  int score; // -1 (no), 0 (unknown), 1 (yes)
15  
}
16  
17  
static new L<Rel> relations;
18  
19  
p {
20  
  load("relations");
21  
}
22  
23  
answer {
24  
  if "* is a *" {
25  
    saveRel(m.unq(0), m.unq(1), "is a", 1);
26  
    ret "OK, relation saved.";
27  
  }
28  
  
29  
  if "* is not a *" {
30  
    saveRel(m.unq(0), m.unq(1), "is a", -1);
31  
    ret "OK, anti-relation saved.";
32  
  }
33  
  
34  
  if "number of relations"
35  
    ret str(l(relations));
36  
    
37  
  if "list all relations"
38  
    ret structure(relations);
39  
    
40  
  if "is * a *" {
41  
    S a = m.unq(0), b = m.unq(1);
42  
    int score = relScore(a, b, "is a");
43  
    S answer = score < 0 ? "No, * is not a *"
44  
      : score > 0 ? "Yes, * is a *"
45  
      : "I'm not sure if * is a *.";
46  
    ret format(answer, a, b);
47  
  }
48  
  
49  
}
50  
51  
static void saveRel(S a, S b, S name, int score) {
52  
  new Rel r;
53  
  r.source = currentSource();
54  
  r.a = a;
55  
  r.b = b;
56  
  r.name = name;
57  
  r.score = score;
58  
  relations.add(r);
59  
  save("relations");
60  
}
61  
62  
static Source currentSource() {
63  
  new Source s;
64  
  s.username = getUserName();
65  
  s.slackTS = getSlackTS();
66  
  s.time = now();
67  
  ret s;
68  
}
69  
70  
static int relScore(S a, S b, S name) {
71  
  L<Rel> l = findRels(a, b, name);
72  
  ret listScore(l);
73  
}
74  
75  
static L<Rel> findRels(S a, S b, S name) {
76  
  new L<Rel> l;
77  
  for (Rel r : relations)
78  
    if (eq(r.a, a) && eq(r.b, b) && eq(r.name, name))
79  
      l.add(r);
80  
  ret l;
81  
}
82  
83  
static int listScore(L<Rel> l) {
84  
  int min = 1, max = -1;
85  
  for (Rel r : l) {
86  
    min = min(min, r.score);
87  
    max = max(max, r.score);
88  
  }
89  
  ret min != max ? 0 : min; // default to unknown
90  
}

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