gpt4 book ai didi

java - SemgrexPattern 引理属性似乎不起作用

转载 作者:行者123 更新时间:2023-11-30 07:13:53 29 4
gpt4 key购买 nike

这是一个使用斯坦福 NLP 的 SemgrexPattern 的非常简单的示例。我不明白为什么它找不到与 {lemma:/eat/} 的任何匹配项,而却找到与 {word:/eats/} 的匹配项。我使用 LemmaAnnotation 类来获取动词“to eat”的引理,它是“eat”。

感谢您的帮助:)

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"));
}
}
}

最佳答案

当您将树字符串解析为 Tree 对象时,引理不会自动添加到标记中,因此 SemanticGraph 中所有节点的引理属性为 null因此 {lemma:/eat/} 不匹配任何节点。

您可以使用 Morphology 类的 lemma(String word, String pos) 方法添加引理:

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"));
}
}

关于java - SemgrexPattern 引理属性似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746639/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com