List of usage examples for edu.stanford.nlp.trees Tree pennPrint
public void pennPrint(PrintStream ps)
From source file:edu.umn.biomedicus.gpl.stanford.parser.StanfordConstituencyParserModel.java
License:Open Source License
public void parseSentence(TextRange sentenceLabel, LabelIndex<ParseToken> parseTokenLabelIndex, LabelIndex<PosTag> partOfSpeechLabelIndex, Labeler<ConstituencyParse> constituencyParseLabeler) { List<TaggedWord> taggedWordList = new ArrayList<>(); for (ParseToken parseTokenLabel : parseTokenLabelIndex.inside(sentenceLabel)) { String word = parseTokenLabel.getText(); PartOfSpeech partOfSpeech = partOfSpeechLabelIndex.firstAtLocation(parseTokenLabel).getPartOfSpeech(); TaggedWord taggedWord = new TaggedWord(word, PartsOfSpeech.tagForPartOfSpeech(partOfSpeech)); taggedWordList.add(taggedWord);//from w w w .j a va 2 s . co m } Tree tree = shiftReduceParser.apply(taggedWordList); StringWriter stringWriter = new StringWriter(); tree.pennPrint(new PrintWriter(stringWriter)); String pennPrint = stringWriter.toString(); ConstituencyParse constituencyParse = new ConstituencyParse(sentenceLabel, pennPrint); constituencyParseLabeler.add(constituencyParse); }
From source file:service.AnnotationProcessor.java
License:Apache License
public static void annotate(final String textInput, final String outputFileName) throws IOException { PrintWriter xmlOut = new PrintWriter(outputFileName); Properties props = new Properties(); // props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, // parse, cleanxml"); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation(textInput); pipeline.annotate(annotation);/*from w ww .ja v a2 s .c om*/ pipeline.xmlPrint(annotation, xmlOut); // An Annotation is a Map and you can get and use the // various analyses individually. For instance, this // gets the parse tree of the 1st sentence in the text. List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); if (sentences != null && sentences.size() > 0) { CoreMap sentence = sentences.get(0); Tree tree = sentence.get(TreeAnnotation.class); PrintWriter out = new PrintWriter(System.out); out.println("The first sentence parsed is:"); if (tree != null) { tree.pennPrint(out); } } }