java - 如何获取斯坦福解析器输出作为节点和边的列表?

标签 java graph nodes stanford-nlp edges

我正在处理一批文本文件,我需要使用斯坦福解析器的输出作为节点和边的数字列表,其中节点具有ID和标签, 由两个节点 ID 和一个边权重组成,例如:

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

根据斯坦福 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.

怎么做呢?我尝试了这段代码:

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);
      }  
    }
  }
}

但它抛出一个错误:重复局部变量边缘StanfordCoreNlpSemGraph.java/stan-nlp/src line 60

最佳答案

这是形成边缘列表的基本示例。 (节点列表部分应该很简单 - 您只需迭代句子中的标记并将其打印出来。)

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);
}

关于java - 如何获取斯坦福解析器输出作为节点和边的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28866301/

相关文章:

java - Eclipse Swing 到 JavaC?

java - Oracle ADF 或 Java

java - 用 Jena 加载 owl 文件

java - 带 super 的通配符

ios:Coreplot,无法在一张图表上绘制多个图

ios - 带有值和日期图表 iOS 的折线图

php - 解析XML : Get child nodes for each parent node

algorithm - 将给定集合中的最大可能边添加到具有节点容量的图中

python - 更正 pb 文件以将 Tensorflow 模型移动到 ML.NET

delphi - 在 Treeview 中禁用和更改节点的颜色