Java SemgrexPattern引理属性不';好像不行
下面是一个使用斯坦福NLP的Java SemgrexPattern引理属性不';好像不行,java,nlp,pattern-matching,stanford-nlp,Java,Nlp,Pattern Matching,Stanford Nlp,下面是一个使用斯坦福NLP的SemgrexPattern的非常简单的示例。 我不明白为什么它没有找到任何与{lemma:/eat/}匹配的内容,而它却找到了与{word:/eats/}匹配的内容。我使用LemmaAnnotation类得到动词“toeat”的引理,它是“eat” 谢谢你的帮助:) 成套工程; 导入java.io.File; 导入java.util.Scanner; 导入edu.stanford.nlp.parser.lexparser.TreebankLangParserPara
SemgrexPattern
的非常简单的示例。
我不明白为什么它没有找到任何与{lemma:/eat/}
匹配的内容,而它却找到了与{word:/eats/}
匹配的内容。我使用LemmaAnnotation
类得到动词“toeat”的引理,它是“eat”
谢谢你的帮助:)
成套工程;
导入java.io.File;
导入java.util.Scanner;
导入edu.stanford.nlp.parser.lexparser.TreebankLangParserParams;
导入edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams;
导入edu.stanford.nlp.semgraph.SemanticGraph;
导入edu.stanford.nlp.semgraph.SemanticGraphFactory;
导入edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
导入edu.stanford.nlp.semgraph.semgrex.SemgrexPattern;
导入edu.stanford.nlp.trees.GrammaticalStructure;
导入edu.stanford.nlp.trees.grammaticStructureFactory;
导入edu.stanford.nlp.trees.Tree;
公共类SemgrexDemo{
公共静态void main(字符串[]args)引发FileNotFoundException{
String treeString=“(ROOT(S)(NP(NNP-John))(VP(VBZ)(NP(NN-pizza)))(…)”;
Tree Tree=Tree.valueOf(treeString);
SemanticGraph graph=SemanticGraphFactory.generateUncollapsedDependencies(树);
TreebankLangParserParams params=新英语TreebankLangParserParams();
语法结构工厂gsf=params.treebankLanguagePack().grammaticStructureFactory(params.treebankLanguagePack().sparationWordRejectFilter(),params.typedPendencyHeadFinder());
语法结构gs=gsf.newgrammaticstructure(tree);
系统错误打印LN(图形);
SemgrexPattern semgrex=SemgrexPattern.compile(“{}=A当您将树字符串解析为树对象时,引理不会自动添加到标记中,因此语义图
中所有节点的引理属性都是null
,因此{lemma:/eat/}
不匹配任何节点
您可以使用词法
类的引理(字符串单词,字符串位置)
方法添加引理:
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"));
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
String treeString=“(ROOT(S)(NP(NNP-John))(VP(VBZ)(NP(NN-pizza)))(…)”;
Tree Tree=Tree.valueOf(treeString);
SemanticGraph graph=SemanticGraphFactory.generateUncollapsedDependencies(树);
//添加引理
形态=新形态();
for(IndexedWord节点:graph.vertexSet()){
字符串引理=词法引理(node.word(),node.tag());
节点集引理(引理);
}
系统错误打印LN(图形);
SemgrexPattern semgrex=SemgrexPattern.compile(“{}=A哦,我明白了!!谢谢你花时间回答我:)
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"));
}
}