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));
  }

  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   

Travelled to 4 computer(s): mqqgnosmbjvj, onxytkatvevr, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1025615
Snippet name: smartParser1 [Include]
Eternal ID of this version: #1025615/10
Text MD5: 335cef2aa3a2c28980a4f8c2f4cb5807
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-01 13:48:09
Source code size: 1684 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 39 / 529
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1025615 & http://1025615.tinybrain.de