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

46
LINES

< > BotCompany Repo | #1025648 // smartParser1_generic

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4815L/31K).

static L<ExtS> smartParser1_generic(S program, O... _) {
  optPar bool verbose;
  new L<ExtS> out;
  
  // split into paragraphs and unindent

  LS paragraphs = map autoUnindent(map rtrim(tok_splitAtEmptyLines(program)));
  if (verbose) print("Got " + n2(paragraphs, "parapraph"));

  // print the parapraphs
  if (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 (verbose) print("Got " + n2(paragraphs2, "filtered paragraph"));
  if (verbose) print(joinWithEmptyLines(map(s -> indentx("> ", s), paragraphs2)));

  // find fact paragraphs
  
  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))
      out.add(ext fact(s));

  // find logic rules

  new LS paragraphs4;
  for (S para : paragraphs3) {
    PairS p = splitAtDoubleArrow_pair(para);
    if (p == null) continue with paragraphs4.add(para);
    out.add(ext logicRule(para));
  }

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

  // assume the unparsed stuff consists of facts
  for (S para : paragraphs4)
    out.add(ext fact(para));
  
  ret out;
}

Author comment

Began life as a copy of #1025615

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025648
Snippet name: smartParser1_generic
Eternal ID of this version: #1025648/3
Text MD5: f01c694e9982addee465a6cf5eb12874
Transpilation MD5: c7abe6c6924c3fbb6109d6e9f432749a
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-12 17:27:39
Source code size: 1513 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 186 / 255
Version history: 2 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)