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

142
LINES

< > BotCompany Repo | #1027165 // Winograd Solver [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (13988L/72K).

!7

!include once #1027167 // Compact Module Include 2 v2

set flag AllPublic.

module WinogradSolver1 > DynSingleFunctionWithPrintLog {
  transient PhilosophyBot1 engine;
  transient O multiStage; // unused
  
  S prelude = "import bool";
  
  S input = "Bob paid for Charlie's college education. He is very [generous/grateful]. Who is [generous/grateful]?";
  
  Map<S, SS> answers = syncMap();

  delegate LogicRule to PhilosophyBot1.
  replace GroupingAndPatterns with Pair<S, Set<S>>.

  void doIt() {
    // Stage 3a: Select the right rules for the task
    Cl<S> rulesToUse = dm_getUnclearList("Rules"); // take all for now
    LS patternsToUse = dm_getUnclearList("Patterns");
      
    doIt(rulesToUse, patternsToUse);
  }
  
  void doIt(Cl<S> rulesToUse, LS patternsToUse) {
    clear(answers); change();
    if (empty(input)) ret;

    print("Using patterns: " + sfu(patternsToUse));
    print("Using rules: " + sfu(rulesToUse));
    
    // Stage 1: take one option out of square brackets
    for (S variant : winograd_variants(input)) {
      print(variant);
      SS _answers = ciMap();
      dm_call(multiStage, 'addStage, "Variant", variant);
      
      // Stage 1b: Split off 's
      S input2b = ai_splitOffApostropheS(variant);
      dm_call(multiStage, 'addStage, "Split off 's", input2b);
      
      // Stage 1c: Split into sentences
      LS sentences = map dropQuestionMark(sentences(input2b));
      dm_call(multiStage, 'addStage, "Split into sentences", sentences);
      
      // Stage 2: Parser
      S parser = dm_makeModule("#1027108/TopDownParsing");
      //L<virtual TryPatterns> parsed = map(sentences, s -> dm_call(parser, 'parse, s));
      L<Cl<GroupingAndPatterns>> groupingsWithPatterns = map(sentences,
        s -> (Cl<GroupingAndPatterns>) dm_callWithImport(parser, 'parseToGroupingsWithUsedPatterns, s,
          patterns := patternsToUse));
      L<GroupingAndPatterns> groupingsWithPatterns2 = concatLists(groupingsWithPatterns);
      Map<S, Set<S>> groupingToUsedPatterns = pairsToMap(groupingsWithPatterns2);

      //L<Cl<S>> parsed = map(sentences, s -> (Cl<S>) dm_call(parser, 'parseToGroupings, s));
      //pnl(parsed);
      Set<S> allGroupings = toCISet(pairsA(groupingsWithPatterns2));
      print("Have " + n2(allGroupings, "grouping"));
      //dm_call(multiStage, 'addStage, "Parser", parsed);
      
      // Stage 3b: Run logic engine
      S program = joinWithEmptyLines(concatLists(ll(prelude), allGroupings, rulesToUse));
      engine = PhilosophyBot1(program);
      engine.standardImports();
      engine.enableTrails();
      engine.parseProgram();
      for (S s : allGroupings) {
        //print("Setting trail for " + quote(s));
        engine.setTrail(s, "grouped original input");
      }
      engine.printFactsAfterThinking = false;
      L<LogicRule> initialRules = engine.logicRules();
      engine.think();
      engine.printDeducedFactsWithTrails();

      Set<S> allOriginalGroupings = cloneSet(allGroupings);

      // gather simplified versions of questions so we find their answers too
      LPairS simplifications = philosophyBot1_allSimplifications_gen(engine);
      for (PairS p : simplifications)
        if (allGroupings.contains(p.a)) allGroupings.add(p.b);
      
      for (Pair<S, SS> p : engine.matchFacts2("the answer to $x is $y"))
 {
        S fact = p.a;
        SS map = p.b;
        if (allGroupings.contains($x(map))) {
          printAfterNewLine("ANSWER>> " + $x(map) + ": " + $y(map));
          //print("Trail: " + engine.getTrail(fact));
          //print("Question trail: " + engine.getTrail($x(map)));
          
          Set set = engine.thingsUsedForMaking(fact);
          Set<S> strings = asCISet(stringsOnly(set));
          Set<LogicRule> sourceRules = setIntersection(set, initialRules);
          Set<S> usedRules = setIntersection(strings, rulesToUse);
          strings = setMinusSet(strings, usedRules);
          Set<S> usedGroupings = setIntersection(strings, allOriginalGroupings);
          Set<S> remainingStrings = setMinusSet(strings, allGroupings);

          Set<S> usedPatterns = joinCISets(map(usedGroupings, s -> groupingToUsedPatterns.get(s)));
          LS usedRules_prettier = map(usedRules, s -> replacePrefixIC("add simplifier ", "simplifier ", s));

          /*for (O o : sourceRules) {
            print(" RULE >> " + o);
            print("    Trail: " + engine.getTrail(o));
          }
          for (O o : usedRules) {
            print(" FACT >> " + o);
            print("    Trail: " + engine.getTrail(o));
          }*/
          pnl("  PATTERN >> ", usedPatterns);
          pnl("  GROUPED INPUT >> ", usedGroupings);
          pnl("  TOOL >> ", usedRules_prettier);
          _answers.put($x(map), $y(map));
        }
      }
      answers.put(variant, _answers); change();
      print();
    }
  }
  
  visual northCenterAndSouthWithMargins(
    jCenteredSection("Input", dm_centeredTextField input()),
    super,
    jCenteredSection("Answers",
      //dm_calculatedTextArea(() -> pnlToString(answers)))
      dm_calculatedCenteredLabel(() -> joinWithSlash(
        map(values(answers), map -> joinWithComma(values(map)))))
    ));
  
  start {
    dm_registerAs winogradSolver();
  }

  // API

  O getTrailForFact(S fact) {
    ret engine == null ? null : engine.getTrail(fact);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027165
Snippet name: Winograd Solver [dev.]
Eternal ID of this version: #1027165/80
Text MD5: 3ddbfe5f12c07a7e04e9852e6932b3d5
Transpilation MD5: 66e546022db979d3ff1db82d65995295
Author: stefan
Category: javax / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-24 22:48:22
Source code size: 5407 bytes / 142 lines
Pitched / IR pitched: No / No
Views / Downloads: 318 / 1676
Version history: 79 change(s)
Referenced in: #1027181 - Winograd Solver (backup before trails)