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

53
LINES

< > BotCompany Repo | #1002227 // Microtheory Bot (aliases)

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

Libraryless. Click here for Pure Java version (595L/5K/15K).

!752

//static new Set<S> things;
static new L<S> sentences;
static Map<S, L<S>> rewrites = new TreeMap; // thing -> cluster

p {
  addRewrite("john", "johnnie");
  addRewrite("klaus", "klausi");
  assertTrue(isThing("john"));
  assertTrue(isThing("klausi"));
  assertFalse(isThing("jack"));
  print("all ok");
}

static void addThing(S a) {
  addRewrite(a, a);
}

static void addRewrite(S a, S b) {
  if (eq(a, b)) {
    L<S> cluster = rewrites.get(a);
    if (cluster == null)
      rewrites.put(a, litlist(a));
    ret;
  }
  
  L<S> clusterA = rewrites.get(a);
  L<S> clusterB = rewrites.get(b);
  if (clusterA == null && clusterB == null) {
    L<S> cluster = litlist(a, b);
    rewrites.put(a, cluster);
    rewrites.put(b, cluster);
  } else if (clusterA == null) {
    clusterB.add(a);
    rewrites.put(a, clusterB);
  } else if (clusterB == null) {
    clusterA.add(b);
    rewrites.put(b, clusterA);
  } else if (clusterA != clusterB)
    mergeClusters(clusterA, clusterB);
}

static void mergeClusters(L<S> a, L<S> b) {
  for (S x : b) {
    a.add(x);
    rewrites.put(x, a);
  }
}

static boolean isThing(S a) {
  ret rewrites.containsKey(a);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002227
Snippet name: Microtheory Bot (aliases)
Eternal ID of this version: #1002227/1
Text MD5: 759bc5dcb881103af922d8c013bd4631
Transpilation MD5: 1ca8d1053316aea24988c27ea468a387
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-30 01:28:22
Source code size: 1208 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 586 / 640
Referenced in: #3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)