public Collection<Tree> read(final String fileName) throws IOException {
    List<Tree> result = new ArrayList<Tree>();
    InputStream file = getClass().getResourceAsStream(fileName);
    if (file == null) {
      throw new IllegalArgumentException("File " + fileName + " not found");
    }
    try {

      // Get the workbook instance for XLS file
      HSSFWorkbook workbook = new HSSFWorkbook(file);

      // Get first sheet from the workbook
      HSSFSheet sheet = workbook.getSheetAt(0);

      // Get iterator to all the rows in current sheet
      Iterator<Row> rowIterator = sheet.iterator();

      rowIterator.next(); // ignore column title

      while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        TreeBuilder reader = new TreeBuilder(row);
        Tree tree = reader.buildTree();
        result.add(tree);
        System.out.println(tree.toString());
      }
    } finally {
      file.close();
    }
    return result;
  }
Example #2
0
 public static void main(java.lang.String[] args) throws RecognitionException {
   CharStream cs =
       new ANTLRStringStream(
           "package gw.internal.gosu.parser.java;\n"
               + "import java.util.List;\n"
               + "public class Foo {\n"
               + "int a = 1_2_3;\n"
               + "  public static void main(String[] args) {}\n"
               + "}");
   JavaLexer lexer = new JavaLexer(cs);
   TokenRewriteStream tokens = new TokenRewriteStream(lexer);
   JavaParser parser = new JavaParser(tokens);
   TreeBuilder treeBuilder = new TreeBuilder();
   parser.setTreeBuilder(treeBuilder);
   parser.compilationUnit();
   dumpJavaAST(treeBuilder.getTree());
 }
 /* A definite node is constructed from a specified number of
 children.  That number of nodes are popped from the stack and
 made the children of the definite node.  Then the definite node
 is pushed on to the stack. */
 void closeNodeScope(Node n, int num) throws ParseException {
   SimpleNode sn = (SimpleNode) n;
   mk = marks.pop();
   SimpleNode newNode = null;
   try {
     newNode = builder.closeNode(sn, num);
   } catch (ParseException exc) {
     throw exc;
   } catch (Exception exc) {
     exc.printStackTrace();
     throw new ParseException("Internal error:" + exc);
   }
   if (newNode == null) {
     throw new ParseException("Internal AST builder error");
   }
   pushNode(newNode);
   node_created = true;
 }
 /* A conditional node is constructed if its condition is true.  All
 the nodes that have been pushed since the node was opened are
 made children of the the conditional node, which is then pushed
 on to the stack.  If the condition is false the node is not
 constructed and they are left on the stack. */
 void closeNodeScope(Node n, boolean condition) throws ParseException {
   SimpleNode sn = (SimpleNode) n;
   if (condition) {
     SimpleNode newNode = null;
     try {
       newNode = builder.closeNode(sn, nodeArity());
     } catch (ParseException exc) {
       throw exc;
     } catch (Exception exc) {
       exc.printStackTrace();
       throw new ParseException("Internal error:" + exc);
     }
     if (newNode == null) {
       throw new ParseException("Internal AST builder error");
     }
     mk = marks.pop();
     pushNode(newNode);
     node_created = true;
   } else {
     mk = marks.pop();
     node_created = false;
   }
 }
 public Node openNode(int id) {
   return builder.openNode(id);
 }
Example #6
0
 /**
  * Parse HTML into a Document.
  *
  * @param html HTML to parse
  * @param baseUri base URI of document (i.e. original fetch location), for resolving relative
  *     URLs.
  * @return parsed Document
  */
 public static Document parse(String html, String baseUri) {
   TreeBuilder treeBuilder = new HtmlTreeBuilder();
   return treeBuilder.parse(html, baseUri, ParseErrorList.noTracking());
 }
Example #7
0
 public Document parseInput(String html, String baseUri) {
   errors = isTrackErrors() ? ParseErrorList.tracking(maxErrors) : ParseErrorList.noTracking();
   Document doc = treeBuilder.parse(html, baseUri, errors);
   return doc;
 }
 @Override
 public BindingBuilder createBinding(TreeBuilder<?> list) {
   return list.observeContents();
 }
Example #9
0
  /** @param args */
  public static void main(final String[] args) throws Exception {
    final BufferedReader br =
        new BufferedReader(
            new InputStreamReader(
                (new GZIPInputStream(
                    new FileInputStream(
                        new File(
                            new File(System.getProperty("user.dir")), "testdata/mobo1.txt.gz"))))));

    final List<Instance> instances = Lists.newLinkedList();

    int count = 0;
    while (true) {
      count++;
      final String line = br.readLine();
      if (line == null) {
        break;
      }
      final JSONObject jo = (JSONObject) JSONValue.parse(line);
      final HashMapAttributes a = new HashMapAttributes();
      a.putAll((JSONObject) jo.get("attributes"));
      Instance instance = new Instance(a, (String) jo.get("output"));
      instances.add(instance);
    }

    final List<Instance> train = instances.subList(0, instances.size() / 2);
    final List<Instance> test = instances.subList(instances.size() / 2 + 1, instances.size() - 1);

    System.out.println("Read " + instances.size() + " instances");

    System.out.println("Testing scorers with single decision node");
    for (final Scorer scorer : Sets.newHashSet(new Scorer1())) {
      final TreeBuilder tb = new TreeBuilder(scorer);

      final long startTime = System.currentTimeMillis();
      final Node tree = tb.buildPredictiveModel(train).node;
      System.out.println(
          scorer.getClass().getSimpleName()
              + " build time "
              + (System.currentTimeMillis() - startTime)
              + ", size: "
              + tree.size()
              + " mean depth: "
              + tree.meanDepth());

      int correctlyClassified = 0;
      for (Instance testInstance : test) {
        String result = (String) tree.getLeaf(testInstance.getAttributes()).getBestClassification();
        if (result.equals(testInstance.getClassification())) {
          correctlyClassified++;
        }
      }
      System.out.println(", accuracy: " + (double) correctlyClassified / test.size());

      System.out.println("Testing random forest");

      for (int i = 2; i <= 20; i++) {
        RandomForestBuilder rfBuilder = new RandomForestBuilder(new TreeBuilder());
        RandomForest randomForest = rfBuilder.buildPredictiveModel(train);
        correctlyClassified = 0;
        for (Instance testInstance : test) {
          Serializable result =
              randomForest.getClassificationByMaxProb(testInstance.getAttributes());
          if (result.equals(testInstance.getClassification())) {
            correctlyClassified++;
          }
        }
        System.out.println(
            "accuracy with " + i + " trees: " + (double) correctlyClassified / test.size());
        // ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new
        // File("baggedTree.ser")));
        // out.writeObject(baggedTree);
      }
    }
  }