使用POS标签计算每个词性的数量

时间:2012-11-26 16:24:37

标签: java string text-parsing part-of-speech

我想要计算例如副词,但不同类型的标签有不同的“_RB”,“_ RBR”和“_RBS”。我尝试使用序列为3的子串,但这消除了找到更长标签的可能性 - “_RB”与“_RBS”。我在Java中使用Stanford POS标记器,我不知道如何计算每种类型的标记。这是我到目前为止所做的:

  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是标记的字符串。

以下是标记字符串示例:

This_DT is_VBZ a_DT good_JJ sample_NN sentence_NN ._. Here_RB is_VBZ another_DT good_JJ sample_NN sentence_NN ._.

1 个答案:

答案 0 :(得分:1)

在您的情况下,以下(尽管不是最佳)代码可以作为指导

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

并为它fiddle