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