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

105
LINES

< > BotCompany Repo | #1003015 // Opposites

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

Libraryless. Click here for Pure Java version (2013L/13K/44K).

1  
!752
2  
3  
static new MultiMap<S, Node> byName; // multiple entries not really handled yet
4  
5  
sclass BaseNode {
6  
  S id = randomID(12);
7  
  S name;
8  
  
9  
  new L<Node> related;
10  
  new Map<Node, Float> relationStrengths; // strength of relation, 0 to 1
11  
  new L<O> identicalOuter; // identical nodes in other bots
12  
13  
  *() {}  
14  
  *(S *name) {
15  
    byName.put(name, (Node) this);
16  
  }
17  
  
18  
}
19  
20  
sclass Node extends BaseNode {
21  
  *() {}
22  
  *(S name) {
23  
    super(name);
24  
  }
25  
  
26  
  new L<Node> opposites; // list of opposites
27  
  new L<Node> dropped; // no longer considered opposites
28  
  new Map<Node, S> modifiers; // optional modifier for each entry
29  
  new Map<Node, L> sources; // source(s) of each entry
30  
}
31  
32  
p {
33  
  /*Node white = new Node("white");
34  
  Node black = new Node("black");
35  
  white.opposites.add(black);
36  
  black.opposites.add(white);*/
37  
  load("byName");
38  
}
39  
40  
static void saveAll() {
41  
  save("byName");
42  
}
43  
44  
answer { exceptionToUser {
45  
  if (!tb()) null;
46  
  
47  
  if (match("is * an opposite of *", s, m)
48  
    || match("is * the opposite of *", s, m)) {
49  
    S a = m.tlc(0), b = m.tlc(1);
50  
    L<Node> la = byName.get(a), lb = byName.get(b);
51  
    if (empty(la)) ret format("What is *?", a);
52  
    if (empty(lb)) ret format("What is *?", b);
53  
    for (Node na : la)
54  
      for (Node nb : lb)
55  
        if (na.opposites.contains(nb))
56  
          ret "yes";
57  
    ret "no";
58  
  }
59  
  
60  
  
61  
  if (match("what is an opposite of *", s, m)
62  
    || match("what is the opposite of *", s, m)) {
63  
    S a = m.tlc(0);
64  
    L<Node> la = byName.get(a);
65  
    if (empty(la)) ret format("What is *?", a);
66  
    
67  
    new L<Node> l;
68  
    for (Node na : la)
69  
      setAddAll(l, na.opposites);
70  
    if (empty(l))
71  
      ret "I don't know";
72  
    ret join(", ", collectField(l, "name"));
73  
  }
74  
75  
  if (match("* is an opposite of *", s, m)
76  
    || match("* is the opposite of *", s, m)) {
77  
    S a = m.tlc(0), b = m.tlc(1);
78  
    Node na = getOrMake(a), nb = getOrMake(b);
79  
    setAdd(na.opposites, nb);
80  
    setAdd(nb.opposites, na);
81  
    na.dropped.remove(nb);
82  
    nb.dropped.remove(na);
83  
    saveAll();
84  
    ret "OK";
85  
  }
86  
  
87  
  if (match("* is not an opposite of *", s, m)
88  
    || match("* is not the opposite of *", s, m)) {
89  
    S a = m.tlc(0), b = m.tlc(1);
90  
    Node na = getOrMake(a), nb = getOrMake(b);
91  
    setAdd(na.dropped, nb);
92  
    setAdd(nb.dropped, na);
93  
    na.opposites.remove(nb);
94  
    nb.opposites.remove(na);
95  
    saveAll();
96  
    ret "OK";
97  
  }
98  
}}
99  
100  
static Node getOrMake(S name) {
101  
  Node n = byName.getFirst(name);
102  
  if (n == null)
103  
    n = new Node(name);
104  
  ret n;
105  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003015
Snippet name: Opposites
Eternal ID of this version: #1003015/1
Text MD5: a7b9b241195ee2aa61ecb3b923b3ee42
Transpilation MD5: 4bd668d6b3a278266df4e62435f02b1b
Author: stefan
Category: nl bots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-19 03:06:42
Source code size: 2577 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 799 / 1072
Referenced in: [show references]