[英]How can I extract phrases from CoreNLPParser?
從圖像解析器可以看到,返回NP,VP,PP,NP。 我希望能夠訪問不同深度的所有短語。 例如,在depth = 1中,有兩個短語NP和VP,在depth = 2中,還有一些其他短語,在depth = 3中,還有其他一些短語。 如何使用python訪問屬於depth = n的短語?
package edu.stanford.nlp.examples;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import java.util.*;
import java.util.stream.*;
public class ConstituencyParserExample {
public static void main(String[] args) {
String text = "The little lamb climbed the big mountain.";
// set up pipeline properties
Properties props = new Properties();
// set the list of annotators to run
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,parse");
// build pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// create a document object
CoreDocument document = new CoreDocument(text);
// annnotate the document
pipeline.annotate(document);
int maxDepth = 5;
for (CoreSentence sentence : document.sentences()) {
Set<Constituent> constituents = sentence.constituencyParse().constituents(
new LabeledScoredConstituentFactory(), maxDepth).stream().filter(
x -> x.label().value().equals("NP")).collect(Collectors.toSet());
for (Constituent constituent : constituents) {
System.out.println("---");
System.out.println("label: "+constituent.label().value());
System.out.println(sentence.tokens().subList(constituent.start(), constituent.end()+1));
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.