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).

1  
sclass RuleEngine1 {
2  
  int minScore = 50; // minimal percentage score for rule matching
3  
  LPair<S> rules;
4  
  bool printMappings, printGroupedData, printRejectedVars;
5  
  Map<PairS, Set<S>> ruleVariables = new Map;
6  
  O preprocess = f id; // f ai_groupSimplestNounPhrases
7  
  
8  
  *() {}
9  
  *(S _rules) { addRules(_rules); }
10  
11  
  void addRules(S _rules) {
12  
    _rules = mapEachLine(preprocess, _rules);
13  
    if (printGroupedData) print(_rules);
14  
    rules = ai_findDoubleArrowRulesAsPairs(_rules);
15  
    for (PairS rule : rules)
16  
      ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
17  
  }
18  
  
19  
  void processInput(S input) {
20  
    print("\nInput: " + tok_dropCurlyBrackets(input));
21  
    temp tempIndent();
22  
    for (S in : interpretations(input)) {
23  
      print("\nInterpretation: " + formatForUser(in));
24  
      Set<S> out = litciset();
25  
      interpretRecursively(in, 5, out);
26  
    }
27  
  }
28  
29  
  void interpretRecursively(S input, int level, Set<S> out) {
30  
    if (level <= 0) ret;
31  
    LS interpretations = interpretations((S) callF(preprocess, input));
32  
    temp tempIndent();
33  
    for (S in : addAllAndReturnNew(out, interpretations)) {
34  
      print(" => " + formatForUser(in));
35  
      for (S s : lithashset(in, ai_superSimpleVerbCorrector(in)))
36  
        interpretRecursively(s, level-1, out);
37  
    }
38  
  }
39  
40  
  LS defaultParse(S s) {
41  
    ret codeTokens_lazy_uncurly(javaTokNPunctuationWithBrackets_cached(s));
42  
  }
43  
44  
  LS interpretations(S input) {
45  
    LS tokI = defaultParse(input);
46  
    //print("Raw parse: " + tokI);
47  
    new LS interpretations;
48  
      
49  
    for (PairS rule : rules) {
50  
      LS tokR = defaultParse(rule.a);
51  
      final SS map = zipTwoListsToCIMap_strict_withoutEquals(tokR, tokI);
52  
      if (map == null) continue;
53  
      
54  
      // Found matching rule
55  
      
56  
      int score = l(tokR)-l(map);
57  
      L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule)));
58  
      if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars);
59  
      if (nempty(nonVars)) score = 0;
60  
      int percentScore = ratioToIntPercent(score, l(tokR));
61  
      if (printMappings) print("  " + percentScore + "% " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a);
62  
      if (percentScore < minScore) continue;
63  
      
64  
      // Make consequence
65  
      
66  
      S c = rule.b;
67  
      c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
68  
      c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
69  
      //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c);
70  
      
71  
      interpretations.add(c);
72  
    }
73  
      
74  
    ret interpretations;
75  
  }
76  
77  
  S formatForUser(S s) {
78  
    ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s));
79  
  }
80  
}

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: 363 / 927
Version history: 14 change(s)
Referenced in: [show references]