Log in Help
Print
Homewikijape-repository 〉 features.html
 

JAPE Features

Contents

1. To check on the RHS that an annotation has a certain feature value (in this case, a Token that's an adjective)

if (anAnn.getType().equals("Token") &&
   anAnn.getFeatures().containsKey("category") &&
   anAnn.getFeatures().get("category").equals("JJ")  )

2. Clone a feature map

FeatureMap fm = (FeatureMap)((SimpleFeatureMapImpl)ann.getFeatures()).clone();

3. Copy all the features from one annotation to another

FeatureMap newAnnFeatures = Factory.newFeatureMap();
newAnnFeatures.putAll(oldAnn.getFeatures()); 

4. Print the count for each word of an annotation type with a certain feature

Here the annotation type is "Token" and the feature is "category=JJ" for adjective.

Phase: Count
Input: Token
Options: control = once

Rule: Count
{Token}
-->
{
  Map<String, Integer> countByStringMap = new TreeMap<String, Integer>();
  FeatureMap featureMap = Factory.newFeatureMap();
  featureMap.put("category", "JJ");
  for (Annotation annotation : inputAS.get("Token", featureMap)) {
    String string = (String) annotation.getFeatures().get("string");
    Integer count = (Integer) countByStringMap.get(string);
    if (count == null) {
      countByStringMap.put(string, 1);
    } else {
      countByStringMap.put(string, count + 1);
    }
  }

  for (Map.Entry entry : countByStringMap.entrySet()) {
     System.out.println( entry.getKey() + "," + entry.getValue());
  }
}

5. To do mathematical operations on the value of a feature that's a string (e.g. value of a Lookup annotation from the gazetteer)

({Lookup.score >=0.5}):tag
-->
...
float score = Float.parseFloat(sentiAnn.getFeatures().get("score").toString());
float newScore = score * 2;

features.put("score", newScore);
...

6. Combine the root features of the Tokens under the annotation into a single root feature on the annotation

Imports: {
  import gate.Utils;
  import org.apache.commons.lang.StringUtils;
}


Rule: GetTopicRoot
({Topic}):tag
-->

{
  Annotation topicAnn = Utils.getOnlyAnn(bindings.get("tag"));

  List<Annotation> topicTokens = Utils.inDocumentOrder(
    Utils.getContainedAnnotations(inputAS, topicAnn, "Token"));
  List<String> roots = new ArrayList<String>(topicTokens.size());
  for(Annotation a : topicTokens) {
    roots.add((String)a.getFeatures().get("root"));
  }
  topicAnn.getFeatures().put("root", StringUtils.join(roots, " "));
}