Libraryless. Click here for Pure Java version (7920L/52K/179K).
!7 static Guesser best; static double bestScore; sS key = "verb"; concept Sentence { S text; SS data; S get(S s) { ret data.get(s); } S subject() { ret get("subject"); } S verb() { ret get(key); } } sclass Input { L<S> tok; IntRange subject; *() {} *(L<S> *tok, IntRange *subject) {} } Input > Example { new L<IntRange> verbs; *() {} *(L<S> *tok, IntRange *subject, L<IntRange> *verbs) {} toString { ret quote(joinWithSpaces(tok)) + " => " + map(verbs, func(IntRange r) { joinWithSpaces(subList(tok, r.start, r.end)) }); } } abstract sclass GuesserBase { void learn(L<Example> material) { for (Example e : material) learn(e); } void learn(Example e) {} } abstract sclass Guesser extends GuesserBase { abstract L<IntRange> getVerbTokens(Input input); } Guesser > GWordAfterSubject { L<IntRange> getVerbTokens(Input input) { IntRange r = input.subject; ret r == null ? null : ll(intRange(r.end, r.end+1)); } } p { loadConceptsFrom(#1008692); L<Example> material = learningMaterial(); pnlStruct(material); // This yields the empty learner Pair<Guesser, Double> p = bestLearner(material, ll(new GWordAfterSubject), 50, 3, true); // Now we train it with all data for in-program use if (p.a != null) p.a.learn(material); // Print and store print("Best learner: " + formatDouble(p.b, 1) + "% - " + struct(p.a)); best = p.a; bestScore = p.b; } sbool printDetails, printSuccesses; static double checkGuesser(L<Example> testMaterial, Guesser g) { print(); int score = 0, n = 0; for (final Example e : testMaterial) { L<IntRange> r = cast pcall(g, "getVerbTokens", e.tok); bool ok = eq(r, e.verbs); if (ok) ++score; ++n; if (printDetails || ok && printSuccesses) if (ok) print("OK " + e); else print("FAIL " + (r == null ? "-" : map(r, func(IntRange r) { joinWithSpaces(subList(e.tok, r)) })) + " for " + e); } printScore(shortClassName(g), score, n); ret ratioToPercent(score, n); } static double checkGuesserAfterRandomizedPartialLearn(L<Example> testMaterial, Guesser g, double percentToLearn, bool hardMode) { Pair<L<Example>> p = getRandomPercent2(testMaterial, percentToLearn); g.learn(p.a); ret checkGuesser(hardMode ? p.b : testMaterial, g); } // best learner with randomized x% training material // returns guesser, percentage solved // hardMode = only count scores on untrained examples static Pair<Guesser, Double> bestLearner(final L<Example> material, L<? extends Guesser> guessers, final double percent, int repetitions, final bool hardMode) { new Best<Guesser> best; for (final Guesser g : guessers) best.put(g, repeatAndAdd_double(repetitions, func { checkGuesserAfterRandomizedPartialLearn(material, cloneObject(g), percent, hardMode) })/repetitions); ret best.pair(); } static L<Example> learningMaterial() { L<Example> out = new L; for (Sentence s) { if (s.verb() == null) continue; L<IntRange> r = ai_parseVerbAction(s.verb()); if (r != null) { L<S> tok = nlTok5(s.text); IntRange subject = ai_parseSubjectAction(s.subject()); subject = charRangeToCodeTokens(tok, subject); r = charRangeToCodeTokens(tok, r); tok = codeTokens(tok); out.add(Example(tok, subject, r)); } } ret out; } // to be called from applications - works on character level static void callGuesser(Guesser g, S sentence, SS data) { L<S> tok = nlTok5(sentence); L<IntRange> r = g.getVerbTokens(new Input(codeTokens(tok), charRangeToCodeTokens(tok, ai_parseAction(data.get("subject"))))); if (r == null) ret; data.put(key, ai_renderAction(sentence, codeTokenRangeToChars(tok, first/*XX*/(r)))); }
Began life as a copy of #1008680
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: | #1008704 |
Snippet name: | Find Verb (map version): Learner 1 [dev.] |
Eternal ID of this version: | #1008704/12 |
Text MD5: | 71a2659f6c36ebc9cd7b87b20cfcf0de |
Transpilation MD5: | 3bebdae0e1e24d24e2ebde13aa918b82 |
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-30 17:07:27 |
Source code size: | 3887 bytes / 134 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 433 / 837 |
Version history: | 11 change(s) |
Referenced in: | [show references] |