Skip to content

Commit

Permalink
Rename Writer objects to not be called os
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed Feb 14, 2015
1 parent 0bb5443 commit 53734cd
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/edu/stanford/nlp/pipeline/TextOutputter.java
Expand Up @@ -37,7 +37,7 @@ public void print(Annotation annotation, OutputStream stream, Options options) t
/** /**
* The meat of the outputter * The meat of the outputter
*/ */
private static void print(Annotation annotation, PrintWriter os, Options options) throws IOException { private static void print(Annotation annotation, PrintWriter pw, Options options) throws IOException {
double beam = options.beamPrintingOption; double beam = options.beamPrintingOption;


List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
Expand All @@ -48,80 +48,81 @@ private static void print(Annotation annotation, PrintWriter os, Options options
List<CoreLabel> tokens = annotation.get(CoreAnnotations.TokensAnnotation.class); List<CoreLabel> tokens = annotation.get(CoreAnnotations.TokensAnnotation.class);
int nSentences = (sentences != null)? sentences.size():0; int nSentences = (sentences != null)? sentences.size():0;
int nTokens = (tokens != null)? tokens.size():0; int nTokens = (tokens != null)? tokens.size():0;
os.printf("Document: ID=%s (%d sentences, %d tokens)%n", docId, nSentences, nTokens); pw.printf("Document: ID=%s (%d sentences, %d tokens)%n", docId, nSentences, nTokens);
} }


// Display doctitle if available // Display doctitle if available
String docTitle = annotation.get(CoreAnnotations.DocTitleAnnotation.class); String docTitle = annotation.get(CoreAnnotations.DocTitleAnnotation.class);
if (docTitle != null) { if (docTitle != null) {
os.printf("Document Title: %s%n", docTitle); pw.printf("Document Title: %s%n", docTitle);
} }


// Display docdate if available // Display docdate if available
String docDate = annotation.get(CoreAnnotations.DocDateAnnotation.class); String docDate = annotation.get(CoreAnnotations.DocDateAnnotation.class);
if (docDate != null) { if (docDate != null) {
os.printf("Document Date: %s%n", docDate); pw.printf("Document Date: %s%n", docDate);
} }


// Display doctype if available // Display doctype if available
String docType = annotation.get(CoreAnnotations.DocTypeAnnotation.class); String docType = annotation.get(CoreAnnotations.DocTypeAnnotation.class);
if (docType != null) { if (docType != null) {
os.printf("Document Type: %s%n", docType); pw.printf("Document Type: %s%n", docType);
} }


// Display docsourcetype if available // Display docsourcetype if available
String docSourceType = annotation.get(CoreAnnotations.DocSourceTypeAnnotation.class); String docSourceType = annotation.get(CoreAnnotations.DocSourceTypeAnnotation.class);
if (docSourceType != null) { if (docSourceType != null) {
os.printf("Document Source Type: %s%n", docSourceType); pw.printf("Document Source Type: %s%n", docSourceType);
} }


// display each sentence in this annotation // display each sentence in this annotation
if (sentences != null) { if (sentences != null) {
for(int i = 0, sz = sentences.size(); i < sz; i ++) { for (int i = 0, sz = sentences.size(); i < sz; i ++) {
CoreMap sentence = sentences.get(i); CoreMap sentence = sentences.get(i);
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class); List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
os.printf("Sentence #%d (%d tokens):%n", (i + 1), tokens.size()); pw.printf("Sentence #%d (%d tokens):%n", (i + 1), tokens.size());


String text = sentence.get(CoreAnnotations.TextAnnotation.class); String text = sentence.get(CoreAnnotations.TextAnnotation.class);
os.println(text); pw.println(text);


// display the token-level annotations // display the token-level annotations
String[] tokenAnnotations = { String[] tokenAnnotations = {
"Text", "PartOfSpeech", "Lemma", "Answer", "NamedEntityTag", "CharacterOffsetBegin", "CharacterOffsetEnd", "NormalizedNamedEntityTag", "Timex", "TrueCase", "TrueCaseText" }; "Text", "PartOfSpeech", "Lemma", "Answer", "NamedEntityTag", "CharacterOffsetBegin", "CharacterOffsetEnd", "NormalizedNamedEntityTag", "Timex", "TrueCase", "TrueCaseText" };
for (CoreLabel token: tokens) { for (CoreLabel token: tokens) {
os.print(token.toShorterString(tokenAnnotations)); pw.print(token.toShorterString(tokenAnnotations));
os.println(); pw.println();
} }


// display the parse tree for this sentence // display the parse tree for this sentence
Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
if (tree != null) { if (tree != null) {
options.constituentTreePrinter.printTree(tree, os); options.constituentTreePrinter.printTree(tree, pw);
} }


// It is possible to turn off the semantic graphs, in which // It is possible to turn off the semantic graphs, in which
// case we don't want to recreate them using the dependency // case we don't want to recreate them using the dependency
// printer. This might be relevant if using corenlp for a // printer. This might be relevant if using CoreNLP for a
// language which doesn't have dependencies, for example. // language which doesn't have dependencies, for example.
if (sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class) != null) { if (sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class) != null) {
os.print(sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class).toList()); pw.print(sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class).toList());
os.println(); pw.println();
} }


// display MachineReading entities and relations // display MachineReading entities and relations
List<EntityMention> entities = sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class); List<EntityMention> entities = sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class);
if (entities != null) { if (entities != null) {
os.println("Extracted the following MachineReading entity mentions:"); pw.println("Extracted the following MachineReading entity mentions:");
for (EntityMention e : entities) { for (EntityMention e : entities) {
os.println("\t" + e); pw.print('\t');
pw.println(e);
} }
} }
List<RelationMention> relations = sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class); List<RelationMention> relations = sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);
if(relations != null){ if (relations != null){
os.println("Extracted the following MachineReading relation mentions:"); pw.println("Extracted the following MachineReading relation mentions:");
for(RelationMention r: relations){ for (RelationMention r: relations) {
if(r.printableObject(beam)){ if (r.printableObject(beam)) {
os.println(r); pw.println(r);
} }
} }
} }
Expand All @@ -146,10 +147,10 @@ private static void print(Annotation annotation, PrintWriter os, Options options
continue; continue;
if (!outputHeading) { if (!outputHeading) {
outputHeading = true; outputHeading = true;
os.println("Coreference set:"); pw.println("Coreference set:");
} }
// all offsets start at 1! // all offsets start at 1!
os.printf("\t(%d,%d,[%d,%d]) -> (%d,%d,[%d,%d]), that is: \"%s\" -> \"%s\"%n", pw.printf("\t(%d,%d,[%d,%d]) -> (%d,%d,[%d,%d]), that is: \"%s\" -> \"%s\"%n",
mention.sentNum, mention.sentNum,
mention.headIndex, mention.headIndex,
mention.startIndex, mention.startIndex,
Expand All @@ -163,7 +164,7 @@ private static void print(Annotation annotation, PrintWriter os, Options options
} }
} }
} }
os.flush(); pw.flush();
} }


/** Static helper */ /** Static helper */
Expand All @@ -172,9 +173,9 @@ public static void prettyPrint(Annotation annotation, OutputStream stream, Stanf
} }


/** Static helper */ /** Static helper */
public static void prettyPrint(Annotation annotation, PrintWriter os, StanfordCoreNLP pipeline) { public static void prettyPrint(Annotation annotation, PrintWriter pw, StanfordCoreNLP pipeline) {
try { try {
TextOutputter.print(annotation, os, getOptions(pipeline)); TextOutputter.print(annotation, pw, getOptions(pipeline));
// already flushed // already flushed
// don't close, might not want to close underlying stream // don't close, might not want to close underlying stream
} catch (IOException e) { } catch (IOException e) {
Expand Down

0 comments on commit 53734cd

Please sign in to comment.