如何从 CoreNLPParser 中提取短语?
How can I extract phrases from CoreNLPParser?
See the screenshot
正如你从图像解析器中看到的那样returns NP, VP, PP, NP。我希望能够访问不同深度的所有短语。例如,in depth=1 有两个短语NP 和VP,in depth=2 还有一些其他的短语,in depth=3 还有一些其他的。如何访问属于 depth = n with python?
的短语
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));
}
}
}
}
See the screenshot
正如你从图像解析器中看到的那样returns NP, VP, PP, NP。我希望能够访问不同深度的所有短语。例如,in depth=1 有两个短语NP 和VP,in depth=2 还有一些其他的短语,in depth=3 还有一些其他的。如何访问属于 depth = n with python?
的短语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));
}
}
}
}