Example usage for edu.stanford.nlp.trees LabeledScoredTreeNode LabeledScoredTreeNode

List of usage examples for edu.stanford.nlp.trees LabeledScoredTreeNode LabeledScoredTreeNode

Introduction

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

Prototype

public LabeledScoredTreeNode(Label label, List<Tree> daughterTreesList) 

Source Link

Document

Create parse tree with given root and array of daughter trees.

Usage

From source file:qmul.align.TurnConcatSimilarityMeasure.java

License:Open Source License

/**
 * @param t//  w w  w .jav a  2s . c o m
 * @return the result of concatenating all sentences linearly for transcription, as daughters of a "TURN" mother for
 *         syntax (unless there's just one, in which case it's just copied without a TURN mother, to prevent false
 *         positive similarity between single-sent turns)
 */
private DialogueSentence concatTurn(DialogueTurn t) {
    DialogueSentence cs = new DialogueSentence(null, 0, t, "");
    System.out.print("Concatenating sentences for turn " + t.getId());
    for (DialogueSentence s : t.getSents()) {
        System.out.print(".");
        if (s.getTranscription() != null) {
            cs.setTranscription((cs.getTranscription() + " " + s.getTranscription()).trim());
        }
        if (s.getTokens() != null) {
            if (cs.getTokens() == null) {
                cs.setTokens(s.getTokens());
            } else {
                cs.getTokens().addAll(s.getTokens());
            }
        }
        if (s.getSyntax() != null) {
            Tree tree;
            if (cs.getSyntax() == null) {
                tree = s.getSyntax();
            } else {
                ArrayList<Tree> dtrs = new ArrayList<Tree>();
                if (cs.getSyntax().label().value().equals("TURN")) {
                    for (Tree child : cs.getSyntax().getChildrenAsList()) {
                        dtrs.add(child);
                    }
                } else {
                    dtrs.add(cs.getSyntax());
                }
                dtrs.add(s.getSyntax());
                tree = new LabeledScoredTreeNode(new StringLabel("TURN"), dtrs);
            }
            cs.setSyntax(tree);
        }
        if (!Double.isNaN(s.getSyntaxProb())) {
            cs.setSyntaxProb(Double.isNaN(cs.getSyntaxProb()) ? s.getSyntaxProb()
                    : (cs.getSyntaxProb() * s.getSyntaxProb()));
        }
    }
    System.out.println(" done.");
    return cs;
}

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

License:Open Source License

/**
 * Convenience method: splits utt into sentences, uses {@link LexicalizedParser}'s parse() to tokenize and parse
 * each sentence/*from w ww . j a va2  s  .  co m*/
 * 
 * @param utt
 * @return a {@link Tree} with ROOT node, with the getBestParse() trees for each sentence as children
 */
public Tree parse(String utt) {
    String[] sentences = utt.split("[.!?]");
    // System.out.println("there are sentences:" + sentences.length);
    // LinkedList<Tree> list=new LinkedList<Tree>();
    Label rootLabel = new StringLabel("ROOT");
    Tree concat = new LabeledScoredTreeNode(rootLabel, new LinkedList<Tree>());

    try {
        for (int i = 0; i < sentences.length; i++) {
            boolean parsed = false;
            if (sentences[i].length() > 0)
                parsed = lp.parse(sentences[i]);
            else
                continue;
            Tree t = lp.getBestParse();
            Tree rootChild;
            if (t.children().length == 1)
                rootChild = t.removeChild(0);
            else
                rootChild = t;
            concat.addChild(rootChild);
        }
        if (concat.children().length > 1)
            return concat;
        else
            return concat.removeChild(0);
    } catch (Throwable t) {
        System.out.println(t.getMessage());
        System.out.println("Reinitializing parser because of trying to parse error " + utt);
        this.lp = null;
        Runtime r = Runtime.getRuntime();
        r.gc();
        lp = new LexicalizedParser(System.getProperty("user.dir") + File.separator + "utils" + File.separator
                + "englishPCFG.ser.gz");
        this.lp.setOptionFlags(new String[] { "-maxLength", "100", "-retainTmpSubcategories" });
        return null;
    }

}