私はStanford CoreNLP Java APIを学び始めており、文の構文木を印刷しようとしています。構文木は、ParserAnnotatorによって生成されるものとします。私のコード(以下に掲載)では、ParserAnnotatorはエラーなく実行されますが、何も生成しません。エラーは、コードがツリーのルートノードのラベルを取得しようとしたときにのみ表示され、ツリーはnullになることがわかります。アノテーションを問題なく生成する前に実行されるコンポーネント。Stanford ParserAnnotatorは注釈を生成しません
ParserAnnotatorに問題があった人がSOに1人いましたが、メモリに問題がありました。私はEclipseの使用を可能にするメモリを増やしましたが、動作は同じです。デバッガでコードを実行してもエラーは発生しませんでした。
いくつかの背景情報:私が使用した文章は「これはランダムな文章です」私は最近、あなたはかなりの文章(CoreMap
)よりも、文書(Annotation
)のオフツリーを取得しようとしている
public static void main(String[] args){
String sentence = "This is a random sentence.";
Annotation doc = initStanford(sentence);
Tree syntaxTree = doc.get(TreeAnnotation.class);
printTreePreorder(syntaxTree);
}
private static Annotation initStanford(String sentence){
StanfordCoreNLP pipeline = pipeline("tokenize, ssplit, parse");
Annotation document = new Annotation(sentence);
pipeline.annotate(document);
return document;
}
private static StanfordCoreNLP pipeline(String components){
Properties props = new Properties();
props.put("annotators", components);
return new StanfordCoreNLP(props);
}
public static void printTreePreorder(Tree tree){
System.out.println(tree.label());
for(int i = 0;i < tree.numChildren();i++){
printTreePreorder(tree.getChild(i));
}
}
ガボールのコードだけで、文書内の最初の文を処理します。より一般的なパターンは次のとおりです。 'for(CoreMap s:doc.get(CoreAnnotations.SentencesAnnotation.class)){ s.get(TreeAnnotation.class).pennPrint(); } ' –