实际上我使用Java从西班牙语文本中提取三元组。我需要提取NP-VP-NP
形式的三元组。我也使用Stanford Parser CoreNLP v 3.7.0和西班牙模型v 3.7.0。接下来我的问题是,有没有办法从西班牙语模型中的句子中提取NP子树和VP子树?我意识到西班牙语解析器树形式与英语形式不同。
例如:
(ROOT (sentence (sn (spec (da0000 El)) (grup.nom (nc0s000 reino))) (grup.verb (vmm0000 canta) (sadv (spec (rg muy)) (grup.adv (rg bien))) (fp .)))
答案 0 :(得分:1)
您应该使用主发行版来确保拥有所有内容并下载西班牙语模型
(可在此处获取:http://stanfordnlp.github.io/CoreNLP/download.html)
package edu.stanford.nlp.examples;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.trees.tregex.*;
import edu.stanford.nlp.util.*;
import java.util.*;
public class TregexExample {
public static void main(String[] args) {
// set up pipeline
Properties props = StringUtils.argsToProperties("-props", "StanfordCoreNLP-spanish.properties");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// Spanish example
Annotation spanishDoc = new Annotation("...insert Spanish text...");
pipeline.annotate(spanishDoc);
// get first sentence
CoreMap firstSentence = spanishDoc.get(CoreAnnotations.SentencesAnnotation.class).get(0);
Tree firstSentenceTree = firstSentence.get(TreeCoreAnnotations.TreeAnnotation.class);
// use Tregex to match
String nounPhrasePattern = "/grup\\.nom/";
TregexPattern nounPhraseTregexPattern = TregexPattern.compile(nounPhrasePattern);
TregexMatcher nounPhraseTregexMatcher = nounPhraseTregexPattern.matcher(firstSentenceTree);
while (nounPhraseTregexMatcher.find()) {
nounPhraseTregexMatcher.getMatch().pennPrint();
}
}
}