1   package eu.fbk.dh.tint.digimorph.annotator;
2   
3   import edu.stanford.nlp.ling.CoreAnnotation;
4   import edu.stanford.nlp.ling.CoreAnnotations;
5   import edu.stanford.nlp.ling.CoreLabel;
6   import edu.stanford.nlp.pipeline.Annotation;
7   import edu.stanford.nlp.pipeline.Annotator;
8   import edu.stanford.nlp.util.ArraySet;
9   import edu.stanford.nlp.util.CoreMap;
10  import eu.fbk.dh.tint.digimorph.DigiMorph;
11  
12  import java.util.*;
13  
14  /**
15   * Created by giovannimoretti on 18/05/16.
16   */
17  public class DigiMorphAnnotator implements Annotator {
18  
19      DigiMorph dm;
20  
21      public DigiMorphAnnotator(String annotatorName, Properties prop) {
22          String model_path = prop.getProperty(annotatorName + ".model");
23          this.dm = DigiMorphModel.getInstance(model_path);
24      }
25  
26      public void annotate(Annotation annotation) {
27  
28          List<String> token_word = new LinkedList<String>();
29  
30          if (annotation.containsKey(CoreAnnotations.SentencesAnnotation.class)) {
31              for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
32                  List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
33                  for (CoreLabel c : tokens) {
34                      token_word.add(c.word());
35                  }
36              }
37  
38              token_word = dm.getMorphology(token_word);
39  
40              try {
41                  if (token_word.size() > 0) {
42                      for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
43                          List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
44                          for (CoreLabel c : tokens) {
45                              c.set(DigiMorphAnnotations.MorphoAnnotation.class, token_word.get(0));
46                              token_word.remove(0);
47                          }
48                      }
49                  }
50              } catch (Exception e) {
51                  System.out.println(token_word);
52                  e.printStackTrace();
53              }
54  
55          }
56      }
57  
58      /**
59       * Returns a set of requirements for which tasks this annotator can
60       * provide.  For example, the POS annotator will return "pos".
61       */
62      @Override public Set<Class<? extends CoreAnnotation>> requirementsSatisfied() {
63          return Collections.singleton(DigiMorphAnnotations.MorphoAnnotation.class);
64      }
65  
66      /**
67       * Returns the set of tasks which this annotator requires in order
68       * to perform.  For example, the POS annotator will return
69       * "tokenize", "ssplit".
70       */
71      @Override public Set<Class<? extends CoreAnnotation>> requires() {
72          return Collections.unmodifiableSet(new ArraySet<>(Arrays.asList(
73                  CoreAnnotations.TokensAnnotation.class,
74                  CoreAnnotations.SentencesAnnotation.class
75          )));
76      }
77  
78  }
79  
80