Skip to content

Commit

Permalink
Add the choice of dependency graph to output to the TextOutputter, as…
Browse files Browse the repository at this point in the history
… requested in #1339
  • Loading branch information
AngledLuffa committed Feb 19, 2023
1 parent 5c085f0 commit 33e6c42
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/edu/stanford/nlp/pipeline/AnnotationOutputter.java
Expand Up @@ -2,6 +2,7 @@

import edu.stanford.nlp.ling.AnnotationLookup;
import edu.stanford.nlp.ling.CoreAnnotation;
import edu.stanford.nlp.semgraph.SemanticGraphFactory;
import edu.stanford.nlp.trees.TreePrint;
import edu.stanford.nlp.util.PropertiesUtils;

Expand Down Expand Up @@ -38,6 +39,8 @@ public abstract class AnnotationOutputter {

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

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

public static class Options {

Expand Down Expand Up @@ -66,7 +69,8 @@ public static class Options {
/** Print some fake dependency info in the CoNLL output.
Useful for the original conll eval script, for example */
public final boolean printFakeDeps;

/** Which graph to output if we only output one */
public final SemanticGraphFactory.Mode semanticGraphMode;

public Options() {
// this creates the default options object
Expand All @@ -84,6 +88,7 @@ public Options(boolean pretty) {
relationsBeam = 0.0;
keysToPrint = getKeysToPrint(DEFAULT_KEYS);
printFakeDeps = false;
semanticGraphMode = DEFAULT_SEMANTIC_GRAPH;
}

public Options(Properties properties) {
Expand All @@ -99,6 +104,12 @@ public Options(Properties properties) {
relationsBeam = PropertiesUtils.getDouble(properties, "output.relation.beam", 0.0);
keysToPrint = getKeysToPrint(properties.getProperty("output.columns", DEFAULT_KEYS));
printFakeDeps = PropertiesUtils.getBool(properties, "output.printFakeDeps", false);
String graphMode = properties.getProperty("output.dependencyType", null);
if (graphMode == null) {
semanticGraphMode = DEFAULT_SEMANTIC_GRAPH;
} else {
semanticGraphMode = SemanticGraphFactory.Mode.valueOf(graphMode.toUpperCase());
}
}

private static List<Class<? extends CoreAnnotation<?>>> getKeysToPrint(String columns) {
Expand Down
33 changes: 30 additions & 3 deletions src/edu/stanford/nlp/pipeline/TextOutputter.java
Expand Up @@ -19,6 +19,7 @@
import edu.stanford.nlp.naturalli.NaturalLogicAnnotations;
import edu.stanford.nlp.naturalli.OpenIE;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations;
Expand Down Expand Up @@ -148,10 +149,36 @@ private static void print(Annotation annotation, PrintWriter pw, Options options
// case we don't want to recreate them using the dependency
// printer. This might be relevant if using CoreNLP for a
// language which doesn't have dependencies, for example.
if (sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class) != null) {
final SemanticGraph graph;
final String graphName;
switch (options.semanticGraphMode) {
case BASIC:
graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
graphName = "basic dependencies";
break;
case ENHANCED:
graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
graphName = "enhanced dependencies";
break;
case ENHANCED_PLUS_PLUS:
graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
graphName = "enhanced plus plus dependencies";
break;
case COLLAPSED:
graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
graphName = "collapsed dependencies";
break;
case CCPROCESSED:
graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
graphName = "cc processed dependencies";
break;
default:
throw new RuntimeException("Sorry, but " + options.semanticGraphMode + " dependencies cannot be output as part of the TextOutputter");
}
if (graph != null) {
pw.println();
pw.println("Dependency Parse (enhanced plus plus dependencies):");
pw.print(sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class).toList());
pw.println("Dependency Parse (" + graphName + "):");
pw.print(graph.toList());
}

// display the entity mentions
Expand Down

0 comments on commit 33e6c42

Please sign in to comment.