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

199
LINES

< > BotCompany Repo | #1008669 // Find Subject: Learner 1 [OK]

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

Libraryless. Click here for Pure Java version (8129L/53K/182K).

!7

static Guesser best;
static double bestScore;

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;
  }
  
  toString {
    ret quote(joinWithSpaces(tok)) + " => " + joinWithSpaces(subList(tok, start, 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 IntRange getSubjectTokens(L<S> tok);
}

Guesser > GLengthOfSubject {
  new MultiSet<S> pos; // words to end on
  new MultiSet<S> neg; // words not to end on
  
  IntRange getSubjectTokens(L<S> tok) {
    ret getSubjectTokens(tok, 0);
  }
  
  IntRange getSubjectTokens(L<S> tok, int startAt) {
    int i = startAt;
    while (i < l(tok)) {
      S t = lower(tok.get(i));
      if (pos.get(t) <= neg.get(t)) // also stop if unknown word
        break;
      ++i;
    }
    ret intRange(startAt, min(l(tok), i+1));
  }
  
  void learn(Example e) {
    L<S> subjectTokens = allToLower(subList(e.tok, e.start, e.end));
    for (S word : dropLast(subjectTokens))
      pos.add(word);
    addIfNotNull(neg, last(subjectTokens));
  }
}

Guesser > GSkip1 { // returns first word or second word
  new MultiSet<S> pos; // words to skip
  new MultiSet<S> neg; // words not to skip
  
  void learn(Example e) {
    (e.start > 0 ? pos : neg).add(lower(first(e.tok));
  }
  
  IntRange getSubjectTokens(L<S> tok) {
    S t = lower(first(tok));
    ret intRangeFromStartAndLength(pos.get(t) > neg.get(t) ? 1 : 0, 1);
  }
}

Guesser > GSkip2 { // can skip multiple words
  new MultiSet<S> pos; // words to skip
  new MultiSet<S> neg; // words not to skip
  
  void learn(Example e) {
    (e.start > 0 ? pos : neg).add(lower(first(e.tok));
  }
  
  IntRange getSubjectTokens(L<S> tok) {
    int i = 0;
    while (i < l(tok)) {
      S t = lower(tok.get(i));
      if (pos.get(t) <= neg.get(t)) // also stop if unknown word
        break;
      ++i;
    }
    ret intRangeFromStartAndLength(i, i+1);
  }
}

Guesser > GCombine {
  Guesser a;
  new GLengthOfSubject b;
  
  *() {}
  *(Guesser *a) {}
  
  IntRange getSubjectTokens(L<S> tok) {
    IntRange r = a.getSubjectTokens(tok);
    int skip = r == null ? 0 : r.start;
    ret b.getSubjectTokens(tok, skip);
  }
  
  void learn(L<Example> material) {
    a.learn(material);
    b.learn(material);
  }  
}

p {
  loadConceptsFrom(#1008607);
  L<Example> material = learningMaterial();
  //pnlStruct(material);
  
  // This yields the empty learner
  Pair<Guesser, Double> p = bestLearner(material, 
    //ll(new GSkip1),
    ll(new GCombine(new GSkip1), new GCombine(new GSkip2)),
    50, 3, true);
    
  // Now we train it with all data for in-program use
  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 (Example e : testMaterial) {
    IntRange r = cast pcall(g, "getSubjectTokens", e.tok);
    bool ok = eq(IntRange(e.start, e.end), r);
    if (ok) ++score;
    ++n;
    if (printDetails || ok && printSuccesses)
      if (ok)
        print("OK " + e);
      else
        print("FAIL " + (r == null ? "-" : joinWithSpaces(subList(e.tok, r.start, r.end))) + " 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;
    IntRange r = ai_parseSubjectAction(s.action);
    if (r != null) {
      L<S> tok = nlTok5(s.text);
      r = charRangeToTokenRange(tok, r);
      r = tokenRangeToCodeTokens(r);
      tok = codeTokens(tok);
      out.add(Example(tok, r));
    }
  }
  ret out;
}

// to be called from applications - works on character level
static IntRange callGuesser(Guesser g, S sentence) {
  L<S> tok = nlTok5(sentence);
  IntRange r = g.getSubjectTokens(codeTokens(tok));
  if (r == null) null;
  r = tokenRangeFromCodeTokens(r);
  ret tokenRangeToCharRange(tok, r);
}

Author comment

Began life as a copy of #1008653

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lhdilzshxjzv, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008669
Snippet name: Find Subject: Learner 1 [OK]
Eternal ID of this version: #1008669/18
Text MD5: 8ef34e0ae82bd5f37e3395c731f68a1d
Transpilation MD5: 14552dca52c8321a133cb3f6fe8b2ec4
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 16:30:44
Source code size: 5333 bytes / 199 lines
Pitched / IR pitched: No / No
Views / Downloads: 531 / 992
Version history: 17 change(s)
Referenced in: [show references]