如何使用Stanford TokensRegex? [英] How to use Stanford TokensRegex?

查看:121
本文介绍了如何使用Stanford TokensRegex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用斯坦福TokensRegex.但是,我在匹配器的行中出现错误(请参阅注释),它表示().请尽力帮助我.下面是我的代码:

I am trying to use Stanford TokensRegex. However, I am getting error in the line for the matcher (see comment), it says that (). Please do you best to help me. Below is my code:

 String file = "A store has many branches. A  manager may manage at most 2 branches.";
 Properties props = new Properties();
 props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
 StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
 Annotation document = new Annotation(file);
 pipeline.annotate(document);
 List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
 for(CoreMap sentence: sentences) {
    TokenSequencePattern pattern = TokenSequencePattern.compile("[]");
    TokenSequenceMatcher matcher = pattern.getMatcher(sentence); // ERROR HERE!
    while( matcher.find()){
        JOptionPane.showMessageDialog(rootPane, "It has been found"); 
    }
 } 

推荐答案

错误来自pattern.getMatcher(sentence),因为getMatcher(*)该方法仅将List<CoreLabel>作为其输入参数.我在下面做了一些事情:

The error is from pattern.getMatcher(sentence) here, as getMatcher(*) this method only takes List<CoreLabel> as its input argument. I did something below:

List<CoreLabel> tokens = new ArrayList<CoreLabel>();
for(CoreMap sentence: sentences) {
    // **using TokensRegex**
    for (CoreLabel token: sentence.get(TokensAnnotation.class)) 
        tokens.add(token);            
    TokenSequencePattern p1 = TokenSequencePattern.compile("A store has");
    TokenSequenceMatcher matcher = p1.getMatcher(tokens);
    while (matcher.find())              
        System.out.println("found");

    // **looking for the POS**
    for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
        String word = token.get(TextAnnotation.class);
        // this is the POS tag of the token
        String pos = token.get(PartOfSpeechAnnotation.class);
        System.out.println("word is "+ word +", pos is " + pos);
    }
}

以上代码未优化.请根据需要调整它们.

The above codes are not optimized. Please adapt them like what you want.

这篇关于如何使用Stanford TokensRegex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆