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

1  
!752
2  
3  
static class Line {
4  
  S text;
5  
  Source source;
6  
  
7  
  *(S *text, Source *source) {}
8  
  *() {}
9  
}
10  
11  
static class Theory {
12  
  S name;
13  
  new L<Rel> relations;
14  
  new L<Line> allLines;
15  
  
16  
  *(S *name) {}
17  
  *() {}
18  
  
19  
  int relScore(S a, S b, S name) {
20  
    L<Rel> l = findRels(a, b, name);
21  
    ret listScore(l);
22  
  }
23  
24  
  L<Rel> findRels(S a, S b, S name) {
25  
    new L<Rel> l;
26  
    for (Rel r : relations)
27  
      if (eq(r.a, a) && eq(r.b, b) && eq(r.name, name))
28  
        l.add(r);
29  
    ret l;
30  
  }
31  
  
32  
  int listScore(L<Rel> l) {
33  
    int min = 1, max = -1;
34  
    for (Rel r : l) {
35  
      min = min(min, r.score);
36  
      max = max(max, r.score);
37  
    }
38  
    ret min != max ? 0 : min; // default to unknown
39  
  }
40  
41  
  void saveRel(S a, S b, S name, int score) {
42  
    new Rel r;
43  
    r.source = currentSource();
44  
    r.a = a;
45  
    r.b = b;
46  
    r.name = name;
47  
    r.score = score;
48  
    relations.add(r);
49  
    save("theories");
50  
  }
51  
  
52  
  void saveLine(S text, Source source) {
53  
    allLines.add(new Line(text, source));
54  
    save("theories");
55  
  }
56  
}
57  
58  
static class Source {
59  
  S username, slackTS;
60  
  long time;
61  
}
62  
63  
static class Rel {
64  
  Source source;
65  
  
66  
  S a, b; // the things to relate
67  
  S name; // e.g. "is a"
68  
  
69  
  int score; // -1 (no), 0 (unknown), 1 (yes)
70  
}
71  
72  
static new Map<S, Theory> theories;
73  
74  
p {
75  
  load("theories");
76  
}
77  
78  
static S answer(S s) {
79  
  new Matches m;
80  
  
81  
  if "number of theories"
82  
    ret str(l(theories));
83  
  
84  
  if "list all theories"
85  
    ret structure(keys(theories));
86  
 
87  
  if (!matchStart("theory *:", s, m)) ret null;
88  
  
89  
  S theoryName = m.unq(0);
90  
  Theory theory = theories.get(theoryName);
91  
  if (theory == null) {
92  
    theories.put(theoryName, theory = new Theory(theoryName));
93  
    save("theories");
94  
  }
95  
  s = m.rest().trim();
96  
  
97  
  // has theory now
98  
  
99  
  theory.saveLine(s, currentSource());
100  
101  
  if "* is a *" {
102  
    theory.saveRel(m.unq(0), m.unq(1), "is a", 1);
103  
    ret "OK, relation saved.";
104  
  }
105  
  
106  
  if "* is not a *" {
107  
    theory.saveRel(m.unq(0), m.unq(1), "is a", -1);
108  
    ret "OK, anti-relation saved.";
109  
  }
110  
  
111  
  if "a * is a *" {
112  
    theory.saveRel(m.unq(0), m.unq(1), "a X is a Y", 1);
113  
    ret "OK, relation saved.";
114  
  }
115  
  
116  
  if "a * is not a *" {
117  
    theory.saveRel(m.unq(0), m.unq(1), "a X is a Y", -1);
118  
    ret "OK, anti-relation saved.";
119  
  }
120  
  
121  
  if "number of relations"
122  
    ret str(l(theory.relations));
123  
    
124  
  if "list all relations"
125  
    ret structure(theory.relations);
126  
    
127  
  if "all lines"
128  
    ret structure(theory.allLines);
129  
    
130  
  if "is * a *" {
131  
    S a = m.unq(0), b = m.unq(1);
132  
    int score = theory.relScore(a, b, "is a");
133  
    S answer = score < 0 ? "No, * is not a *"
134  
      : score > 0 ? "Yes, * is a *"
135  
      : "I'm not sure if * is a *.";
136  
    ret format(answer, a, b);
137  
  }
138  
  
139  
  ret "Unknown theory command, saved literally: " + m.rest();
140  
}
141  
142  
static Source currentSource() {
143  
  new Source s;
144  
  s.username = getUserName();
145  
  s.slackTS = getSlackTS();
146  
  s.time = now();
147  
  ret s;
148  
}

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