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

44
LINES

< > BotCompany Repo | #1025615 // smartParser1 [Include]

JavaX fragment (include)

bool smartParser1_verbose;

void smartParser1(S program) {
  // split into paragraphs and unindent

  LS paragraphs = map(s -> withoutLeadingLinesEmptyAfterTrim_autoUnindent(rtrim(s)), tok_splitAtEmptyLines(program));
  if (smartParser1_verbose) print("Got " + n2(paragraphs, "parapraph"));

  // print the parapraphs
  if (smartParser1_verbose) print(joinWithEmptyLines(map(s -> indentx("> ", s), paragraphs)));

  // throw away comment-only and quoted paragraphs (assume it's a title)
  LS paragraphs2 = antiFilter(paragraphs, s -> 
    isSingleLine(trim(s)) && isQuoted(trim(s)) || countJavaTokens(s) == 0
    || endsWith(rtrim(s), "----"));
  if (smartParser1_verbose) print("Got " + n2(paragraphs2, "filtered paragraph"));
  if (smartParser1_verbose) print(joinWithEmptyLines(map(s -> indentx("> ", s), paragraphs2)));

  // find fact paragraphs
  
  if (smartParser1_verbose) print(map allLinesAreUnindented(paragraphs2));
  Pair<LS> p1 = filterAntiFilter(s ->
    !isSingleLine(trim(s)) && allLinesAreUnindented(s), paragraphs2);
  LS multiFactParagraphs = p1.a, paragraphs3 = p1.b;

  for (S para : multiFactParagraphs)
    for (S s : tlft(para))
      addFactFromProgram(s);

  // find logic rules

  new LS paragraphs4;
  for (S para : paragraphs3) {
    PairS p = splitAtFirstDoubleArrow(javaTokWithBrackets(para));
    if (p == null) continue with paragraphs4.add(para);
    addLogicRule(new LogicRule(splitAtAmpersand2(p.a), p.b), para);
  }

  if (smartParser1_verbose) pnlStruct("Unparsed - assuming facts", paragraphs4);

  // assume the unparsed stuff consists of facts
  for (S para : paragraphs4)
    addFactFromProgram(para);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1025615
Snippet name: smartParser1 [Include]
Eternal ID of this version: #1025615/11
Text MD5: 43e575a62dec9c59e45537f66b888139
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-24 21:23:06
Source code size: 1690 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 282 / 1387
Version history: 10 change(s)
Referenced in: [show references]