1 | static class Case {
|
2 | String id, name; |
3 | new List<Object[]> examples; // all examples |
4 | new List<Object> halfExamples; // input only examples |
5 | |
6 | // examples1 = train examples, examples2 = test examples |
7 | List<Object[]> examples1, examples2; |
8 | List<Case> combined; |
9 | int splitPoint = -1; |
10 | |
11 | void add(Case _case) {
|
12 | combined.add(_case); |
13 | examples.addAll(_case.examples); |
14 | halfExamples.addAll(_case.halfExamples); |
15 | } |
16 | |
17 | void checkSize() {}
|
18 | |
19 | L<S[]> stringExamples() {
|
20 | ret (L) examples; |
21 | } |
22 | |
23 | int defaultSplitPoint() {
|
24 | ret l(examples)-1; |
25 | } |
26 | |
27 | // parameter: how many training examples do you need? |
28 | void split(int trainingExamples) {
|
29 | splitPoint = trainingExamples; |
30 | examples1 = examples2 = null; |
31 | split(); |
32 | } |
33 | |
34 | void split() {
|
35 | if (examples1 != null) |
36 | return; // already done |
37 | if (examples.isEmpty()) |
38 | fail("No examples in case");
|
39 | if (splitPoint < 0) |
40 | splitPoint = defaultSplitPoint(); |
41 | //print("Full examples: " + examples.size() + ", splitPoint: " + splitPoint + (halfExamples.isEmpty() ? "" : ", half examples: " + halfExamples.size()));
|
42 | examples1 = examples.subList(0, splitPoint); |
43 | examples2 = subList(examples, splitPoint); |
44 | } |
45 | } |
46 | |
47 | static Case ioioiParse(S arg) tex {
|
48 | boolean verbose = false; |
49 | |
50 | if (arg == null) return null; |
51 | arg = arg.trim(); |
52 | if (arg.length() == 0) return null; |
53 | new Case _case; |
54 | String text; |
55 | |
56 | if (arg.startsWith("Combine")) {
|
57 | _case.id = "Combine"; |
58 | _case.combined = new ArrayList<Case>(); |
59 | List<String> tok = javaTok(arg); |
60 | new List<String> ids; |
61 | for (int i = 5; i < tok.size(); i += 6) { // skip # and "and"
|
62 | if (verbose) |
63 | System.out.println("Combine: Parsing " + tok.get(i));
|
64 | Case case2 = ioioiParse("#" + tok.get(i));
|
65 | _case.id += " #" + tok.get(i); |
66 | _case.add(case2); |
67 | } |
68 | return _case; |
69 | } |
70 | |
71 | if (verbose) |
72 | System.out.println("parse: testing snippet ID");
|
73 | |
74 | if (isSnippetID(arg)) {
|
75 | _case.id = arg; |
76 | String[] x = loadSnippetAndTitle(arg); |
77 | text = x[0]; |
78 | _case.name = x[1]; |
79 | } else {
|
80 | _case.id = "direct (" + shorten(arg, 10) + ")";
|
81 | text = arg; |
82 | } |
83 | |
84 | if (verbose) |
85 | System.out.println("parse: testing snippet ID done " + quote(text));
|
86 | |
87 | // it's a collection of cases! |
88 | /*if (text.trim().startsWith("#") || text.trim().startsWith("Combine")) {
|
89 | for (String line : toLines(text)) |
90 | if (isSnippetID(line)) |
91 | parse(line); |
92 | else if (line.trim().length() != 0) |
93 | fail("Unknown line: " + line);
|
94 | return null; |
95 | }*/ |
96 | |
97 | // it's a "Continue:" task - transform to I/O format |
98 | if (text.trim().startsWith("Continue:")) {
|
99 | List<String> lines = toLines(text); |
100 | new StringBuilder buf; |
101 | for (int i = 1; i < lines.size(); i++) {
|
102 | buf.append("In: " + quote("" + i) + "\n");
|
103 | buf.append("Out: " + quote(lines.get(i)) + "\n");
|
104 | } |
105 | int numAsking = 3; |
106 | for (int i = lines.size(); i < lines.size()+numAsking; i++) |
107 | buf.append("In: " + quote("" + i) + "\n");
|
108 | text = buf.toString(); |
109 | } |
110 | |
111 | // it's an "Execute." task - run Java(X) and transform to I/O format |
112 | /* |
113 | if (text.trim().startsWith("Execute.")) {
|
114 | if (!execTasks) return null; |
115 | List<String> tok = javaTok(text); |
116 | new StringBuilder buf; |
117 | for (int i = 5; i < tok.size(); i += 2) {
|
118 | if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) {
|
119 | i += 2; |
120 | buf.append("--\n");
|
121 | } else {
|
122 | String code = unquote_fixSpaces(tok.get(i)); |
123 | String result = execute("!636\n" + code);
|
124 | buf.append("In: " + quote(code) + "\n");
|
125 | buf.append("Out: " + quote(result) + "\n");
|
126 | } |
127 | } |
128 | text = buf.toString(); |
129 | } |
130 | */ |
131 | |
132 | if (text.trim().startsWith("Marking.")) {
|
133 | List<String> tok = javaTok(text); |
134 | new StringBuilder buf; |
135 | if (verbose) |
136 | System.out.println("Is marking.");
|
137 | for (int i = 5; i < tok.size(); i += 2) {
|
138 | if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) {
|
139 | i += 2; |
140 | buf.append("--\n");
|
141 | } else if (tok.get(i).equals("Doc") && tok.get(i+2).equals(":") && tok.get(i+4).equals("#")) {
|
142 | if (verbose) |
143 | System.out.println("Loading subsnippet: " + tok.get(i+6));
|
144 | String out = loadSnippet(tok.get(i+6)); |
145 | i += 6; |
146 | String in = out.replace("[[", "").replace("]]", "");
|
147 | buf.append("In: " + quote(in) + "\n");
|
148 | buf.append("Out: " + quote(out) + "\n");
|
149 | } else {
|
150 | String out = unquote_fixSpaces(tok.get(i)); |
151 | String in = out.replace("[[", "").replace("]]", "");
|
152 | buf.append("In: " + quote(in) + "\n");
|
153 | buf.append("Out: " + quote(out) + "\n");
|
154 | } |
155 | } |
156 | text = buf.toString(); |
157 | } |
158 | |
159 | //System.out.println(text); |
160 | String in = null; |
161 | |
162 | if (verbose) |
163 | System.out.println("parse: JavaToking " + quote(text));
|
164 | |
165 | // parse a standard IOIOI document (Ins and Outs) |
166 | |
167 | List<String> tok = javaTok(text); |
168 | for (int i = 1; i < tok.size(); i += 2) {
|
169 | String t = tok.get(i), t2 = i+2 < tok.size() ? tok.get(i+2) : ""; |
170 | |
171 | if (t.equalsIgnoreCase("Cmd") && t2.equals("-") && tok.get(i+4).equalsIgnoreCase("Hint") && tok.get(i+6).equals(":")) { // "Cmd-Hint:"
|
172 | i += 8; |
173 | int j = tokFindNextLine(tok, i); |
174 | String cmdHint = unquote_fixSpaces(join(tok.subList(i, j-1))); // result unused as of now |
175 | i = j-2; |
176 | } else if (t.equals("-") && t2.equals("-")) {
|
177 | i += 2; |
178 | _case.splitPoint = _case.examples.size(); |
179 | //System.out.println("t=" + t + ", t2= " + t2);
|
180 | } else if (t.toUpperCase().startsWith("I") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("DOC") && tok.get(i+6).equals(":")) { // "In-Doc:"
|
181 | if (in != null) |
182 | _case.halfExamples.add(in); |
183 | i += 8; |
184 | int j = tokFindNextLine(tok, i); |
185 | String inID = unquote_fixSpaces(join(tok.subList(i, j-1))); |
186 | in = loadSnippet(inID); |
187 | i = j-2; |
188 | } else if (t.toUpperCase().startsWith("I") && t2.equals(":")) { // "In:" or "I:"
|
189 | if (in != null) |
190 | _case.halfExamples.add(in); |
191 | i += 4; |
192 | int j = tokFindNextLine(tok, i); |
193 | in = unquote_fixSpaces(join(tok.subList(i, j-1))); |
194 | i = j-2; |
195 | } else if ((swic(t, "O") || eqic(t, "Expect")) && t2.equals(":")) { // "Out: " or "O: " or "Expect: "
|
196 | i += 4; |
197 | int j = tokFindNextLine(tok, i); |
198 | String out = unquote_fixSpaces(join(tok.subList(i, j-1))); |
199 | i = j-2; |
200 | if ((in + out).indexOf('\t') >= 0)
|
201 | System.err.println("WARNING: Tab character used!");
|
202 | |
203 | System.out.println(shorten(quote(in), 80) + " => " + shorten(quote(out), 80)); |
204 | _case.examples.add(ioMakeArray(in, out)); |
205 | _case.checkSize(); |
206 | in = null; |
207 | } else if (t.toUpperCase().startsWith("O") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("LIST") && tok.get(i+6).equals(":")) { // "Out-List:"
|
208 | i += 8; |
209 | if (!tok.get(i).equals("{")) fail("Syntax error in Out-List");
|
210 | i += 2; |
211 | new List<String> outList; |
212 | while (!tok.get(i).equals("}")) {
|
213 | if (tok.get(i).equals(",")) i += 2;
|
214 | if (tok.get(i).equals("}")) break;
|
215 | String listEntry = unquote(tok.get(i)); |
216 | outList.add(listEntry); |
217 | i += 2; |
218 | } |
219 | System.out.println(shorten(quote(in), 80) + " => list of " + outList.size()); |
220 | _case.examples.add(ioMakeArray(in, outList)); |
221 | _case.checkSize(); |
222 | in = null; |
223 | } else {
|
224 | int j = tokFindNextLine(tok, i); |
225 | String line = join(tok.subList(i, j-1)); |
226 | i = j-2; |
227 | System.out.println("-- Ignoring line: " + line);
|
228 | } |
229 | } |
230 | |
231 | if (in != null) |
232 | _case.halfExamples.add(in); |
233 | return _case; |
234 | } |
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: | 923 / 886 |
| Referenced in: | [show references] |