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();
  }
}
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
| 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 | 
| 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: | 1423 / 268 | 
| Referenced in: | #3000189 - Answer for stefanreich(>> t bla) #3000382 - Answer for ferdie (>> t = 1, f = 0) #3000383 - Answer for funkoverflow (>> t=1, f=0 okay)  |