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

104
LINES

< > BotCompany Repo | #1008643 // Learn parsing sentences with JTree [dev.]

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

Transpiled version (8015L) is out of date.

!7

concept Sentence {
  S text;
  S action;
}

sclass Example {
  L<S> tok;
  int start, end;
  
  *() {}
  *(L<S> *tok, IntRange subjectTokens) {
    start = subjectTokens.start;
    end = subjectTokens.end;
  }
}

sclass Result {
  Example e;
  IntRange answer;
  bool correct;
}

abstract sclass Guesser {
  abstract IntRange getSubjectTokens(L<S> tok);
  void learn(L<Example> material) {
    for (Example e : material)
      learn(e);
  }
  void learn(Example e) {}
}

Guesser > G1 {
  IntRange getSubjectTokens(L<S> tok) {
    ret new IntRange(0, 1);
  }
}

Guesser > G2 {
  new StringTree1 skipTree;
  
  IntRange getSubjectTokens(L<S> tok) {
    int n = walkStringTreeToLeaf(skipTree, allToLower(tok));
    if (n >= 0) ret new IntRange(n, n+1);
    null;
  }
  
  void learn(Example e) {
    if (e.start > 0)
      addToStringTree(skipTree, allToLower(takeFirst(e.tok, e.start)));
  }
}

sclass GuesserNode extends DefaultMutableTreeNode {
  
  *(S *text) { super(text); }
}

p {
  loadConceptsFrom(#1008607);
  L<Example> material = learningMaterial();
  pnlStruct(material);
  
  DefaultMutableTreeNode root = new("Guesser results");
  checkGuesser(root, material, new G1);
  checkGuesserAfterFullLearn(root, material, new G2);
  
  showFrame(jDynamicTree(new F1<DefaultMutableTreeNode, Bool>() {
    Bool get(DefaultMutableTreeNode node) {
      if (node.isRoot())
        ret guess
    }
  }, root));
}

sS checkGuesser(L<Example> testMaterial, Guesser g) {
  int score = 0, n = 0;
  for (Example e : testMaterial) {
    IntRange r = cast pcall(g, "getSubjectTokens", e.tok);
    if (eq(IntRange(e.start, e.end), r)) ++score;
    ++n;
  }
  ret printScore(getClassName(g), score, n);
}

svoid checkGuesserAfterFullLearn(DefaultMutableTreeNode root, L<Example> testMaterial, Guesser g) {
  g.learn(testMaterial);
  S s = checkGuesser(testMaterial, g);
  root.add(new DefaultMutableTreeNode(s));
}

static L<Example> learningMaterial() {
  L<Example> out = new L;
  for (Sentence s) {
    if (s.action == null) continue;
    IntRange r = ai_parseSubjectAction(s.action);
    if (r != null) {
      L<S> tok = codeTokens(nlTok5(s.text));
      out.add(Example(tok, charRangeToTokenRange(tok, r)));
    }
  }
  ret out;
}

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: #1008643
Snippet name: Learn parsing sentences with JTree [dev.]
Eternal ID of this version: #1008643/19
Text MD5: 5555fe0f53e2239f97e3f98fc757c60a
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-26 19:56:05
Source code size: 2333 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 552 / 672
Version history: 18 change(s)
Referenced in: [show references]