How to use Stanford TokensRegex?

I am trying to use Stanford TokensRegex. However, I get an error in the string for matches (see Comment), it says (). Please do your 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"); 
    }
 } 
+4
source share
1 answer

The error here is from pattern.getMatcher(sentence), since getMatcher(*)this method accepts only List<CoreLabel>as an 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. Apply them the way you want.

+3
source

Source: https://habr.com/ru/post/1546695/


All Articles