Question

I want count for example adverbs, but there are different tags for different types, "_RB", "_RBR", and "_RBS". I tried going through using substrings in sequences of 3 but that eliminates the possiblity of finding the longer tags - "_RB" vs "_RBS". I am using the Stanford POS tagger in Java and I don't know how to count each type of tag. Here's what I have so far:

  int pos = 0;
  int end = tagged.length() - 1;

  int nouns = 0;
  int adjectives = 0;
  int adverbs = 0;

  while (pos < (end - 1)){
      pos++;
      String sequence = tagged.substring(pos - 1, pos + 2);
      //System.out.println(sequence);
      if (sequence.equals("_NN")){
          nouns++;
      }
      if (sequence.equals("_JJ")){
          adjectives++;
      }
      if (sequence.equals("_RB")){
          adverbs++;
      }
  }

tagged is the tagged string.

Here is an example tagged string:

This_DT is_VBZ a_DT good_JJ sample_NN sentence_NN ._. Here_RB is_VBZ another_DT good_JJ sample_NN sentence_NN ._.
Was it helpful?

Solution

In your case the following (although not optimal) code can serve as a guidance

public class Main {
    public static void main(final String[] args) throws Exception {
        final String tagged = "World_NN Big_RBS old_RB stupid_JJ";

        int nouns = 0;
        int adjectives = 0;
        int adverbs = 0;

        final String[] tokens = tagged.split(" ");

        for (final String token : tokens) {
            final int lastUnderscoreIndex = token.lastIndexOf("_");
            final String realToken = token.substring(lastUnderscoreIndex + 1);
            if ("NN".equals(realToken)) {
                nouns++;
            }
            if ("JJ".equals(realToken)) {
                adjectives++;
            }
            if ("RB".equals(realToken) || "RBS".equals(realToken)) {
                adverbs++;
            }
        }

        System.out.println(String.format("Nouns: %d Adjectives: %d, Adverbs: %d", nouns, adjectives, adverbs));
    }
}

And a fiddle for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top