请帮助我将Stanford Parser SemanticGraph输出编码为节点和边缘的数字列表 其中节点具有ID和标签,边缘由两个节点ID和边缘权重组成,如: 节点列表:1 A,2 B ...边缘列表:1 2 10,2 1 10。 虽然,根据stanford nlp javadoc - > Class SemanticGraph:
没有一次返回所有边缘的机制(例如
edgeSet()
)。这是故意的。使用edgeIterable()
进行迭代 必要时边缘。
因此,我尝试使用一个类似的例子,其中一个论坛成员提出了建议,但没有让它发挥作用。 我正在使用Eclipse Luna。 有我的尝试:
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.trees.*;
import edu.stanford.nlp.util.*;
public class StanfordCoreNlpDemo {
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("Out of the unconstructed nature ...");
}
pipeline.annotate(annotation);
pipeline.prettyPrint(annotation, out);
if (xmlOut != null) {
pipeline.xmlPrint(annotation, xmlOut);
}
// An Annotation is a Map and you can get and use the various analyses individually.
// For instance, this gets the parse tree of the first sentence in the text.
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);
SemanticGraph sg = sg.get(StanfordCoreNlpSemGraph.class);
for (SemanticGraphEdge edge : sg.getEdgesIterable()) {
int headIndex = edge.getGovernor().index();
int depIndex = edge.getDependent().index();
int weight = 1 // Not sure what "edge weight" if at all put here?
System.out.printf("%d %d %d%n", headIndex, depIndex, weight);
}
}
}
}
我遇到了两个错误:
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())
此外,我需要通过迭代句子中的标记并将其打印出来获得的节点列表。 但同样,我不确定如何实现它。 非常感谢提前 你的帮助。
答案 0 :(得分:0)
为什么要尝试从自身获取语义图实例?
在我看来,你应该从句子中获取SemanticGraph,使用:
SemanticGraph sg = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);