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

99
LINES

< > BotCompany Repo | #1009474 // RelBot

JavaX fragment (include)

sclass RelBot_Rel {
  S relation;
  L<S> things;
  
  [stdEq]
}

sclass RelBot {
  L<S> categories, things;
  L<Pair<S>> goodOpposites;
  L<Pair<S>> whatIsWhat, whatIsntWhat;
  L<S> relationNames;
  L<RelBot_Rel> relations;
  Map<S, L<S>> identities;

  *(ILister l) {
    categories = l.getList("Categories");
    goodOpposites = l.getList("Good opposites");
    whatIsWhat = l.getList("What is what");
    whatIsntWhat = l.getList("What is not what");
    things = l.getList("Things");
    relations = l.getList("Relations");
    identities = l.getTreeMap("Identities");
  }

  S cat(S s) { setAdd(categories, s); ret s; }

  S answer(S s) {
    new Matches m;
    if "what is *" {
      S w = $1;
      L<S> a = pairList_forwardLookup(whatIsWhat, w);
      L<S> b = pairList_forwardLookup(whatIsntWhat, w);
      L<S> c = pairList_backwardLookup(whatIsWhat, w);
      L<S> d = pairList_backwardLookup(whatIsntWhat, w);
      new L<S> l;
      if (nempty(a)) l.add(w + " " + isAre(a) + " " + textOut_and(a));
      if (nempty(b)) l.add(w + " " + isAre(b) + " not " + textOut_and(b));
      if (nempty(c)) l.add(textOut_and(c) + " " + isAre(c) + w);
      if (nempty(d)) l.add(textOut_and(d) + " " + isAre(d) + " not " + w);
      ret lines(l);
    }
    
    if "cat *" { setAdd(categories, $1); ret "OK, " + n(categories, "category"); }
    if "del cat *" { categories.remove($1); ret "OK, " + n(categories, "category"); }
    if "categories" ret cloneStruct(categories);
    
    if "good opposite * *" { setAdd(goodOpposites, pair(cat($1), cat($2))); ret "OK, " + n(goodOpposites, "good opposite"); }
    if "del good opposite * *" { goodOpposites.remove(pair($1, $2)); ret "OK, " + n(goodOpposites, "good opposite"); }
    if "good opposites" ret cloneStruct(goodOpposites);
    
    if "thing *" { setAdd(things, $1); ret "OK, " + n(things, "thing"); }
    if "things" ret cloneStruct(things);
    
    if "* is *" {
      setAdd_remove(whatIsWhat, whatIsntWhat, pair($1, $2));
      ret "OK, " + n(whatIsWhat, " what is whats");
    }
    if "* is not *|* isn't *" {
      setAdd_remove(whatIsntWhat, whatIsWhat, pair($1, $2));
      ret "OK, " + n(whatIsWhat, " what isn't whats");
    }
    if "forget * isn't *" {
      remove(whatIsntWhat, pair($1, $2));
      ret "OK, " + n(whatIsntWhat, " what isn't whats");
    }
    if "collect things" { collectThings(); ret "OK, " + n(things, 'thing); }
    
    if "relation names" ret sfu(relationNames);
    if "relations" ret lines(map(func(RelBot_Rel r) { relationToText(r) }, relations));
    
    if "rel * ..." {
      RelBot_Rel rel = nu(RelBot_Rel, relation := $1, things := javaTokC(m.rest()));
      setAdd(relations, rel);
      ret "OK: " + relationToText(rel);
    }
    if "forget rel * ..." {
      RelBot_Rel rel = nu(RelBot_Rel, relation := $1, things := javaTokC(m.rest()));
      remove(relations, rel);
      ret "OK, " + n(relations, "relation");
    }
    
    if "identity ..." {
      L<S> l = javaTokC(m.rest();
      L<S> cluster = clusters_add(identities, l);
      ret "OK, new cluster: " + sfu(cluster);
    }
    
    null;
  }

  void collectThings {
    setAddAllQuick(things, flattenPairs(concatLists(whatIsWhat, whatIsntWhat)));
  }
  
  S relationToText(RelBot_Rel r) {
    ret quoteIfNotIdentifier(r.relation) + "(" + join(", ", r.things) + ")";
  }
}

Author comment

Began life as a copy of #1009469

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: #1009474
Snippet name: RelBot
Eternal ID of this version: #1009474/7
Text MD5: 9cfeeee7fa2aa01dccf4fada9b5c8c40
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-04 16:45:43
Source code size: 3440 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 504 / 1097
Version history: 6 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)