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

155
LINES

< > BotCompany Repo | #1021468 // Test Gazelle On Input v3 [with tree]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (18030L/101K).

1  
!7
2  
3  
set flag DynModule.
4  
5  
sclass GazelleTree {
6  
  L<GazelleTree> children;
7  
  GazelleTree parent;
8  
  double weight = 1, totalWeight;
9  
  S line, lineType;
10  
  S prediction, judgement;
11  
  RuleEngine2_MatchedRule mr;
12  
  
13  
  transient EvalContext ctx;
14  
15  
  *() {}
16  
  *(S *line) {}
17  
  
18  
  toString {
19  
    new LS l;
20  
    if (mr != null)
21  
      for (S s : unnull(mr.remainingConditions))
22  
        l.add("if: " + s);
23  
    addIfNempty(l, prediction);
24  
    if (mr != null)
25  
      l.add(mr.qualitySum() + " / " + formatDouble(mr.relativeQuality(), 2));
26  
    ret line + appendSquareBracketed(joinWithComma(l));
27  
  }
28  
}
29  
30  
sclass EvalContext {
31  
  RuleEngine2 engine;
32  
  Map<S, ai_gazelle_analyzeStatementsForRule_Data> dataByRule;
33  
}
34  
35  
cmodule TestRuleEngine2 > DynSCP {
36  
  S input, processedInput;
37  
  transient ReliableSingleThread rstCalc = dm_rstWithPostDelay(this, r calc, 500);
38  
  transient int maxResults = 500;
39  
  transient bool skipBadPredictions = true;
40  
  transient JTree jTree;
41  
  transient GazelleTree root;
42  
  transient double qualityCutOff = -0.75;
43  
  
44  
  S switchableFields() { ret "skipBadPredictions maxResults"; }
45  
  
46  
  start {
47  
    dm_vmBus_onMessage('gazelleRuleCreated, rstCalc);
48  
  }
49  
  
50  
  visualize {
51  
    jTree = jDynamicTree(null, func(GazelleTree tree) -> L<GazelleTree> { gt_getChildren(tree) }, makeChildrenIsFast := true);
52  
    
53  
    componentPopupMenu(jTree, voidfunc(JPopupMenu menu) {
54  
      MouseEvent evt = componentPopupMenu_getEvent();
55  
      TreePath path = jTree.getPathForLocation(evt.getX(), evt.getY());
56  
      if (path == null) ret;
57  
      DefaultMutableTreeNode node = cast path.getLastPathComponent();
58  
      final GazelleTree e = cast node.getUserObject();
59  
      jTree.setSelectionPath(path);
60  
      
61  
      print("Selected: " + e);
62  
      addMenuItem(menu, "Copy line", rThread { copyTextToClipboard(e.line) });
63  
      addMenuItem(menu, "Make rule...", rThread {
64  
        S s = e.line;
65  
        GazelleTree ee = e;
66  
        while ((ee = ee.parent) != null)
67  
          s = ee.line + "\n+ " + s;
68  
        dm_gazelle_newRuleDialog_2(s);
69  
      });
70  
    });
71  
    
72  
    JComponent tf = dm_textField('input);
73  
    onChangeAndNow(tf, rstCalc);
74  
    ret northCenterAndSouthWithMargins(
75  
      centerAndEastWithMargin(
76  
        tf,
77  
        jbutton("Make rule...", rThread { dm_gazelle_newRuleDialog_2(input) })),
78  
      super.visualize(),
79  
      rightAlignedButtons(
80  
        treeDependentButton(jTree, "Mark good", rMark('good)),
81  
        treeDependentButton(jTree, "Mark bad", rMark('bad)),
82  
        treeDependentButton(jTree, "Mark funny", rMark('funny))
83  
      ));
84  
  }
85  
  
86  
  visualize2 {
87  
    ret jscroll(jTree);
88  
  }
89  
  
90  
  Runnable rMark(fS judgement) {
91  
    ret rThread {
92  
      GazelleTree e = selected();
93  
      e.judgement = judgement;
94  
      jTree_fireUserObjectChanged(jTree, e);
95  
      saveEntry(e);
96  
    };
97  
  }
98  
  
99  
  GazelleTree selected() {
100  
    ret (GazelleTree) jtree_selectedUserObject(jTree);
101  
  }
102  
  
103  
  void calc {
104  
    S input = this.input;
105  
    root = new GazelleTree(input);
106  
    jTree_setRootObject(jTree, root);
107  
    print("Replaced root on " + isShowing(jTree) + " " + jTree);
108  
    jtree_collapseAndOpenRootNode(jTree);
109  
    processedInput = input;
110  
  }
111  
  
112  
  L<GazelleTree> gt_getChildren(GazelleTree tree) {
113  
    if (tree == null) null;
114  
    if (tree.children != null) ret tree.children;
115  
    
116  
    // make rule engine etc.
117  
    if (tree.ctx == null) {
118  
      new EvalContext ctx;
119  
      tree.ctx = ctx;
120  
      ctx.engine = new RuleEngine2;
121  
      ctx.engine.addRules2(dm_allRulesFromRulesModuleWithCommentsAndIDs());
122  
      
123  
      // gather data for predictor
124  
      MultiMap<S> statementsByRule = ai_gazelle_indexStatementsByRule(dm_gazelle_statementsFromAppliedRules());
125  
      ctx.dataByRule = mapValues ai_gazelle_analyzeStatementsForRule(multiMapToMap(statementsByRule));
126  
    }
127  
    EvalContext ctx = tree.ctx;
128  
    
129  
    new L<GazelleTree> children;
130  
    L<RuleEngine2_MatchedRule> l = sortByMethodDesc qualitySum(ai_ruleEngine2_rulesForInput_3(tree.ctx.engine, tree.line));
131  
    for (RuleEngine2_MatchedRule r : l) {
132  
      if (r.relativeQuality() < qualityCutOff) continue;
133  
      S line = r.applyMappingTo(r.rule.out);
134  
      
135  
      GazelleTree child = new(line);
136  
      child.parent = tree;
137  
      child.ctx = tree.ctx;
138  
      child.mr = r;
139  
      
140  
      ai_gazelle_analyzeStatementsForRule_Data data = ctx.dataByRule.get(r.rule.globalID);
141  
      child.prediction = data == null ? null : ai_gazelle_predictor1_onData(r, data);
142  
      children.add(child);
143  
    }
144  
    sortByCalculatedFieldDesc(children, func(GazelleTree c) -> int { ai_goodBadToInt(c.prediction) });
145  
    ret tree.children = children;
146  
  }
147  
  
148  
  void saveEntry(GazelleTree e) {
149  
    S modifiedRule = e.ctx.engine.formatForUser(e.mr.applyMapping().asText());
150  
    dm_gazelle_saveAppliedRule(
151  
      +modifiedRule,
152  
      judgement := e.judgement,
153  
      matchedRuleStruct := struct(e.mr));
154  
  }
155  
}

Author comment

Began life as a copy of #1021407

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021468
Snippet name: Test Gazelle On Input v3 [with tree]
Eternal ID of this version: #1021468/53
Text MD5: 6490435effc9436a27111a7e88356709
Transpilation MD5: ec09ba483958d3bfcb6e44879b372d20
Author: stefan
Category: javax / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-15 04:51:15
Source code size: 4943 bytes / 155 lines
Pitched / IR pitched: No / No
Views / Downloads: 312 / 841
Version history: 52 change(s)
Referenced in: [show references]