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

!752

static new MultiMap<S, Node> byName; // multiple entries not really handled yet

sclass BaseNode {
  S id = randomID(12);
  S name;
  
  new L<Node> related;
  new Map<Node, Float> relationStrengths; // strength of relation, 0 to 1
  new L<O> identicalOuter; // identical nodes in other bots

  *() {}  
  *(S *name) {
    byName.put(name, (Node) this);
  }
  
}

sclass Node extends BaseNode {
  *() {}
  *(S name) {
    super(name);
  }
  
  new L<Node> opposites; // list of opposites
  new L<Node> dropped; // no longer considered opposites
  new Map<Node, S> modifiers; // optional modifier for each entry
  new Map<Node, L> sources; // source(s) of each entry
}

p {
  /*Node white = new Node("white");
  Node black = new Node("black");
  white.opposites.add(black);
  black.opposites.add(white);*/
  load("byName");
}

static void saveAll() {
  save("byName");
}

answer { exceptionToUser {
  if (!tb()) null;
  
  if (match("is * an opposite of *", s, m)
    || match("is * the opposite of *", s, m)) {
    S a = m.tlc(0), b = m.tlc(1);
    L<Node> la = byName.get(a), lb = byName.get(b);
    if (empty(la)) ret format("What is *?", a);
    if (empty(lb)) ret format("What is *?", b);
    for (Node na : la)
      for (Node nb : lb)
        if (na.opposites.contains(nb))
          ret "yes";
    ret "no";
  }
  
  
  if (match("what is an opposite of *", s, m)
    || match("what is the opposite of *", s, m)) {
    S a = m.tlc(0);
    L<Node> la = byName.get(a);
    if (empty(la)) ret format("What is *?", a);
    
    new L<Node> l;
    for (Node na : la)
      setAddAll(l, na.opposites);
    if (empty(l))
      ret "I don't know";
    ret join(", ", collectField(l, "name"));
  }

  if (match("* is an opposite of *", s, m)
    || match("* is the opposite of *", s, m)) {
    S a = m.tlc(0), b = m.tlc(1);
    Node na = getOrMake(a), nb = getOrMake(b);
    setAdd(na.opposites, nb);
    setAdd(nb.opposites, na);
    na.dropped.remove(nb);
    nb.dropped.remove(na);
    saveAll();
    ret "OK";
  }
  
  if (match("* is not an opposite of *", s, m)
    || match("* is not the opposite of *", s, m)) {
    S a = m.tlc(0), b = m.tlc(1);
    Node na = getOrMake(a), nb = getOrMake(b);
    setAdd(na.dropped, nb);
    setAdd(nb.dropped, na);
    na.opposites.remove(nb);
    nb.opposites.remove(na);
    saveAll();
    ret "OK";
  }
}}

static Node getOrMake(S name) {
  Node n = byName.getFirst(name);
  if (n == null)
    n = new Node(name);
  ret n;
}

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