有没有办法返回所有匹配的表达式?
考虑下面这句话
John Snow killed Ramsay Bolton其中John-NNP、Snow-NNP、killed- 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));
});
}
}en.rules
{
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"
}发布于 2018-09-22 05:06:55
我认为你需要为你想要的不同东西创建不同的提取器。
这里的问题是,当您有两个像这样重叠的词性标记规则序列时,第一个匹配的规则序列会吸收阻止第二个模式匹配的标记。
因此,如果(NNP,NNP)是第一个规则,则匹配"John Snow“。但是“雪”不能与“雪杀”相提并论。
如果你有一组像这样重叠的模式,你应该把它们分开,放在单独的提取器中。
例如,你可以有一个(名词,动词)提取器和一个单独的(名词,名词)提取器。
https://stackoverflow.com/questions/52449624
复制相似问题