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

50
LINES

< > BotCompany Repo | #1023173 // WordTokRule

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3789L/24K).

1  
abstract sclass WordTokRule extends Attractor implements RuleOnTok, RuleOnString, RuleOnExtTok, RuleOnExtS {
2  
  S pattern; // example sentence
3  
  Set<S> vars; // list of the variable words in the sentence
4  
  bool debug;
5  
6  
  transient LS tokPat; // parsed pattern
7  
  transient SS map; // not null if there is a match
8  
  
9  
  *() {}
10  
  *(S *pattern, S... vars) { this.vars = litciset(vars); }
11  
  *(S *pattern, Set<S> *vars) {}
12  
  
13  
  LS tokPat() {
14  
    if (tokPat == null) tokPat = wordTok(pattern);
15  
    ret tokPat;
16  
  }
17  
  
18  
  public final bool matches(ExtS s) {
19  
    process(s);
20  
    ret matched();
21  
  }
22  
  
23  
  public final bool matches(S s) {
24  
    process(s);
25  
    ret matched();
26  
  }
27  
  
28  
  public void process(S s) {
29  
    process(ext(s));
30  
  }
31  
  
32  
  public void process(ExtS s) {
33  
    processExtTok(extLift(wordTok(s!), s));
34  
  }
35  
  
36  
  public void processTok(LS tok) {
37  
    processExtTok(ext(tok));
38  
  }
39  
  
40  
  public void processExtTok(Ext<LS> tok) {
41  
    if (!preCheck(tok)) ret;
42  
    map = nullIfContainsKeysOtherThan(gazelle_zipTwoTokenizationsToMap(tokPat(), tok!), vars);
43  
    if (debug) print(pattern + " + " + join(tok!) + " => " + map);
44  
  }
45  
  
46  
  bool preCheck(DynamicObject o) { true; }
47  
  
48  
  bool matched() { ret map != null; }
49  
  S getVar(S name) { ret getOrKeep(map, name); }
50  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023173
Snippet name: WordTokRule
Eternal ID of this version: #1023173/23
Text MD5: af680d66a7bd4ac1c41c0e2684d516ea
Transpilation MD5: 70ef83d2a316da7edf529193800f9c89
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-27 14:18:59
Source code size: 1300 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 326 / 835
Version history: 22 change(s)
Referenced in: [show references]