Stanford nlp 返回所有匹配的表达式
是否有方法返回所有匹配的表达式 考虑下面的句子Stanford nlp 返回所有匹配的表达式,stanford-nlp,Stanford Nlp,是否有方法返回所有匹配的表达式 考虑下面的句子 John Snow killed Ramsay Bolton 在哪里 John NNP,Snow NNP,Villed-VBD,Ramsay NNP,Bolton NNP 我使用下面的标记组合作为规则 NNP-NNP NNP-VBD VBD-NNP 上述规则中的预期匹配词为: John Snow, Snow killed, killed Ramsay, Ramsay Bolton 但使用下面的代码,我只得到匹配表达式: [John Snow,
John Snow killed Ramsay Bolton
在哪里
John NNP
,Snow NNP
,Villed-VBD
,Ramsay NNP
,Bolton NNP
我使用下面的标记组合作为规则
NNP-NNP
NNP-VBD
VBD-NNP
上述规则中的预期匹配词为:
John Snow, Snow killed, killed Ramsay, Ramsay Bolton
但使用下面的代码,我只得到匹配表达式:
[John Snow, killed Ramsay]
在斯坦福大学的stanford
中有没有一种方法可以从句子中获得所有预期的匹配词?这是我现在使用的代码和规则文件:
import com.factweavers.multiterm.SetNLPAnnotators;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor;
import edu.stanford.nlp.ling.tokensregex.Env;
import edu.stanford.nlp.ling.tokensregex.NodePattern;
import edu.stanford.nlp.ling.tokensregex.TokenSequencePattern;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import java.util.List;
import java.util.regex.Pattern;
public class StanfordTest {
public static void main(String[] args) {
String rulesFile="en.rules";
Env env = TokenSequencePattern.getNewEnv();
env.setDefaultStringMatchFlags(NodePattern.NORMALIZE);
env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE);
env.bind("collapseExtractionRules", false);
CoreMapExpressionExtractor extractor= CoreMapExpressionExtractor.createExtractorFromFiles(env, rulesFile);
String content="John Snow killed Ramsay Bolton";
Annotation document = new Annotation(content);
SetNLPAnnotators snlpa = new SetNLPAnnotators();
StanfordCoreNLP pipeline = snlpa.setAnnotators("tokenize, ssplit, pos, lemma, ner");
pipeline.annotate(document);
List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
sentences.parallelStream().forEach(sentence -> {
System.out.println(extractor.extractExpressions(sentence));
});
}
}
我认为你需要为不同的东西创建不同的提取器 这里的问题是,当您有两个像这样重叠的词性标记规则序列时,第一个匹配的标记会吸收阻止第二个模式匹配的标记 因此,如果(NNP,NNP)是第一条规则,“约翰·斯诺”将被匹配。但那时的雪是无法与“雪死”相匹配的 如果您有一组像这样重叠的模式,您应该将它们分开,并将它们放在单独的提取器中
所以你可以有一个(名词,动词)提取器,还有一个单独的(名词,名词)提取器。那么如果有10条规则,我需要创建10个提取器对象吗?Env类中是否有类似于collapseeextractionrules的简单设置?
{
ruleType:"tokens",
pattern:([{tag:/VBD/}][ {tag:/NNP/}]),
result:"result1"
}
{
ruleType:"tokens",
pattern:([{tag:/NNP/}][ {tag:/VBD/}]),
result:"result2"
}
{
ruleType:"tokens",
pattern:([{tag:/NNP/}][ {tag:/NNP/}]),
result:"result3"
}