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

154
LINES

< > BotCompany Repo | #281 // GroupingTest.java

Java source code

1  
package ai.d.ai07;
2  
3  
import drjava.util.F;
4  
import drjava.util.ListUtil;
5  
import drjava.util.StringUtil;
6  
import structcom.sc03.PStringLit;
7  
8  
public class GroupingTest {
9  
  public static void main(String[] args) {
10  
    String poem =
11  
      "for each line in file:\n" +
12  
      "  compare file \"/bla1/$file\" to file \"/bla2/$file\"";
13  
14  
    String groupedPoem =
15  
      "[[for] [each] [line] [in] [file]:]\n" +
16  
      "[[  ][[compare] [file] [\"/bla1/$file\"] [to] [file] [\"/bla2/$file\"]]]";
17  
    group(poem, "lines");
18  
    group(poem, "linesWithIndent");
19  
    group(poem, "words");
20  
    group(poem, "stringLits");
21  
    group(poem, "words_skipGrouped");
22  
    String grouped = group(poem, "stringLits+words_skipGrouped+linesWithIndent");
23  
    System.out.println("Expected =>");
24  
    System.out.println(StringUtil.indent(2, groupedPoem));
25  
  }
26  
27  
  private static String group(String poem, String operator) {
28  
    try {
29  
      System.out.println(operator + " =>");
30  
      String grouped = doGroup(poem, operator);
31  
      System.out.println(StringUtil.indent(2, grouped));
32  
      return grouped;
33  
    } catch (Throwable e) {
34  
      System.out.println(operator + "  ERROR: " + e);
35  
      return "";
36  
    }
37  
  }
38  
39  
  private static String doGroup(String poem, String operator) {
40  
    String[] split = operator.split("\\+");
41  
    if (split.length > 1) {
42  
      for (String op : split) poem = doGroup(poem, op);
43  
      return poem;
44  
    }
45  
46  
    if (operator.equals("lines"))
47  
      return groupLines(poem);
48  
    if (operator.equals("linesWithIndent"))
49  
      return groupLinesWithIndent(poem);
50  
    if (operator.equals("words"))
51  
      return groupWords(poem);
52  
    if (operator.equals("stringLits"))
53  
      return groupStringLits(poem);
54  
    if (operator.equals("words_skipGrouped"))
55  
      return groupWords_skipGrouped(poem);
56  
    throw new RuntimeException("Unknown operator '" + operator + "'");
57  
  }
58  
59  
  private static String groupLines(String poem) {
60  
    return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F<String, String>() {
61  
      public String _(String s) {
62  
        return "[" + s + "]";
63  
      }
64  
    }));
65  
  }
66  
67  
  private static String groupLinesWithIndent(String poem) {
68  
    return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F<String, String>() {
69  
      public String _(String s) {
70  
        if (s.startsWith("  "))
71  
          return "[[  ][" + s.substring(2) + "]]";
72  
        else
73  
          return "[" + s + "]";
74  
      }
75  
    }));
76  
  }
77  
78  
  private static String groupWords(String poem) {
79  
    return groupByPredicate(poem, new F<Character, Boolean>() {
80  
      public Boolean _(Character c) {
81  
        return Character.isLetterOrDigit(c);
82  
      }
83  
    });
84  
  }
85  
86  
  private static String groupWords_skipGrouped(String poem) {
87  
    return groupByPredicate_skipGrouped(poem, new F<Character, Boolean>() {
88  
      public Boolean _(Character c) {
89  
        return Character.isLetterOrDigit(c);
90  
      }
91  
    });
92  
  }
93  
94  
  private static String groupByPredicate(String poem, F<Character, Boolean> pred) {
95  
    StringBuilder buf = new StringBuilder();
96  
    for (int i = 0; i < poem.length(); i++) {
97  
      char c = poem.charAt(i);
98  
      if (!pred._(c))
99  
        buf.append(c);
100  
      else {
101  
        int j = i+1;
102  
        while (j < poem.length() && pred._(poem.charAt(j)))
103  
          ++j;
104  
        buf.append("[" + poem.substring(i, j) + "]");
105  
        i = j-1;
106  
      }
107  
    }
108  
    return buf.toString();
109  
  }
110  
111  
  private static String groupByPredicate_skipGrouped(String poem, F<Character, Boolean> pred) {
112  
    StringBuilder buf = new StringBuilder();
113  
    for (int i = 0; i < poem.length(); i++) {
114  
      char c = poem.charAt(i);
115  
      if (c == '[') {
116  
        int j = findEndOfGroup(poem, i); // TODO: This is unreliable
117  
        buf.append(poem.substring(i, j));
118  
        i = j-1;
119  
      } else if (!pred._(c))
120  
        buf.append(c);
121  
      else {
122  
        int j = i+1;
123  
        while (j < poem.length() && pred._(poem.charAt(j)))
124  
          ++j;
125  
        buf.append("[" + poem.substring(i, j) + "]");
126  
        i = j-1;
127  
      }
128  
    }
129  
    return buf.toString();
130  
  }
131  
132  
  private static int findEndOfGroup(String poem, int i) {
133  
    while (i < poem.length() && poem.charAt(i) != ']')
134  
      ++i;
135  
    return i;
136  
  }
137  
138  
  private static String groupStringLits(String poem) {
139  
    StringBuilder buf = new StringBuilder();
140  
    for (int i = 0; i < poem.length(); i++) {
141  
      char c = poem.charAt(i);
142  
      if (c == '"') {
143  
        PStringLit parser = new PStringLit();
144  
        parser.setText(poem, i);
145  
        parser.parse();
146  
        int j = parser.getIdx();
147  
        buf.append("[" + poem.substring(i, j) + "]");
148  
        i = j-1;
149  
      } else
150  
        buf.append(c);
151  
    }
152  
    return buf.toString();
153  
  }
154  
}

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

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

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
package ai.d.ai07;

import drjava.util.F;
import drjava.util.ListUtil;
import drjava.util.StringUtil;
import structcom.sc03.PStringLit;

public class GroupingTest {
public static void main(String[] args) {
String poem =
"for each line in file:\n" +
" compare file \"/bla1/$file\" to file \"/bla2/$file\"";

String groupedPoem =
"[[for] [each] [line] [in] [file]:]\n" +
"[[ ][[compare] [file] [\"/bla1/$file\"] [to] [file] [\"/bla2/$file\"]]]";
group(poem, "lines");
group(poem, "linesWithIndent");
group(poem, "words");
group(poem, "stringLits");
group(poem, "words_skipGrouped");
String grouped = group(poem, "stringLits+words_skipGrouped+linesWithIndent");
System.out.println("Expected =>");
System.out.println(StringUtil.indent(2, groupedPoem));
}

private static String group(String poem, String operator) {
try {
System.out.println(operator + " =>");
String grouped = doGroup(poem, operator);
System.out.println(StringUtil.indent(2, grouped));
return grouped;
} catch (Throwable e) {
System.out.println(operator + " ERROR: " + e);
return "";
}
}

private static String doGroup(String poem, String operator) {
String[] split = operator.split("\\+");
if (split.length > 1) {
for (String op : split) poem = doGroup(poem, op);
return poem;
}

if (operator.equals("lines"))
return groupLines(poem);
if (operator.equals("linesWithIndent"))
return groupLinesWithIndent(poem);
if (operator.equals("words"))
return groupWords(poem);
if (operator.equals("stringLits"))
return groupStringLits(poem);
if (operator.equals("words_skipGrouped"))
return groupWords_skipGrouped(poem);
throw new RuntimeException("Unknown operator '" + operator + "'");
}

private static String groupLines(String poem) {
return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F<String, String>() {
public String _(String s) {
return "[" + s + "]";
}
}));
}

private static String groupLinesWithIndent(String poem) {
return StringUtil.join("\n", ListUtil.map(StringUtil.toLines(poem), new F<String, String>() {
public String _(String s) {
if (s.startsWith(" "))
return "[[ ][" + s.substring(2) + "]]";
else
return "[" + s + "]";
}
}));
}

private static String groupWords(String poem) {
return groupByPredicate(poem, new F<Character, Boolean>() {
public Boolean _(Character c) {
return Character.isLetterOrDigit(c);
}
});
}

private static String groupWords_skipGrouped(String poem) {
return groupByPredicate_skipGrouped(poem, new F<Character, Boolean>() {
public Boolean _(Character c) {
return Character.isLetterOrDigit(c);
}
});
}

private static String groupByPredicate(String poem, F<Character, Boolean> pred) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (!pred._(c))
buf.append(c);
else {
int j = i+1;
while (j < poem.length() && pred._(poem.charAt(j)))
++j;
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
}
}
return buf.toString();
}

private static String groupByPredicate_skipGrouped(String poem, F<Character, Boolean> pred) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (c == '[') {
int j = findEndOfGroup(poem, i); // TODO: This is unreliable
buf.append(poem.substring(i, j));
i = j-1;
} else if (!pred._(c))
buf.append(c);
else {
int j = i+1;
while (j < poem.length() && pred._(poem.charAt(j)))
++j;
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
}
}
return buf.toString();
}

private static int findEndOfGroup(String poem, int i) {
while (i < poem.length() && poem.charAt(i) != ']')
++i;
return i;
}

private static String groupStringLits(String poem) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < poem.length(); i++) {
char c = poem.charAt(i);
if (c == '"') {
PStringLit parser = new PStringLit();
parser.setText(poem, i);
parser.parse();
int j = parser.getIdx();
buf.append("[" + poem.substring(i, j) + "]");
i = j-1;
} else
buf.append(c);
}
return buf.toString();
}
}

}}
2015-08-18 00:47:03

add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 4785 [visualize]

Snippet ID: #281
Snippet name: GroupingTest.java
Eternal ID of this version: #281/1
Text MD5: 059a6a85452935dd18855afe3c0060fd
Author: stefan
Category: Java sources
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2014-10-06 19:01:32
Source code size: 4785 bytes / 154 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1009 / 190
Referenced in: [show references]