我正在尝试使用斯坦福 TokensRegex 。但是,我在匹配器的行中遇到错误(请参阅评论),它说 ()。请尽力帮助我。下面是我的代码:

 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> 作为其输入参数。我在下面做了一些事情:

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

上面的代码没有优化。请根据您的需要调整它们。

10-08 01:44