【问题标题】:Stanford POS tagging: get words tagged as singular nouns(NN)斯坦福 POS 标记:获取标记为单数名词的单词(NN)
【发布时间】:2013-09-17 06:05:36
【问题描述】:
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"));

}
}

输出:

I_PRP 
am_VB 
a_DT 
good_JJ 
boy_NN

问:我需要在上面的程序中打印男孩作为输出,因为它被标记为单数名词(NN)

【问题讨论】:

  • 问题中存在少量混淆。问题要求NN,但是问题的代码显示了一种以N开头的任何词性的方法,其中有4个。然后,有以P开头的代词,那些重要的名词吗?我尽我所能编辑。

标签: java tagging stanford-nlp pos-tagger


【解决方案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));
    }
}

输出:

列表

操作

元素

空

【讨论】:

  • 谢谢,这是获取名词的唯一方法吗?奇怪的是,MaxentTagger.tag() 在形成Strign 输出之前无法过滤标记名。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多