【发布时间】:2019-02-18 01:41:58
【问题描述】:
我正在尝试使用斯坦福 NLP 解析医学研究报告。我可以获得除第一个或根节点之外的所有节点的 GrammaticalRelation。我如何获得这个值。
我写了一个java程序,它通过获取依赖图来解析报告,并且可以获取除根节点之外的所有节点的子对。
public void DocAnnotationParse(String Input_text) {
Annotation document = new Annotation(Input_text);
Properties props = new Properties();
//props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);
int sentNum = 0;
Map<String, Map<String, Map<String,IndexedWord>>> sentMap = new LinkedHashMap<>(); // A map contains maps of each sentence
for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
SemanticGraph dependencyParse = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
IndexedWord firstVertex = dependencyParse.getFirstRoot();
Map<String, Map<String,IndexedWord>> outterMap = new LinkedHashMap<>();
RecursiveChild(outterMap, dependencyParse, firstVertex, 0);
sentMap.put(Integer.toString(++sentNum), outterMap);
logger.debug("outtermap: "+outterMap);
}
logger.debug("all sentMaps: "+sentMap);
PrettyPrintBySentence(sentMap);
}
public void RecursiveChild(Map<String, Map<String, IndexedWord>> outterMap,
SemanticGraph dependencyParse,
IndexedWord vertex, int hierLevel) {
Map<String, IndexedWord> pairMap = new LinkedHashMap<>();
pairMap.put("Root", vertex);
List<IndexedWord>indxwdsL = dependencyParse.getChildList(vertex);
List<Pair<GrammaticalRelation,IndexedWord>>childPairs = dependencyParse.childPairs(vertex);
List<IndexedWord> nxtLevalAL = new ArrayList<>();
if(!indxwdsL.isEmpty()) {
++hierLevel;
for(Pair<GrammaticalRelation, IndexedWord> aPair : childPairs) { //at level hierLevel x
logger.debug(aPair);
String grammRel = aPair.first.toString(); //Gramatic Relation
IndexedWord indxwd = aPair.second;
pairMap.put(grammRel, indxwd);
List<Pair<GrammaticalRelation,IndexedWord>>childPairs2 = dependencyParse.childPairs(indxwd);
if(!childPairs2.isEmpty()) {
nxtLevalAL.add(indxwd);
}
}
}
String level = Integer.toString(hierLevel);
outterMap.put(level, pairMap);
//Go to each lower level
for(IndexedWord nxtIwd : nxtLevalAL) {
RecursiveChild(outterMap, dependencyParse, nxtIwd, hierLevel);
}
}
根顶点的 childPair 不包含我想要的语法关系。查看依赖关系图没有任何价值,只有字符串根。如何获得该节点的语法关系。例如简单的句子“我喜欢炸薯条”。给出图表:
-> love/VBP (root)
-> I/PRP (nsubj)
-> fries/NNS (dobj)
-> French/JJ (amod)
-> ./. (punct)
【问题讨论】:
标签: parsing stanford-nlp