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

165
LINES

< > BotCompany Repo | #1021287 // "Pizza with..." [modified rules]

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

Download Jar. Libraryless. Click here for Pure Java version (12646L/92K).

!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
  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;
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));
  LS rulesRaw = ai_splitEntriesWithMultipleDoubleArrows(splitAtEmptyLines(sections.get("Rules")));
  rules = mapNonNulls splitAtDoubleArrow_pair(rulesRaw);
  for (PairS rule : rules)
    ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
  //pnlStruct(ruleVariables);
  
  printAsciiHeading("REASONING");
  new MultiMap<S> solutions;

  for (S input : tlft(sections.get("Input"))) {
    print("\nInput: " + tok_dropCurlyBrackets(input));
    temp tempIndent();
    LPair<S, Int> interpretations = interpretations(input);
    for (Pair<S, Int> in : interpretations) {
      print();
      printInterpretations(ll(in), "Interpretation: ");
      Set<S> out = litciset();
      interpretRecursively(in.a, 5, false, 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, bool newLines, Set<S> out) {
  if (level <= 0) ret;
  input = ai_groupSimplestNounPhrases(input);
  L<Pair<S, Int>> interpretations = interpretations(input);
  addAll(out, pairsA(interpretations));
  temp tempIndent();
  for (Pair<S, Int> in : interpretations) {
    if (newLines) print();
    printInterpretations(ll(in), newLines ? "Interpretation: " : " => ");
    S corrected = ai_superSimpleVerbCorrector(in.a);
    interpretRecursively(in.a, level-1, false, out);
    if (neq(corrected, in.a))
      interpretRecursively(corrected, level-1, false, out);
  }
}

svoid printInterpretations(LPair<S, Int> interpretations, S prefix) {
  for (Pair<S, Int> p : interpretations)
    print(prefix + formatForUser(p.a));
}

static LS defaultParse(S s) {
  ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s));
}

static LPair<S, Int> interpretations(S input) {
  new MultiSet<PairS> ms;
  LS tokI = defaultParse(input);
  //print("Raw parse: " + tokI);
  new L<Pair<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;
    ms.add(rule, score+1);
    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) }));
    // TODO: multiple grouping levels
    c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
    
    int percent = ratioToIntPercent(score, l(tokR));
    if (percent >= minScore)
      addPair(interpretations, c, percent);
  }
    
  ret sortByPairBDesc(interpretations);
}

sS 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: #1021287
Snippet name: "Pizza with..." [modified rules]
Eternal ID of this version: #1021287/1
Text MD5: 0cb474ee043a2895a1d293cb17f9e029
Transpilation MD5: 2e15e6b6b5ed861b6fe5205f00c06360
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-06 20:43:41
Source code size: 4828 bytes / 165 lines
Pitched / IR pitched: No / No
Views / Downloads: 472 / 1169
Referenced in: [show references]