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.

1  
!7
2  
3  
concept Sentence {
4  
  S text;
5  
  S action;
6  
}
7  
8  
sclass Example {
9  
  L<S> tok;
10  
  int start, end;
11  
  
12  
  *() {}
13  
  *(L<S> *tok, IntRange subjectTokens) {
14  
    start = subjectTokens.start;
15  
    end = subjectTokens.end;
16  
  }
17  
}
18  
19  
sclass Result {
20  
  Example e;
21  
  IntRange answer;
22  
  bool correct;
23  
}
24  
25  
abstract sclass Guesser {
26  
  abstract IntRange getSubjectTokens(L<S> tok);
27  
  void learn(L<Example> material) {
28  
    for (Example e : material)
29  
      learn(e);
30  
  }
31  
  void learn(Example e) {}
32  
}
33  
34  
Guesser > G1 {
35  
  IntRange getSubjectTokens(L<S> tok) {
36  
    ret new IntRange(0, 1);
37  
  }
38  
}
39  
40  
Guesser > G2 {
41  
  new StringTree1 skipTree;
42  
  
43  
  IntRange getSubjectTokens(L<S> tok) {
44  
    int n = walkStringTreeToLeaf(skipTree, allToLower(tok));
45  
    if (n >= 0) ret new IntRange(n, n+1);
46  
    null;
47  
  }
48  
  
49  
  void learn(Example e) {
50  
    if (e.start > 0)
51  
      addToStringTree(skipTree, allToLower(takeFirst(e.tok, e.start)));
52  
  }
53  
}
54  
55  
sclass GuesserNode extends DefaultMutableTreeNode {
56  
  
57  
  *(S *text) { super(text); }
58  
}
59  
60  
p {
61  
  loadConceptsFrom(#1008607);
62  
  L<Example> material = learningMaterial();
63  
  pnlStruct(material);
64  
  
65  
  DefaultMutableTreeNode root = new("Guesser results");
66  
  checkGuesser(root, material, new G1);
67  
  checkGuesserAfterFullLearn(root, material, new G2);
68  
  
69  
  showFrame(jDynamicTree(new F1<DefaultMutableTreeNode, Bool>() {
70  
    Bool get(DefaultMutableTreeNode node) {
71  
      if (node.isRoot())
72  
        ret guess
73  
    }
74  
  }, root));
75  
}
76  
77  
sS checkGuesser(L<Example> testMaterial, Guesser g) {
78  
  int score = 0, n = 0;
79  
  for (Example e : testMaterial) {
80  
    IntRange r = cast pcall(g, "getSubjectTokens", e.tok);
81  
    if (eq(IntRange(e.start, e.end), r)) ++score;
82  
    ++n;
83  
  }
84  
  ret printScore(getClassName(g), score, n);
85  
}
86  
87  
svoid checkGuesserAfterFullLearn(DefaultMutableTreeNode root, L<Example> testMaterial, Guesser g) {
88  
  g.learn(testMaterial);
89  
  S s = checkGuesser(testMaterial, g);
90  
  root.add(new DefaultMutableTreeNode(s));
91  
}
92  
93  
static L<Example> learningMaterial() {
94  
  L<Example> out = new L;
95  
  for (Sentence s) {
96  
    if (s.action == null) continue;
97  
    IntRange r = ai_parseSubjectAction(s.action);
98  
    if (r != null) {
99  
      L<S> tok = codeTokens(nlTok5(s.text));
100  
      out.add(Example(tok, charRangeToTokenRange(tok, r)));
101  
    }
102  
  }
103  
  ret out;
104  
}

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: 543 / 662
Version history: 18 change(s)
Referenced in: [show references]