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

234
LINES

< > BotCompany Repo | #1003023 // ioioiParse

JavaX fragment (include)

static class Case {
  String id, name;
  new List<Object[]> examples; // all examples
  new List<Object> halfExamples; // input only examples
  
  // examples1 = train examples, examples2 = test examples
  List<Object[]> examples1, examples2;
  List<Case> combined;
  int splitPoint = -1;
  
  void add(Case _case) {
    combined.add(_case);
    examples.addAll(_case.examples);
    halfExamples.addAll(_case.halfExamples);
  }
  
  void checkSize() {}
  
  L<S[]> stringExamples() {
    ret (L) examples;
  }
  
  int defaultSplitPoint() {
    ret l(examples)-1;
  }
  
  // parameter: how many training examples do you need?
  void split(int trainingExamples) {
    splitPoint = trainingExamples;
    examples1 = examples2 = null;
    split();
  }
  
  void split() {    
    if (examples1 != null)
      return; // already done
    if (examples.isEmpty())
      fail("No examples in case");
    if (splitPoint < 0)
      splitPoint = defaultSplitPoint();
    //print("Full examples: " + examples.size() + ", splitPoint: " + splitPoint + (halfExamples.isEmpty() ? "" : ", half examples: " + halfExamples.size()));
    examples1 = examples.subList(0, splitPoint);
    examples2 = subList(examples, splitPoint);
  }
}
    
static Case ioioiParse(S arg) tex {
  boolean verbose = false;
  
  if (arg == null) return null;
  arg = arg.trim();
  if (arg.length() == 0) return null;
  new Case _case;
  String text;
  
  if (arg.startsWith("Combine")) {
    _case.id = "Combine";
    _case.combined = new ArrayList<Case>();
    List<String> tok = javaTok(arg);
    new List<String> ids;
    for (int i = 5; i < tok.size(); i += 6) { // skip # and "and"
      if (verbose)
        System.out.println("Combine: Parsing " + tok.get(i));
      Case case2 = ioioiParse("#" + tok.get(i));
      _case.id += " #" + tok.get(i);
      _case.add(case2);
    }
    return _case;
  }
  
  if (verbose)
    System.out.println("parse: testing snippet ID");
    
  if (isSnippetID(arg)) {
    _case.id = arg;
    String[] x = loadSnippetAndTitle(arg);
    text = x[0];
    _case.name = x[1];
  } else {
    _case.id = "direct (" + shorten(arg, 10) + ")";
    text = arg;
  }

  if (verbose)
    System.out.println("parse: testing snippet ID done " + quote(text));
    
  // it's a collection of cases!
  /*if (text.trim().startsWith("#") || text.trim().startsWith("Combine")) {
    for (String line : toLines(text))
      if (isSnippetID(line))
        parse(line);
      else if (line.trim().length() != 0)
        fail("Unknown line: " + line);
    return null;
  }*/
  
  // it's a "Continue:" task - transform to I/O format
  if (text.trim().startsWith("Continue:")) {
    List<String> lines = toLines(text);
    new StringBuilder buf;
    for (int i = 1; i < lines.size(); i++) {
      buf.append("In: " + quote("" + i) + "\n");
      buf.append("Out: " + quote(lines.get(i)) + "\n");
    }
    int numAsking = 3;
    for (int i = lines.size(); i < lines.size()+numAsking; i++)
      buf.append("In: " + quote("" + i) + "\n");
    text = buf.toString();
  }
    
  // it's an "Execute." task - run Java(X) and transform to I/O format
  /*
  if (text.trim().startsWith("Execute.")) {
    if (!execTasks) return null;
    List<String> tok = javaTok(text);
    new StringBuilder buf;
    for (int i = 5; i < tok.size(); i += 2) {
      if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) {
        i += 2;
        buf.append("--\n");
      } else {
        String code = unquote_fixSpaces(tok.get(i));
        String result = execute("!636\n" + code);
        buf.append("In: " + quote(code) + "\n");
        buf.append("Out: " + quote(result) + "\n");
      }
    }
    text = buf.toString();
  }
  */

  if (text.trim().startsWith("Marking.")) {
    List<String> tok = javaTok(text);
    new StringBuilder buf;
    if (verbose)
      System.out.println("Is marking.");
    for (int i = 5; i < tok.size(); i += 2) {
      if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) {
        i += 2;
        buf.append("--\n");
      } else if (tok.get(i).equals("Doc") && tok.get(i+2).equals(":") && tok.get(i+4).equals("#")) {
        if (verbose)
          System.out.println("Loading subsnippet: " + tok.get(i+6));
        String out = loadSnippet(tok.get(i+6));
        i += 6;
        String in = out.replace("[[", "").replace("]]", "");
        buf.append("In: " + quote(in) + "\n");
        buf.append("Out: " + quote(out) + "\n");
      } else {
        String out = unquote_fixSpaces(tok.get(i));
        String in = out.replace("[[", "").replace("]]", "");
        buf.append("In: " + quote(in) + "\n");
        buf.append("Out: " + quote(out) + "\n");
      }
    }
    text = buf.toString();
  }

  //System.out.println(text);
  String in = null;
  
  if (verbose)
    System.out.println("parse: JavaToking " + quote(text));
  
  // parse a standard IOIOI document (Ins and Outs)
  
  List<String> tok = javaTok(text);
  for (int i = 1; i < tok.size(); i += 2) {
    String t = tok.get(i), t2 = i+2 < tok.size() ? tok.get(i+2) : "";
    
     if (t.equalsIgnoreCase("Cmd") && t2.equals("-") && tok.get(i+4).equalsIgnoreCase("Hint") && tok.get(i+6).equals(":")) { // "Cmd-Hint:"
      i += 8;
      int j = tokFindNextLine(tok, i);
      String cmdHint = unquote_fixSpaces(join(tok.subList(i, j-1))); // result unused as of now
      i = j-2;
    } else if (t.equals("-") && t2.equals("-")) {
      i += 2;
      _case.splitPoint = _case.examples.size();
      //System.out.println("t=" + t + ", t2= " + t2);
    } else if (t.toUpperCase().startsWith("I") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("DOC") && tok.get(i+6).equals(":")) { // "In-Doc:"
      if (in != null)
        _case.halfExamples.add(in);
      i += 8;
      int j = tokFindNextLine(tok, i);
      String inID = unquote_fixSpaces(join(tok.subList(i, j-1)));
      in = loadSnippet(inID);
      i = j-2;
    } else if (t.toUpperCase().startsWith("I") && t2.equals(":")) { // "In:" or "I:"
      if (in != null)
        _case.halfExamples.add(in);
      i += 4;
      int j = tokFindNextLine(tok, i);
      in = unquote_fixSpaces(join(tok.subList(i, j-1)));
      i = j-2;
    } else if ((swic(t, "O") || eqic(t, "Expect")) && t2.equals(":")) { // "Out: " or "O: " or "Expect: "
      i += 4;
      int j = tokFindNextLine(tok, i);
      String out = unquote_fixSpaces(join(tok.subList(i, j-1)));
      i = j-2;
      if ((in + out).indexOf('\t') >= 0)
        System.err.println("WARNING: Tab character used!");
        
      System.out.println(shorten(quote(in), 80) + " => " + shorten(quote(out), 80));
      _case.examples.add(ioMakeArray(in, out));
      _case.checkSize();
      in = null;
    } else if (t.toUpperCase().startsWith("O") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("LIST") && tok.get(i+6).equals(":")) { // "Out-List:"
      i += 8;
      if (!tok.get(i).equals("{")) fail("Syntax error in Out-List");
      i += 2;
      new List<String> outList;
      while (!tok.get(i).equals("}")) {
        if (tok.get(i).equals(",")) i += 2;
        if (tok.get(i).equals("}")) break;
        String listEntry = unquote(tok.get(i));
        outList.add(listEntry);
        i += 2;
      }
      System.out.println(shorten(quote(in), 80) + " => list of " + outList.size());
      _case.examples.add(ioMakeArray(in, outList));
      _case.checkSize();
      in = null;
    } else {
      int j = tokFindNextLine(tok, i);
      String line = join(tok.subList(i, j-1));
      i = j-2;
      System.out.println("-- Ignoring line: " + line);
    }
  }
  
  if (in != null)
    _case.halfExamples.add(in);
  return _case;
}

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: #1003023
Snippet name: ioioiParse
Eternal ID of this version: #1003023/1
Text MD5: 45635c5da01bd4ceadd170d88f95badc
Author: stefan
Category: javax / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-21 17:10:24
Source code size: 7848 bytes / 234 lines
Pitched / IR pitched: No / No
Views / Downloads: 642 / 617
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)