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

86
LINES

< > BotCompany Repo | #1002372 // New Parser Bot (parse/jparse)

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

Libraryless. Click here for Pure Java version (2977L/20K/64K).

!759

static Class parser;

static S javaRulesID = "#1002329";

p {
  print("parser bot loading");
  parser = hotwire_overBot("#1002472");
  print("parser bot loaded");
}

answer {
  if (startsWithIgnoreCase(s, "parse ")) try {
    fillNLParser(parser);
    S text = s.substring(5).trim();
    boolean debug = text.startsWith("debug ");
    if (debug) text = text.substring(5).trim();
    boolean explain = text.startsWith("explain ");
    S explainClass = null;
    if (explain) {
      text = text.substring("explain ".length()).trim();
      int i = text.indexOf(' ');
      explainClass = text.substring(0, i).trim();
      text = text.substring(i).trim();
    }
    
    O result;
    time {
      result = call(parser, "parse", text);
    }
    
    if (debug)
      ret structure(get(parser, "recog"));
    else if (explain)
      ret structure(call(result, "explain", explainClass));
    else {
      L<S> classes = cast call(result, "fullMatchClasses");
      ret (empty(classes) ? "not parsed" : "parsed as " + structure(classes)) + " [" + getLastTiming() + " ms, " + getOpt(parser, "timing") + " ms actual]";
    }
  } catch (Throwable e) {
    ret exceptionToUser(e);
  }
  
  if (startsWithIgnoreCase(s, "jparse ")) try {
    S text = s.substring(6).trim();
    
    // make a separate parser for Java
    O parser = hotwire_overBot("#1002472");
   
    boolean debug = text.startsWith("debug ");
    if (debug) text = text.substring(5).trim();
    boolean explain = text.startsWith("explain ");
    S explainClass = null;
    if (explain) {
      text = text.substring("explain ".length()).trim();
      int i = text.indexOf(' ');
      explainClass = text.substring(0, i).trim();
      text = text.substring(i).trim();
    }
    
    O result;
    time {
      result = call(parser, "parse", text, javaRulesID);
    }
    
    if (debug)
      ret "```" + call(result, "prettierAnalysis") + "```";
    else if (explain)
      ret structure(call(result, "explain", explainClass));
    else {
      L<S> classes = cast call(result, "fullMatchClasses");
      ret (empty(classes) ? "not parsed" : "parsed as " + structure(classes)) + " [" + getLastTiming() + " ms, " + getOpt(parser, "timing") + " ms actual]";
    }
  } catch (Throwable e) {
    ret exceptionToUser(e);
  }
}

static void fillNLParser(O parser) {
  callOpt(parser, "addDict", "adjective", makeEleuSet("adjective"));

  O bot = getBot("#1002342"); // nouns bot
  if (bot != null)
    callOpt(parser, "addDict", "noun", get(bot, "nouns"));
}

Author comment

Began life as a copy of #1002308

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002372
Snippet name: New Parser Bot (parse/jparse)
Eternal ID of this version: #1002372/1
Text MD5: 61300e08f4333bc3f99089d5d7fdf44b
Transpilation MD5: 22e34baa7a7e5ac885341764f1a3c3e5
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-01 19:57:32
Source code size: 2604 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 679 / 1631
Referenced in: [show references]