2016-10-26 35 views
0

我使用斯坦福分析器來實現我的實現。 我想用句子的樹來提取各種信息。在斯坦福分析器中的樹中提取的引理

我使用的代碼: Get certain nodes out of a Parse Tree

我有我的CoreMap句子和對應的樹:

Tree sentenceTree= sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
for (Tree sentenceTree: t) { 
String pos = sentenceTree.label().value(); 
String wd = sentenceTree.firstChild().label().value(); 
Integer wdIndex = ?? 
CoreLabel token = sentence.get(CoreAnnotations.TokensAnnotation.class).get(wdIndex); 

}

我無法提取引理,沒有任何人有一個想法如何做到這一點?

我嘗試下面的代碼和它的作品,但它會產生一些警告,是不是很乾淨,也不:

Annotation a = new Annotation("geese"); 
ss.pipeline.annotate(a); 
CoreMap se = a.get(CoreAnnotations.SentencesAnnotation.class).get(0); 
CoreLabel token = se.get(CoreAnnotations.TokensAnnotation.class).get(0); 
String lemma = token.get(CoreAnnotations.LemmaAnnotation.class); 
System.out.println(lemma); // goose 

有沒有人有什麼建議?

謝謝!

+0

在句子樹中是否有單詞索引,與CoreMap(句子)中的單詞索引具有相同的值? –

回答

1

我有同樣的問題,但我用雙葉的HashMap和葉索引解決了它。此代碼打印每個匹配葉的名詞化版本。

 List<CoreLabel> tokens = sentence.get(TokensAnnotation.class); 
     Tree tree = sentence.get(TreeAnnotation.class); 
     TregexPattern pattern = TregexPattern.compile("NNP | NNS | NN | NNPS"); 
     TregexMatcher matcher = pattern.matcher(tree); 

     HashMap<Tree, Integer> leafDict = new HashMap<>(); 
     int i = 0; 
     for(Tree leaf : tree.getLeaves()) { 
      leafDict.put(leaf, i); 
      i++; 
     } 

     while (matcher.find()) { 
      int index = leafDict.get(matcher.getMatch().firstChild()); 
      String result = tokens.get(index).get(LemmaAnnotation.class); 
      System.out.println(result); 
     } 

該解決方案僅在搜索節點在葉子之前的一級時起作用。