java - 使用 POS 标注器计算每个词性的数量

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

我想要计数例如副词,但不同类型有不同的标签,“_RB”、“_RBR”和“_RBS”。我尝试在 3 个序列中使用子字符串,但这消除了找到更长标签的可能性 - “_RB”与“_RBS”。我正在 Java 中使用斯坦福 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 ._.

最佳答案

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

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为了它。

关于java - 使用 POS 标注器计算每个词性的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13568895/

相关文章:

android - JSON解析成textview Android

java - 为什么Spring Security需要对权限集合进行排序?

javascript - javascript从字符串中获取特定文本

java - LWJGL 顶点和片段着色器无法编译(错误 CO206)

python - 从 python 中的字符串获取城市、州或 zip

iOS : Is there a way to replace string that moSTLy like same?

php - 使用 PHP 解析 pdftk dump_data_fields?

bash - 解析ip :port from a text file的shell脚本

java - 将字符串格式化为小数点后两位

java - 打印不带分隔符的数组