Example usage for edu.stanford.nlp.trees PennTreeReader readTree

List of usage examples for edu.stanford.nlp.trees PennTreeReader readTree

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees PennTreeReader readTree.

Prototype

@Override
public Tree readTree() throws IOException 

Source Link

Document

Reads a single tree in standard Penn Treebank format from the input stream.

Usage

From source file:qmul.util.parse.RASPParser.java

License:Open Source License

@Override
public Tree getBestParse() {
    PennTreeReader ptr = new PennTreeReader(new StringReader(buffer), new LabeledScoredTreeFactory());
    ArrayList<Tree> trees = new ArrayList<Tree>();
    try {/* w w w .  jav  a2s  .  c om*/
        Tree tree = null;
        while ((tree = ptr.readTree()) != null) {
            // System.out.println("got tree " + tree);
            trees.add(tree);
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }
    // List<Tree> trees = CreateTreeFromSWBD.makeTrees(buffer);
    if (trees.size() > 0) {
        return trees.get(0);
    } else {
        return null;
    }
}

From source file:tml.utils.StanfordUtils.java

License:Apache License

/**
 * Calculates a Penn grammatical tree from its string representation
 * @param pennTreeString the string/*  ww w  .  java  2  s  . c  om*/
 * @return the grammar tree
 * @throws Exception
 */
public static Tree getTreeFromString(String passageId, String pennTreeString) {
    double time = System.nanoTime();
    time = System.nanoTime() - time;
    Tree t = null;
    if (pennTreeCache.containsKey(passageId)) {
        t = pennTreeCache.get(passageId);
    } else {
        LabeledScoredTreeFactory tf = new LabeledScoredTreeFactory();
        PennTreeReader reader = new PennTreeReader(new StringReader(pennTreeString), tf);
        try {
            t = reader.readTree();
            pennTreeCache.put(passageId, t);
        } catch (IOException e) {
            logger.error("Error parsing penntree string length " + pennTreeString.length());
            e.printStackTrace();
            return null;
        }
    }
    logger.debug("PennTree calculated in " + time * 10E-6 + " milliseconds.");
    return t;
}