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

129
LINES

< > BotCompany Repo | #1008680 // Find Verb: Learner 1 [dev.]

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

Libraryless. Click here for Pure Java version (7859L/52K/178K).

!7

static Guesser best;
static double bestScore;

concept Sentence {
  S text;
  S action, verb;
}

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(#1008607);
  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.action == null) continue;
    L<IntRange> r = ai_parseVerbAction(s.verb);
    if (r != null) {
      L<S> tok = nlTok5(s.text);
      IntRange subject = ai_parseSubjectAction(s.action);
      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 L<IntRange> callGuesser(Guesser g, S sentence, IntRange subject) {
  L<S> tok = nlTok5(sentence);
  L<IntRange> r = g.getVerbTokens(new Input(codeTokens(tok),
    charRangeToCodeTokens(tok, subject)));
  if (r == null) null;
  ret codeTokenRangeToChars(tok, r);
}

Author comment

Began life as a copy of #1008669

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: #1008680
Snippet name: Find Verb: Learner 1 [dev.]
Eternal ID of this version: #1008680/13
Text MD5: c5d2f3c10d3251ab894175830e1d4544
Transpilation MD5: 44f3c6792d496a0ab1cbfefaf6c30685
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-28 18:29:33
Source code size: 3701 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 331 / 585
Version history: 12 change(s)
Referenced in: [show references]