Example usage for edu.stanford.nlp.trees Tree pennString

List of usage examples for edu.stanford.nlp.trees Tree pennString

Introduction

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

Prototype

public String pennString() 

Source Link

Document

Calls pennPrint() and saves output to a String

Usage

From source file:qmul.corpus.CorpusParser.java

License:Open Source License

/**
 * Parse a corpus, replacing any existing syntactic annotation unless isLeaveExisting()
 * /*from w w w.ja  va 2  s.com*/
 * @param corpus
 *            the corpus to parse (which gets modified)
 * @return the number of {@link DialogueSentence}s actually affected (i.e. successfully parsed)
 */
public static int parse(DialogueCorpus corpus) {
    if (parser == null) {
        System.err.println("WARNING: null parser, setting default ...");
        setParser();
    }
    int iD = 0;
    int iS = 0;
    int iP = 0;
    for (Dialogue d : corpus.getDialogues()) {
        System.out.println("Parsing dialogue " + ++iD + " of " + corpus.getDialogues().size());
        for (DialogueSentence s : d.getSents()) {
            if (leaveExisting && (s.getSyntax() != null)) {
                continue;
            }
            List<? extends HasWord> words = s.getTokens();
            if (words == null) {
                words = tok.getWordsFromString(s.getTranscription());
            }
            if ((words == null) || words.isEmpty()) {
                System.err.println("No words in sentence " + ++iS + " " + s.getNum() + ", skipping ...");
                continue;
            } else {
                System.err.print("Parsing sentence " + ++iS + " " + s.getNum() + " " + words + " ...");
            }
            boolean success = parser.parse(words);
            if (success) {
                iP++;
                Tree t = null;
                if (parser instanceof LexicalizedParser) {
                    t = ((LexicalizedParser) parser).getBestParse();
                } else if (parser instanceof TreeParser) {
                    // why doesn't the Stanford Parser interface include a method for returning the tree??
                    t = ((TreeParser) parser).getBestParse();
                } else {
                    throw new RuntimeException("unknown parser class " + parser);
                }
                System.err.println(" success!\n" + t.pennString());
                s.setSyntax(t);
            } else {
                System.err.println(" failed.");
                s.setSyntax(null);
            }
            System.out.println(
                    "Mem " + Runtime.getRuntime().freeMemory() + " " + Runtime.getRuntime().totalMemory());
            System.out.println("Parser done " + iD + " dialogues, " + iS + " sentences ...");
        }
    }
    System.out.println("Finished (parsed " + iP + " sentences)");
    return iP;
}

From source file:wtute.parser.EssayParser.java

private String parse(Tree toParse) {
    GrammaticalStructure gs = gsf.newGrammaticalStructure(toParse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    toParse.pennPrint();/*from www . j  a v  a 2  s . c om*/
    System.out.println(tdl);
    return toParse.pennString() + "\n" + tdl + "\n" + toParse.taggedYield() + "\n\n";
}