0

I'm processing a batch of text files, and I need to use the Stanford parser's output as a numeric list of nodes and edges where Nodes have IDs and labels, edges consist of two node ids and an edge weight like:

Node List:   1  A ,  2  B...
Edge list:  1 2 10, 2 1 10...

According to the Stanford NLP javadoc -->Class SemanticGraph:

There is no mechanism for returning all edges at once (eg edgeSet()). This is intentional. Use edgeIterable() to iterate over the edges if necessary.

How to do it? I tried this code:

import java.io.*;
import java.util.*;

import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;

public class StanfordCoreNlpSemGraph {
  public static void main(String[] args) throws IOException {
    PrintWriter out;
    if (args.length > 1) {
      out = new PrintWriter(args[1]);
    } else {
      out = new PrintWriter(System.out);
    }
    PrintWriter xmlOut = null;
    if (args.length > 2) {
      xmlOut = new PrintWriter(args[2]);
    }

    StanfordCoreNLP pipeline = new StanfordCoreNLP();
    Annotation annotation;
    if (args.length > 0) {
      annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[0]));
    } else {
      annotation = new Annotation("This is the first annotation.");
    }

    pipeline.annotate(annotation);
    pipeline.prettyPrint(annotation, out);
    if (xmlOut != null) {
      pipeline.xmlPrint(annotation, xmlOut);
    }
      // An Annotation is a Map.
     // For instance, this gets the parse tree of the first sentence. 

    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    if (sentences != null && sentences.size() > 0) {
      CoreMap sentence = sentences.get(0);
      Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
      out.println();
      out.println("The first sentence parsed is:");
      tree.pennPrint(out);
      Object IndexedWord;
      SemanticGraph sg = new SemanticGraph();

      SemanticGraphEdge edge = new SemanticGraphEdge(edge);

      for (SemanticGraphEdge edge : sg.edgeIterable()) 
      {
        int headIndex = edge.getGovernor().index();
        int depIndex = edge.getDependent().index();
        int weight = 1; // "edge weight"-- should it be the 
        // sum of the weights of the 
        // selected edges? 
        System.out.printf("%d %d %d%n", headIndex, depIndex, weight);
      }  
    }
  }
}

But it throws an error: Duplicate local variable edge StanfordCoreNlpSemGraph.java /stan-nlp/src line 60

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
arvega
  • 3
  • 4
  • Hi @arvega — are you still stuck on this? Looks like you could accomplish what you want with the `edgeIterable()` method you mentioned. – Jon Gauthier Mar 15 '15 at 19:39
  • Hi @Jon Gauthier : Yes, i'm still stuck on trying extract the nodes and edges from the coreNLP output. The javadoc description of the Class SemanticGraph turned out be not sufficient for my inexpert understanding. I'd greatly appreciate your suggestions how to get it by example (it's just a dream though) . THX! – arvega Mar 16 '15 at 00:27

1 Answers1

1

Here's a basic example of forming the edge list. (The node list part should be easy — you just need to iterate over the tokens in the sentence and print them out.)

SemanticGraph sg = ....
for (SemanticGraphEdge edge : sg.getEdgesIterable()) {
  int headIndex = edge.getGovernor().index();
  int depIndex = edge.getDependent().index();
  int weight = ... // Not sure what "edge weight" you want here.
  System.out.printf("%d %d %d%n", headIndex, depIndex, weight);
}
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • G'd afternoon, @Jon Gautier: Many thanks for the snippet! Will try it tonight. Regards, A – arvega Mar 17 '15 at 00:19
  • Hi @Jon Gautier. Thanks again for your suggestion but I didn't succeed to fix these two errors: SemanticGraph cannot be resolved to a type --> SemanticGraph sg = sg.get(StanfordCoreNlpSemGraph.class); SemanticGraphEdge cannot be resolved to a type --> for (SemanticGraphEdge edge : sg.getEdgesIterable()) Also, given that it might be the sum of the weights of the selected edges (?), i assigned the "value" of "1" to the graph weight though not sure could it be kinda default? --> int weight = 1; Could you kindly help me resolve these errors, if available, please? – arvega Mar 23 '15 at 17:14
  • Hi, @Jon Gautier. Thanks for your suggestion marked "Answer": very helpful indeed. Only problem: (SemanticGraphEdge edge : sg.getEdgesIterable()) throws an error: -The method getEdgesIterable() is undefined for the type SemanticGraph - SemanticGraphEdge cannot be resolved to a type How to fix this error? Thanks. – arvega Apr 01 '15 at 09:07
  • Since the suggested by Jon Gauthier method getEdgesIterable() is undefined for the type SemanticGraph, while the SemanticGraphEdge cannot be resolved to a type, I tried to modify this block as: – arvega Apr 03 '15 at 04:14
  • Since the suggested by Jon Gauthier method getEdgesIterable() is undefined for the type SemanticGraph, while the SemanticGraphEdge cannot be resolved to a type, I tried to modify this block as: SemanticGraph sg = new SemanticGraph(); SemanticGraphEdge edge = new SemanticGraphEdge(edge); for (SemanticGraphEdge edge : sg.edgeIterable()) { int headIndex = edge.getGovernor().index(); int depIndex = edge.getDependent().index(); but getting new error: Duplicate local variable 'edge'. - I don't know how to solve it. Would somebody help me, plz. – arvega Apr 03 '15 at 04:29
  • You shouldn't be instantiating new `SemanticGraphEdge` instances. To fix your original problem, just import the requisite class: `import edu.stanford.nlp.semgraph.SemanticGraphEdge;` – Jon Gauthier Apr 04 '15 at 03:48
  • Thank you. In fact, it was already imported. May be give a try modifying SemanticGraph sg = ... to (SemanticGraph)sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class); ? Thanks again. – arvega Apr 06 '15 at 02:36
  • there it is as of March 23: – arvega Apr 06 '15 at 02:42
  • Gautier: Yes, it works now. Thank you. How to mark your last suggestion as an Answer? – arvega Apr 06 '15 at 17:48
  • Just use: ```for (SemanticGraphEdge edge : sg.edgeIterable()) {``` – Melroy van den Berg Aug 01 '15 at 21:49