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

147
LINES

< > BotCompany Repo | #1021251 // "Pizza with..." [sentence disambiguation logic engine demo, works]

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

Download Jar. Libraryless. Click here for Pure Java version (12443L/91K).

1  
!7
2  
3  
sS data = [[
4  
5  
---- Rules
6  
7  
// Some reasonings that everybody will understand.
8  
// Sorry for the curly braces, we have to help out the parser a tiny bit.
9  
// First, the 3 different cases of what "pizza with..." can mean.
10  
11  
  I ate pizza with pepperoni.
12  
  => {I ate pizza} and {the pizza had pepperoni on it}.
13  
  
14  
  I ate pizza with Bob.
15  
  => {I ate pizza} and {Bob was with me}.
16  
  
17  
  I ate pizza with a fork.
18  
  => I used a fork to eat pizza.
19  
  
20  
// Now some more easy rules.
21  
  
22  
  I used a fork to eat pizza.
23  
  => I used a fork.
24  
  
25  
  I used a fork.
26  
  => A fork is a tool.
27  
  
28  
  The pizza had pepperoni on it.
29  
  => Pepperoni is edible.
30  
  
31  
  Bob was with me.
32  
  => Bob is a person.
33  
34  
// Some VERY basic mathematical logic
35  
36  
  $A and $B.
37  
  => $A.
38  
  
39  
  $A and $B.
40  
  => $B.
41  
42  
// Tell the machine what is not plausible
43  
44  
  Mom is edible. => fail
45  
  Mom is a tool. => fail
46  
  anchovis are a tool. => fail
47  
  anchovis are a person. => fail
48  
  ducks are a tool. => fail
49  
  ducks are a person. => fail
50  
  my hands are edible. => fail
51  
  my hands are a person. => fail
52  
53  
// That's it! Now for the input.
54  
55  
---- Input
56  
57  
  I ate pizza with mom.
58  
  I ate pizza with anchovis.
59  
  I ate pizza with ducks.
60  
  I ate pizza with my hands.
61  
62  
]];
63  
64  
static int minScore = 50; // minimal percentage score for rule matching
65  
sbool printMappings, printGroupedData, printRejectedVars;
66  
67  
static LPair<S> rules;
68  
static Map<PairS, Set<S>> ruleVariables = new Map;
69  
70  
p-exp {
71  
  centerBigTTConsole();
72  
  
73  
  S data = mapEachLine ai_groupSimplestNounPhrases(main.data);
74  
  if (printGroupedData) print(data);
75  
  SS sections = asCIMap(minusSignsSections(data));
76  
  rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules"));
77  
  for (PairS rule : rules)
78  
    ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
79  
80  
  printAsciiHeading("REASONING");
81  
  new MultiMap<S> solutions;
82  
83  
  for (S input : tlft(sections.get("Input"))) {
84  
    print("\nInput: " + tok_dropCurlyBrackets(input));
85  
    temp tempIndent();
86  
    for (Pair<S, Int> in : interpretations(input)) {
87  
      print("\nInterpretation: " + formatForUser(in.a));
88  
      Set<S> out = litciset();
89  
      interpretRecursively(in.a, 5, out);
90  
      if (!matchAny("fail", out))
91  
        solutions.put(input, in.a);
92  
    }
93  
  }
94  
  
95  
  printAsciiHeading("RESULTS");
96  
  print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth formatForUser(multiMapToPairs(solutions))));
97  
}
98  
99  
svoid interpretRecursively(S input, int level, Set<S> out) {
100  
  if (level <= 0) ret;
101  
  LS interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(input)));
102  
  temp tempIndent();
103  
  for (S in : addAllAndReturnNew(out, interpretations)) {
104  
    print(" => " + formatForUser(in));
105  
    for (S s : lithashset(in, ai_superSimpleVerbCorrector(in)))
106  
      interpretRecursively(s, level-1, out);
107  
  }
108  
}
109  
110  
static LS defaultParse(S s) {
111  
  ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s));
112  
}
113  
114  
static LPair<S, Int> interpretations(S input) {
115  
  LS tokI = defaultParse(input);
116  
  //print("Raw parse: " + tokI);
117  
  new LPair<S, Int> interpretations;
118  
    
119  
  for (PairS rule : rules) {
120  
    LS tokR = defaultParse(rule.a);
121  
    final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI));
122  
    if (map == null) continue;
123  
    
124  
    // Found matching rule
125  
    
126  
    int score = l(tokR)-l(map);
127  
    L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule)));
128  
    if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars);
129  
    if (nempty(nonVars)) score = 0;
130  
    if (printMappings) print("  " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a);
131  
    
132  
    // Make consequence
133  
    
134  
    S c = rule.b;
135  
    c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
136  
    c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
137  
    //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c);
138  
    
139  
    addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore);
140  
  }
141  
    
142  
  ret interpretations;
143  
}
144  
145  
sS formatForUser(S s) {
146  
  ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s));
147  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021251
Snippet name: "Pizza with..." [sentence disambiguation logic engine demo, works]
Eternal ID of this version: #1021251/137
Text MD5: a3b2bb9ed62d3a03eb2dc6a203e77b41
Transpilation MD5: 218d438616b499619b081a5b88bf9b99
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-07 16:50:54
Source code size: 4264 bytes / 147 lines
Pitched / IR pitched: No / No
Views / Downloads: 794 / 2750
Version history: 136 change(s)
Referenced in: [show references]