2
public class Sample {
   public static void main(String[] args) throws IOException,
        ClassNotFoundException {

    // Initialize the tagger
    MaxentTagger tagger = new MaxentTagger("taggers/wsj-0-18-bidirectional-nodistsim.tagger");

    // The sample string
    String sample = "I am a good boy";
    String[] tokens = sample.split(" ");

    for(int i=0;i<tokens.length;i++){
        String tagged = tagger.tagString(tokens[i]);
         System.out.println(tagged);
    }

    // The tagged string
    //String tagged = tagger.tagTokenizedString(sample);

    // Output the result
    //System.out.println(tagged.startsWith("N"));

}
}

OUTPUT:

I_PRP 
am_VB 
a_DT 
good_JJ 
boy_NN

Q: I need to print boy as output in the above program as it is tagged as singular nouns(NN)

4

2 回答 2

1

得到溶胶:

MaxentTagger tagger = new MaxentTagger("taggers/wsj-0-18-bidirectional-nodistim.tagger");

    // The sample string
    String s = "It implements all optional list operations and it also permits all elements, includes null.";
    String sample = s.replaceAll("\\W", " ");

    // The tagged string
    String tagged = tagger.tagTokenizedString(sample);

    // Output the result
    // System.out.println(tagged);
    String[] x = tagged.split(" ");
    ArrayList<String> list = new ArrayList<String>();  

    for(int i=0;i<x.length;i++)
    {
        if (x[i].substring(x[i].lastIndexOf("_")+1).startsWith("N"))
        {
            list.add(x[i].split("_")[0]);
        }
    }
    for(int i=0;i<list.size();i++)
    {
        System.out.println(list.get(i));
    }
}

输出:

列表

操作

元素

无效的

于 2013-09-17T09:10:01.750 回答
0

您可以尝试先将其放入数组中,然后获取其中包含 NN 的数据。

或其他解决方案,使用哈希映射也可以更轻松地获取要打印的密钥。

于 2013-09-17T06:32:23.953 回答