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

177
LINES

< > BotCompany Repo | #1014753 // Snippet Title Reasoning v1 [OK]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (24310L/165K).

!7

!include once #1014750 // CSnippet

sS inspiration = [[
  snippet titles
  "[OK]"
]];

static new LL<S> facts; // stores code token parses
static Map<S, O> phrasesWordTree = wordTree();
static Clusters<S> clusters = ciClusters();
static Prolog prolog;

sS reasoning = [[
  // check inspiration :o)
  assert(inspired "snippet titles")
  assert(inspired "\"[OK]\"")

  // phrases
  phrase "snippet titles"
  phrase "a list of strings"
  phrase "a quoted string"
  
  // synonyms
  "the snippet titles" = "snippet titles"
  
  // grammar
  singular_plural("snippet title", "snippet titles")
  
  // test parsing
  assert_parse("snippet titles are a list of strings", "snippet titles", "are", "a list of strings")
  
  // facts
  snippet titles are a list of strings
  "[OK]" is a quoted string
  
  // rule to answer a question
  fact_question_answer_upperCaseVars("X are Y", "what are X?", "Y")
  
  // check the rule
  assert_answer("what are snippet titles?", "a list of strings")
]];

p-experiment-tt { go(); }

svoid go {
  prolog = new Prolog;
  
  L<S> program = tlftj(reasoning);
  pnl(program);
  print();
  
  //set translateUsingWordTree_debug;
  //set translateUsingWordTreeC_printReplacements;
  
  for (S s : tlftj(inspiration))
    addFact("inspired " + quote(s));
    
  for (S s : program) {
    Pair<S, L<S>> p = parseFunctionCall(s);
    if (p != null) {
      //print("Function call: " + sfu(p));
      S f = p.a;
      L<S> args = p.b;
      if (eqic(f, "assert"))
        assertFact(getSingleton(p.b));
      else if (swic(f, "assert_")) {
        if (eqic(f, "assert_answer"))
          assertAnswer(unquote(first(args)), unquote(second(args)));
        else if (eqic(f, "assert_parse"))
          assertParse(args);
        else
          fail("Unknown assertion function: " + f);
      } else if (eqic(f, "fact_question_answer_upperCaseVars"))
        prolog.code(
          print("if [" + toProlog(unquote(first(args))) + "] and ["
          + toProlog(unquote(second(args))) + "] then [answer is ["
          + toProlog(unquote(third(args))) + "]]"));
      continue;
    }
    
    L<S> tok = javaTokC(s);
    if (l(tok) == 2 && isIdentifier(first(tok)) && isQuoted(second(tok)))
      if (eq(first(tok), "phrase")) {
        addPhrase(unquote(second(tok)));
        continue;
      }
      
    if (l(tok) == 3 && eq(second(tok), "=")) {
      ciClusters_add(clusters,
        addPhrase(unquote(first(tok))),
        addPhrase(unquote(third(tok))));
      continue;
    }

    addFact(s);
  }
  
  printAsciiHeading("Facts");
  pnlStruct(facts);
  print();
  
  print("Clusters: " + sfu(clusters));
  print("Phrases: " + sfu(phrasesWordTree));
  //loadSnippets();
}

svoid loadSnippets {
  loadMainConceptsFrom_readOnly(#1009918); // get snippets
  print("Have " + countConcepts(CSnippet) + " snippets");
  pnl(takeFirst10(collect(list(CSnippet), 'title));
}

static L<S> parseFact(S s) {
  L<S> tok = map toLowerIfIdentifier(javaTokWithAngleBracketsC(/*dropPunctuation*/(s)));
  ret nempties(translateUsingWordTreeC(tok, phrasesWordTree));
}

svoid assertFact(S fact) {
  if (contains(facts, parseFact(fact)))
    print("OK fact: " + fact);
  else
    print("FAILED fact assert: " + fact);
}

svoid assertAnswer(S q, S a) {
  Prolog p = new(prolog);
  //p.showStuff = true;
  p.code(dontPrint("to prolog: ", squareBracket(toProlog(q))));
  L<Lisp> out = p.rewrite();
  //print("Got " + l(out) + " statement(s)");
  L<S> lines = map nlUnparse(out);
  L<S> answers = startingWithEndingWith_drop(lines, "answer is [", "]");
  if (l(answers) == 1 && eqic(first(answers), a))
    print("OK answer: " + q + " -> " + a);
  else {
    pnl(map nlUnparse(out));
    print("FAIL answer: " + q + " -> " + first(answers) + " - expected: " + a);
  }
}

sS addPhrase(S s) {
  wordTreeAdd(phrasesWordTree, javaTokC(s), s);
  ret s;
}

svoid addFact(S s) {
  L<S> tok = parseFact(s);
  facts.add(tok);
  prolog.code(dontPrint("to prolog: ", squareBracket(factToProlog(tok))));
}

svoid assertParse(L<S> args) {
  args = unquoteAll(args);
  S s = first(args);
  L<S> parsed = parseFact(s);
  L<S> expected = dropFirst(args);
  if (eq(parsed, expected))
    print("OK parse: " + s + " -> " + sfu(parsed));
  else
    print("FAILED parse: " + s + " -> " + sfu(parsed) + " - expected: " + sfu(expected));
}

sS toProlog(S s) {
  L<S> tok = parseFact(upperCaseVarsToDollarVars(s));
  ret factToProlog(tok);
}

sS factToProlog(L<S> tok) {
  ret joinWithSpace(map tokenToProlog(tok));
}

sS tokenToProlog(S t) {
  if (!isQuoted(t) && containsSpace(t)) ret squareBracket(t);
  ret t;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014753
Snippet name: Snippet Title Reasoning v1 [OK]
Eternal ID of this version: #1014753/64
Text MD5: fd884adc61ab7546331d8200c3518e11
Transpilation MD5: 2c24cc4dc2811f1de52d832e24fe3cd1
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-01 22:26:48
Source code size: 4767 bytes / 177 lines
Pitched / IR pitched: No / No
Views / Downloads: 602 / 3480
Version history: 63 change(s)
Referenced in: #1014772 - Snippet Title Reasoning v2 [OK, shortened]