我正在使用stanford-corenlp-3.5.2和德语解析模型germanFactored.ser.gz。代码工作正常,直到我尝试进行依赖解析。在初始化SemanticGraph
时,我会检索NullPointerException
。
加载:CollapsedCCProcessedDependenciesAnnotation.class(第一行抛出异常)
SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
System.out.println(dependencies);
dependencies.prettyPrint();
整个代码:
public static void main(String[] args) {
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
props.put("parse.model", "edu/stanford/nlp/models/lexparser/germanFactored.ser.gz");
props.put("ner.model", "edu/stanford/nlp/models/ner/german.dewac_175m_600.crf.ser.gz");
props.put("pos.model", "edu/stanford/nlp/models/pos-tagger/german/german-hgc.tagger");
props.put("parse.flags", "");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "Sehr gute Kenntnisse in Englisch und Deutsch sowie Programmierkenntnisse (C#, Java, C++)."; // text
Annotation document = new Annotation(text);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
String ne = token.get(NamedEntityTagAnnotation.class);
System.out.println(String.format("%s - %s - %s", word, pos, ne));
}
Tree tree = sentence.get(TreeAnnotation.class);
System.out.println(tree.toString());
try {
SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
System.out.println(dependencies);
dependencies.prettyPrint();
} catch (Exception e) {
e.printStackTrace();
}
}
}