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

84
LINES

< > BotCompany Repo | #1000468 // LOneWordChanged (learner)

JavaX fragment (include)

1  
2  
static class LOneWordChanged extends LearnerImpl {
3  
  static boolean debug;
4  
  Function preprocessor; // e.g. JavaTok
5  
  List<String> lastIn; // only while learning
6  
  String lastOut; // only while learning
7  
  int index;
8  
  String outPattern;
9  
10  
  *(Function *preprocessor) {}
11  
  
12  
  public void processInOut(Object _in, Object _out) {
13  
    if (debug)
14  
      System.out.println("LOneWordChanged: i/o " + _in + " " + _out);
15  
    String in = (String) _in, out = (String) _out;
16  
    List<String> l = (List<String>) preprocessor.process(_in);
17  
    lastIn = l;
18  
    lastOut = out;
19  
  }
20  
  
21  
  public Object processIn(Object _in) {
22  
    if (debug)
23  
      System.out.println("LOneWordChanged: i " + _in);
24  
    List<String> l = (List<String>) preprocessor.process(_in);
25  
    
26  
    if (outPattern == null) {
27  
      if (l.size() != lastIn.size()) fail();
28  
      for (int i = 0; i < l.size(); i++)
29  
        if (!l.get(i).equals(lastIn.get(i))) {
30  
          index = i;
31  
          String word = lastIn.get(index);
32  
          outPattern = lastOut.replace(word, "{*}");
33  
          
34  
          // delete learning data
35  
          lastIn = null;
36  
          lastOut = null;
37  
          break;
38  
        }
39  
        
40  
      if (outPattern == null) {
41  
        if (debug)
42  
          System.out.println("LOneWordChanged: last=" + lastIn + ", this=" + l);
43  
        fail("not applicable - all words identical");
44  
      }
45  
    }
46  
    if (debug)
47  
      System.out.println("LOneWordChanged: index=" + index + ", outPattern=" + quote(outPattern));
48  
49  
    String word = l.get(index);
50  
    return outPattern.replace("{*}", word);
51  
  }
52  
  
53  
  static List<Integer> getFindMarkers2(String s) {
54  
    List<Integer> l = new ArrayList<Integer>();
55  
    int i = 0;
56  
    while (i < s.length()) {
57  
      int j = s.indexOf("[[", i);
58  
      if (j < 0) break;
59  
      int k = s.indexOf("]]", j+2);
60  
      if (k < 0) break;
61  
      l.add(j);
62  
      l.add(k+2);
63  
      i = k+2;
64  
    }
65  
    return l;
66  
  }
67  
  
68  
  static List<String> getMarked(String s) {
69  
    List<Integer> l = getFindMarkers2(s);
70  
    new List<String> result;
71  
    for (int i = 0; i < l.size(); i += 2)
72  
      result.add(s.substring(l.get(i)+2, l.get(i+1)-2));
73  
    return result;
74  
  }
75  
  
76  
  public void toJava(Code code) {
77  
    preprocessor.toJava_process(code);
78  
    String word = code.list() + ".get(" + index + ")";
79  
    if (outPattern.equals("{*}"))
80  
      code.assign(word);
81  
    else
82  
      code.assign(javaQuote(outPattern) + ".replace(" + javaQuote("{*}") + ", (String) (" + word + "))");
83  
  }
84  
}

Author comment

Began life as a copy of #1000465

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

Comments [hide]

ID Author/Program Comment Date
341 #1000604 (pitcher) 2015-08-18 00:07:22
340 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {

static class LOneWordChanged extends LearnerImpl {
static boolean debug;
Function preprocessor; // e.g. JavaTok
List<String> lastIn; // only while learning
String lastOut; // only while learning
int index;
String outPattern;

*(Function *preprocessor) {}

public void processInOut(Object _in, Object _out) {
if (debug)
System.out.println("LOneWordChanged: i/o " + _in + " " + _out);
String in = (String) _in, out = (String) _out;
List<String> l = (List<String>) preprocessor.process(_in);
lastIn = l;
lastOut = out;
}

public Object processIn(Object _in) {
if (debug)
System.out.println("LOneWordChanged: i " + _in);
List<String> l = (List<String>) preprocessor.process(_in);

if (outPattern == null) {
if (l.size() != lastIn.size()) fail();
for (int i = 0; i < l.size(); i++)
if (!l.get(i).equals(lastIn.get(i))) {
index = i;
String word = lastIn.get(index);
outPattern = lastOut.replace(word, "{*}");

// delete learning data
lastIn = null;
lastOut = null;
break;
}

if (outPattern == null) {
if (debug)
System.out.println("LOneWordChanged: last=" + lastIn + ", this=" + l);
fail("not applicable - all words identical");
}
}
if (debug)
System.out.println("LOneWordChanged: index=" + index + ", outPattern=" + quote(outPattern));

String word = l.get(index);
return outPattern.replace("{*}", word);
}

static List<Integer> getFindMarkers2(String s) {
List<Integer> l = new ArrayList<Integer>();
int i = 0;
while (i < s.length()) {
int j = s.indexOf("[[", i);
if (j < 0) break;
int k = s.indexOf("]]", j+2);
if (k < 0) break;
l.add(j);
l.add(k+2);
i = k+2;
}
return l;
}

static List<String> getMarked(String s) {
List<Integer> l = getFindMarkers2(s);
new List<String> result;
for (int i = 0; i < l.size(); i += 2)
result.add(s.substring(l.get(i)+2, l.get(i+1)-2));
return result;
}
}
}}
2015-08-18 03:04:09  delete 

add comment

Snippet ID: #1000468
Snippet name: LOneWordChanged (learner)
Eternal ID of this version: #1000468/1
Text MD5: 410fa431d4124c533c3273cf6b98e5ca
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-21 21:42:10
Source code size: 2523 bytes / 84 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1055 / 2158
Referenced in: [show references]