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

80
LINES

< > BotCompany Repo | #1021346 // RuleEngine1

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9410L/67K/226K).

sclass RuleEngine1 {
  int minScore = 50; // minimal percentage score for rule matching
  LPair<S> rules;
  bool printMappings, printGroupedData, printRejectedVars;
  Map<PairS, Set<S>> ruleVariables = new Map;
  O preprocess = f id; // f ai_groupSimplestNounPhrases
  
  *() {}
  *(S _rules) { addRules(_rules); }

  void addRules(S _rules) {
    _rules = mapEachLine(preprocess, _rules);
    if (printGroupedData) print(_rules);
    rules = ai_findDoubleArrowRulesAsPairs(_rules);
    for (PairS rule : rules)
      ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
  }
  
  void processInput(S input) {
    print("\nInput: " + tok_dropCurlyBrackets(input));
    temp tempIndent();
    for (S in : interpretations(input)) {
      print("\nInterpretation: " + formatForUser(in));
      Set<S> out = litciset();
      interpretRecursively(in, 5, out);
    }
  }

  void interpretRecursively(S input, int level, Set<S> out) {
    if (level <= 0) ret;
    LS interpretations = interpretations((S) callF(preprocess, input));
    temp tempIndent();
    for (S in : addAllAndReturnNew(out, interpretations)) {
      print(" => " + formatForUser(in));
      for (S s : lithashset(in, ai_superSimpleVerbCorrector(in)))
        interpretRecursively(s, level-1, out);
    }
  }

  LS defaultParse(S s) {
    ret codeTokens_lazy_uncurly(javaTokNPunctuationWithBrackets_cached(s));
  }

  LS interpretations(S input) {
    LS tokI = defaultParse(input);
    //print("Raw parse: " + tokI);
    new LS interpretations;
      
    for (PairS rule : rules) {
      LS tokR = defaultParse(rule.a);
      final SS map = zipTwoListsToCIMap_strict_withoutEquals(tokR, tokI);
      if (map == null) continue;
      
      // Found matching rule
      
      int score = l(tokR)-l(map);
      L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule)));
      if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars);
      if (nempty(nonVars)) score = 0;
      int percentScore = ratioToIntPercent(score, l(tokR));
      if (printMappings) print("  " + percentScore + "% " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a);
      if (percentScore < minScore) continue;
      
      // Make consequence
      
      S c = rule.b;
      c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
      c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
      //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c);
      
      interpretations.add(c);
    }
      
    ret interpretations;
  }

  S formatForUser(S s) {
    ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s));
  }
}

Author comment

Began life as a copy of #1021251

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021346
Snippet name: RuleEngine1
Eternal ID of this version: #1021346/15
Text MD5: 6200663c8e4eced3ad30b9dc7ce523b0
Transpilation MD5: 23e53a1ed3faea754d55ec048907ee87
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-10 20:01:04
Source code size: 2824 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 257 / 775
Version history: 14 change(s)
Referenced in: [show references]