Download Jar. Libraryless. Click here for Pure Java version (12443L/91K).
!7 sS data = [[ ---- Rules // Some reasonings that everybody will understand. // Sorry for the curly braces, we have to help out the parser a tiny bit. // First, the 3 different cases of what "pizza with..." can mean. I ate pizza with pepperoni. => {I ate pizza} and {the pizza had pepperoni on it}. I ate pizza with Bob. => {I ate pizza} and {Bob was with me}. I ate pizza with a fork. => I used a fork to eat pizza. // Now some more easy rules. I used a fork to eat pizza. => I used a fork. I used a fork. => A fork is a tool. The pizza had pepperoni on it. => Pepperoni is edible. Bob was with me. => Bob is a person. // Some VERY basic mathematical logic $A and $B. => $A. $A and $B. => $B. // Tell the machine what is not plausible Mom is edible. => fail Mom is a tool. => fail anchovis are a tool. => fail anchovis are a person. => fail ducks are a tool. => fail ducks are a person. => fail my hands are edible. => fail my hands are a person. => fail // That's it! Now for the input. ---- Input I ate pizza with mom. I ate pizza with anchovis. I ate pizza with ducks. I ate pizza with my hands. ]]; static int minScore = 50; // minimal percentage score for rule matching sbool printMappings, printGroupedData, printRejectedVars; static LPair<S> rules; static Map<PairS, Set<S>> ruleVariables = new Map; p-exp { centerBigTTConsole(); S data = mapEachLine ai_groupSimplestNounPhrases(main.data); if (printGroupedData) print(data); SS sections = asCIMap(minusSignsSections(data)); rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules")); for (PairS rule : rules) ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); printAsciiHeading("REASONING"); new MultiMap<S> solutions; for (S input : tlft(sections.get("Input"))) { print("\nInput: " + tok_dropCurlyBrackets(input)); temp tempIndent(); for (Pair<S, Int> in : interpretations(input)) { print("\nInterpretation: " + formatForUser(in.a)); Set<S> out = litciset(); interpretRecursively(in.a, 5, out); if (!matchAny("fail", out)) solutions.put(input, in.a); } } printAsciiHeading("RESULTS"); print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth formatForUser(multiMapToPairs(solutions)))); } svoid interpretRecursively(S input, int level, Set<S> out) { if (level <= 0) ret; LS interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(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); } } static LS defaultParse(S s) { ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)); } static LPair<S, Int> interpretations(S input) { LS tokI = defaultParse(input); //print("Raw parse: " + tokI); new LPair<S, Int> interpretations; for (PairS rule : rules) { LS tokR = defaultParse(rule.a); final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(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; if (printMappings) print(" " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a); // 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); addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore); } ret interpretations; } sS formatForUser(S s) { ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)); }
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: | 909 / 3070 |
Version history: | 136 change(s) |
Referenced in: | [show references] |