1 | // learns to exchange common prefixes and suffixes. |
2 | // maybe it's not perfect... see #1000491 for a problem case |
3 | |
4 | static class LPrefixSuffix extends LearnerImpl { |
5 | static boolean debug; |
6 | String prefixIn, suffixIn, prefixOut, suffixOut; |
7 | |
8 | public void processInOut(Object _in, Object _out) { |
9 | String in = (String) _in, out = (String) _out; |
10 | updateIn(in); |
11 | prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); |
12 | suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); |
13 | if (debug) |
14 | printState("processInOut(" + quote(in) + ", " + quote(out) + ")"); |
15 | } |
16 | |
17 | void updateIn(String in) { |
18 | prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); |
19 | suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); |
20 | if (debug) |
21 | printState("updateIn(" + quote(in) + ")"); |
22 | } |
23 | |
24 | public String processIn(Object _in) { |
25 | String in = (String) _in; |
26 | //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); |
27 | //System.out.println("[last info] " + quote(in)); |
28 | |
29 | // use latest information |
30 | String p = prefixIn, s = suffixIn; |
31 | updateIn(in); |
32 | prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length())); |
33 | suffixOut = suffixOut.substring(s.length()-suffixIn.length()); |
34 | |
35 | //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); |
36 | String core = in.substring(prefixIn.length(), in.length()-suffixIn.length()); |
37 | return prefixOut + core + suffixOut; |
38 | } |
39 | |
40 | public void toJava(Code code) { |
41 | if (prefixIn.length() != 0 || suffixIn.length() != 0) |
42 | code.line(code.var + " = ((String) " + code.var + ").substring(" + prefixIn.length() + ", " + code.s() + ".length()-" + suffixIn.length() + ");"); |
43 | if (prefixOut.length() != 0 || suffixOut.length() != 0) |
44 | code.line(code.var + " = " + quote(prefixOut) + " + " + code.var + " + " + quote(suffixOut) + ";"); |
45 | } |
46 | |
47 | void printState(String text) { |
48 | System.out.println(text); |
49 | printVars(); |
50 | } |
51 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000492 |
Snippet name: | LPrefixSuffix (learner) |
Eternal ID of this version: | #1000492/1 |
Text MD5: | 9f08a357926204438d2dd3cbc5ccf057 |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-09 15:02:59 |
Source code size: | 2284 bytes / 51 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 566 / 1511 |
Referenced in: | [show references] |