Skip to content

Commit 33e6c42

Browse files
committed
Add the choice of dependency graph to output to the TextOutputter, as requested in #1339
1 parent 5c085f0 commit 33e6c42

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/edu/stanford/nlp/pipeline/AnnotationOutputter.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.stanford.nlp.ling.AnnotationLookup;
44
import edu.stanford.nlp.ling.CoreAnnotation;
5+
import edu.stanford.nlp.semgraph.SemanticGraphFactory;
56
import edu.stanford.nlp.trees.TreePrint;
67
import edu.stanford.nlp.util.PropertiesUtils;
78

@@ -38,6 +39,8 @@ public abstract class AnnotationOutputter {
3839

3940
private static final Options DEFAULT_OPTIONS = new Options(); // IMPORTANT: must come after DEFAULT_CONSTITUENCY_TREE_PRINTER
4041

42+
/** Some outputters output all of the SemanticGraphs. Others may just output one */
43+
private static final SemanticGraphFactory.Mode DEFAULT_SEMANTIC_GRAPH = SemanticGraphFactory.Mode.ENHANCED_PLUS_PLUS;
4144

4245
public static class Options {
4346

@@ -66,7 +69,8 @@ public static class Options {
6669
/** Print some fake dependency info in the CoNLL output.
6770
Useful for the original conll eval script, for example */
6871
public final boolean printFakeDeps;
69-
72+
/** Which graph to output if we only output one */
73+
public final SemanticGraphFactory.Mode semanticGraphMode;
7074

7175
public Options() {
7276
// this creates the default options object
@@ -84,6 +88,7 @@ public Options(boolean pretty) {
8488
relationsBeam = 0.0;
8589
keysToPrint = getKeysToPrint(DEFAULT_KEYS);
8690
printFakeDeps = false;
91+
semanticGraphMode = DEFAULT_SEMANTIC_GRAPH;
8792
}
8893

8994
public Options(Properties properties) {
@@ -99,6 +104,12 @@ public Options(Properties properties) {
99104
relationsBeam = PropertiesUtils.getDouble(properties, "output.relation.beam", 0.0);
100105
keysToPrint = getKeysToPrint(properties.getProperty("output.columns", DEFAULT_KEYS));
101106
printFakeDeps = PropertiesUtils.getBool(properties, "output.printFakeDeps", false);
107+
String graphMode = properties.getProperty("output.dependencyType", null);
108+
if (graphMode == null) {
109+
semanticGraphMode = DEFAULT_SEMANTIC_GRAPH;
110+
} else {
111+
semanticGraphMode = SemanticGraphFactory.Mode.valueOf(graphMode.toUpperCase());
112+
}
102113
}
103114

104115
private static List<Class<? extends CoreAnnotation<?>>> getKeysToPrint(String columns) {

src/edu/stanford/nlp/pipeline/TextOutputter.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import edu.stanford.nlp.naturalli.NaturalLogicAnnotations;
2020
import edu.stanford.nlp.naturalli.OpenIE;
2121
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
22+
import edu.stanford.nlp.semgraph.SemanticGraph;
2223
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
2324
import edu.stanford.nlp.trees.Tree;
2425
import edu.stanford.nlp.trees.TreeCoreAnnotations;
@@ -148,10 +149,36 @@ private static void print(Annotation annotation, PrintWriter pw, Options options
148149
// case we don't want to recreate them using the dependency
149150
// printer. This might be relevant if using CoreNLP for a
150151
// language which doesn't have dependencies, for example.
151-
if (sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class) != null) {
152+
final SemanticGraph graph;
153+
final String graphName;
154+
switch (options.semanticGraphMode) {
155+
case BASIC:
156+
graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
157+
graphName = "basic dependencies";
158+
break;
159+
case ENHANCED:
160+
graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
161+
graphName = "enhanced dependencies";
162+
break;
163+
case ENHANCED_PLUS_PLUS:
164+
graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
165+
graphName = "enhanced plus plus dependencies";
166+
break;
167+
case COLLAPSED:
168+
graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
169+
graphName = "collapsed dependencies";
170+
break;
171+
case CCPROCESSED:
172+
graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
173+
graphName = "cc processed dependencies";
174+
break;
175+
default:
176+
throw new RuntimeException("Sorry, but " + options.semanticGraphMode + " dependencies cannot be output as part of the TextOutputter");
177+
}
178+
if (graph != null) {
152179
pw.println();
153-
pw.println("Dependency Parse (enhanced plus plus dependencies):");
154-
pw.print(sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class).toList());
180+
pw.println("Dependency Parse (" + graphName + "):");
181+
pw.print(graph.toList());
155182
}
156183

157184
// display the entity mentions

0 commit comments

Comments
 (0)