2016-08-03 75 views
1

以下是使用來自Stanford NLP的SemgrexPattern的一個非常簡單的示例。 我不明白爲什麼它找不到與{lemma:/eat/}任何匹配,而它找到與{word:/eats/}匹配。我使用LemmaAnnotation類來得到動詞「吃」的引理,它是「吃」。SemgrexPattern引理屬性似乎不起作用

謝謝您的幫助:)

package Project; 
import java.io.File; 
import java.util.Scanner; 

import edu.stanford.nlp.parser.lexparser.TreebankLangParserParams; 
import edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams; 
import edu.stanford.nlp.semgraph.SemanticGraph; 
import edu.stanford.nlp.semgraph.SemanticGraphFactory; 
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher; 
import edu.stanford.nlp.semgraph.semgrex.SemgrexPattern; 
import edu.stanford.nlp.trees.GrammaticalStructure; 
import edu.stanford.nlp.trees.GrammaticalStructureFactory; 
import edu.stanford.nlp.trees.Tree; 

public class SemgrexDemo { 
    public static void main(String[] args) throws FileNotFoundException { 
    String treeString = "(ROOT (S (NP (NNP John)) (VP (VBZ eats) (NP (NN pizza))) (. .)))"; 
    Tree tree = Tree.valueOf(treeString); 
    SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree); 
    TreebankLangParserParams params = new EnglishTreebankParserParams(); 
    GrammaticalStructureFactory gsf = params.treebankLanguagePack().grammaticalStructureFactory(params.treebankLanguagePack().punctuationWordRejectFilter(), params.typedDependencyHeadFinder()); 
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); 
    System.err.println(graph); 
    SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<dobj=reln {lemma:/eat/}=B"); 
    SemgrexMatcher matcher = semgrex.matcher(graph); 
    while (matcher.find()) { 
     System.err.println(matcher.getNode("A") + " <<dobj " + matcher.getNode("B")); 
    } 
    } 
} 

回答

1

的lemmata不是當你分析一個樹串到樹對象的標記自動添加的,所以所有的節點在SemanticGraph引理屬性null因此{lemma:/eat/}不匹配任何節點。

可以使用Morphology類的lemma(String word, String pos)方法中添加lemmata:

public static void main(String[] args) throws FileNotFoundException { 
    String treeString = "(ROOT (S (NP (NNP John)) (VP (VBZ eats) (NP (NN pizza))) (. .)))"; 
    Tree tree = Tree.valueOf(treeString); 
    SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree); 

    //add lemmata 
    Morphology morphology = new Morphology(); 
    for (IndexedWord node : graph.vertexSet()) { 
    String lemma = morphology.lemma(node.word(), node.tag()); 
    node.setLemma(lemma); 
    } 

    System.err.println(graph); 
    SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<dobj=reln {lemma:/eat/}=B"); 
    SemgrexMatcher matcher = semgrex.matcher(graph); 
    while (matcher.find()) { 
    System.err.println(matcher.getNode("A") + " <<dobj " + matcher.getNode("B")); 
    } 
} 
+0

哦,我見!感謝您花時間回答我:) –