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

57
LINES

< > BotCompany Repo | #1004027 // Adapter + WordAdapter

JavaX fragment (include)

// adapters are immutable! (plus returns a modified clone)
abstract sclass Adapter {
  abstract bool canMatch(S in, S out);
  Adapter plus(S in, S out) { ret this; }
  S get(S in) { ret in; }
  double size() { ret 0; }
}

sclass WordAdapter extends Adapter {
  new Map<S, S> wordMap;
  
  L<S> tok(S s) {
    ret nlTok2(dropPunctuation2(s));
  }
  
  bool canMatch(S in, S out) {
    L<S> t1 = tok(in), t2 = tok(out);
    ret l(t1) == l(t2);
  }
  
  Adapter plus(S in, S out) {
    L<S> t1 = tok(in), t2 = tok(out);
    if (l(t1) != l(t2)) ret this;
    
    WordAdapter a = cast nuObject(getClass());
    a.wordMap = cloneMap(wordMap);
    for (int i = 1; i < l(t1); i += 2) {
      S w1 = t1.get(i), w2 = t2.get(i);
      if (!eqic(w1, w2))
        // just overwrite - be flexible!
        a.wordMap.put(w1.toLowerCase(), w2.toLowerCase());
    }
    ret a;
  }
  
  S get(S s) {
    L<S> tok = nlTok2(s);
    for (int i = 1; i < l(tok); i += 2) {
      S w = lookupToken(tok.get(i));
      if (nempty(w))
        tok.set(i, w);
    }
    ret join(tok);
  }
  
  S lookupToken(S s) {
    ret wordMap.get(s.toLowerCase());
  }
  
  double size() {
    ret l(wordMap);
  }
  
  public S toString() {
    ret structure(wordMap);
  }
}

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: #1004027
Snippet name: Adapter + WordAdapter
Eternal ID of this version: #1004027/1
Text MD5: 278479dee51a424e20e3280aedb02d58
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-04 02:25:06
Source code size: 1292 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 630 / 1357
Referenced in: #1004022 - Generators for Random v8
#1004028 - Test Adapter
#1004039 - Generators for Random v9