首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将斯坦福解析器输出作为节点和边的列表?

如何将斯坦福解析器输出作为节点和边的列表?
EN

Stack Overflow用户
提问于 2015-03-04 22:25:44
回答 1查看 752关注 0票数 0

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

代码语言:javascript
复制
Node List:   1  A ,  2  B...
Edge list:  1 2 10, 2 1 10...

根据Stanford javadoc -->Class SemanticGraph的说法:

没有一次返回所有边的机制(例如edgeSet())。这是故意的。如果有必要,可以使用edgeIterable()在边缘上进行迭代。

该怎么做呢?我试过这个代码:

代码语言:javascript
复制
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);
      }  
    }
  }
}

但是它抛出一个错误:Duplicate local variable edge StanfordCoreNlpSemGraph.java /stan-nlp/src line 60

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 04:36:29

下面是形成边缘列表的一个基本示例。(节点列表部分应该很简单--您只需遍历句子中的标记并打印出来。)

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28866301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档