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

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  
  my hands are edible. => fail
50  
  my hands are a person. => fail
51  
52  
// That's it! Now for the input.
53  
54  
---- Input
55  
56  
  I ate pizza with mom.
57  
  I ate pizza with anchovis.
58  
  I ate pizza with ducks.
59  
  I ate pizza with my hands.
60  
61  
]];
62  
63  
static int minScore = 50;
64  
sbool printMappings, printGroupedData, printRejectedVars;
65  
66  
static LPair<S> rules;
67  
static Map<PairS, Set<S>> ruleVariables = new Map;
68  
69  
p-exp {
70  
  centerBigTTConsole();
71  
  
72  
  S data = mapEachLine ai_groupSimplestNounPhrases(main.data);
73  
  if (printGroupedData) print(data);
74  
  SS sections = asCIMap(minusSignsSections(data));
75  
  LS rulesRaw = ai_splitEntriesWithMultipleDoubleArrows(splitAtEmptyLines(sections.get("Rules")));
76  
  rules = mapNonNulls splitAtDoubleArrow_pair(rulesRaw);
77  
  for (PairS rule : rules)
78  
    ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
79  
  //pnlStruct(ruleVariables);
80  
  
81  
  printAsciiHeading("REASONING");
82  
  new MultiMap<S> solutions;
83  
84  
  for (S input : tlft(sections.get("Input"))) {
85  
    print("\nInput: " + tok_dropCurlyBrackets(input));
86  
    temp tempIndent();
87  
    LPair<S, Int> interpretations = interpretations(input);
88  
    for (Pair<S, Int> in : interpretations) {
89  
      print();
90  
      printInterpretations(ll(in), "Interpretation: ");
91  
      Set<S> out = litciset();
92  
      interpretRecursively(in.a, 5, false, out);
93  
      if (!matchAny("fail", out))
94  
        solutions.put(input, in.a);
95  
    }
96  
  }
97  
  
98  
  printAsciiHeading("RESULTS");
99  
  
100  
  print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth formatForUser(multiMapToPairs(solutions))));
101  
}
102  
103  
svoid interpretRecursively(S input, int level, bool newLines, Set<S> out) {
104  
  if (level <= 0) ret;
105  
  input = ai_groupSimplestNounPhrases(input);
106  
  L<Pair<S, Int>> interpretations = interpretations(input);
107  
  addAll(out, pairsA(interpretations));
108  
  temp tempIndent();
109  
  for (Pair<S, Int> in : interpretations) {
110  
    if (newLines) print();
111  
    printInterpretations(ll(in), newLines ? "Interpretation: " : " => ");
112  
    S corrected = ai_superSimpleVerbCorrector(in.a);
113  
    interpretRecursively(in.a, level-1, false, out);
114  
    if (neq(corrected, in.a))
115  
      interpretRecursively(corrected, level-1, false, out);
116  
  }
117  
}
118  
119  
svoid printInterpretations(LPair<S, Int> interpretations, S prefix) {
120  
  for (Pair<S, Int> p : interpretations)
121  
    print(prefix + formatForUser(p.a));
122  
}
123  
124  
static LS defaultParse(S s) {
125  
  ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s));
126  
}
127  
128  
static LPair<S, Int> interpretations(S input) {
129  
  new MultiSet<PairS> ms;
130  
  LS tokI = defaultParse(input);
131  
  //print("Raw parse: " + tokI);
132  
  new L<Pair<S, Int>> interpretations;
133  
    
134  
  for (PairS rule : rules) {
135  
    LS tokR = defaultParse(rule.a);
136  
    final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI));
137  
    if (map == null) continue;
138  
    
139  
    // Found matching rule
140  
    
141  
    int score = l(tokR)-l(map);
142  
    L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule)));
143  
    if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars);
144  
    if (nempty(nonVars)) score = 0;
145  
    ms.add(rule, score+1);
146  
    if (printMappings) print("  " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a);
147  
    
148  
    // Make consequence
149  
    
150  
    S c = rule.b;
151  
    c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
152  
    // TODO: multiple grouping levels
153  
    c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
154  
    
155  
    int percent = ratioToIntPercent(score, l(tokR));
156  
    if (percent >= minScore)
157  
      addPair(interpretations, c, percent);
158  
  }
159  
    
160  
  ret sortByPairBDesc(interpretations);
161  
}
162  
163  
sS formatForUser(S s) {
164  
 ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s));
165  
}

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: 314 / 808
Referenced in: [show references]