Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用postagger后从单词中删除标记_Java_String_Nlp - Fatal编程技术网

在java中使用postagger后从单词中删除标记

在java中使用postagger后从单词中删除标记,java,string,nlp,Java,String,Nlp,我使用斯坦福大学的NLP postagger标记程序中的名词和形容词 interest_NN bui_NNS ground_VBP avail_NN respond_NN detail_NN like_IN quickli_NNS current_JJ 现在我必须只选择那些有标记_NN,_NNS,_JJ的单词,并从单词中删除这些标记 quickli current avail 我试

我使用斯坦福大学的NLP postagger标记程序中的名词和形容词

    interest_NN 
    bui_NNS 
    ground_VBP
     avail_NN 
    respond_NN
     detail_NN 
    like_IN 
    quickli_NNS
    current_JJ 
现在我必须只选择那些有标记_NN,_NNS,_JJ的单词,并从单词中删除这些标记

    quickli
    current
    avail
我试图这样从单词中删除-NN标记。但它删除了前两个单词的标签,并从中得到了例外

           while(tagread.hasNext())
           {
        String s=tagread.next();

        int flag=1;
        jTextArea2.append("\n" +s.toLowerCase());


        String ofInterest2 = s.substring(0, s.indexOf("_NN"));


         for(int i=0;i<s.length();i++){
             if(s.equals(ofInterest2))
                 {
                 flag=0;
                 }
         }
         if(flag!=0)
         {
             System.out.println(ofInterest2);

         }
    }

那么我的方法有什么问题?或者如何继续?

不要使用字符串方法删除标记器文本;使用NLP的API提取词性以进行比较

生成
标记DWORD
对象的
列表
,然后使用直接提取词性:

// Call the API to parse your sentence.
List<TaggedWord> words = tagger.tagSentence( ... );

// For each word tagged in the sentence...
for( TaggedWord word : words ) {
  String tag = word.tag();

  // Check the part-of-speech directly, without having to parse the string.
  if( "NN".equalsIgnoreCase( tag ) ) {
    System.out.printf( "%s is a noun\n", word.word() );
  }
}
这是因为词类可以通过多种方式进行标记(例如NN、NNS)。您可以使用正则表达式或

你应该要求
TaggedWord
的作者提供一个
isNoun
isVerb
isnounumple
和其他类似方法。也就是说,可以使用正则表达式来匹配字符串。我还在代码中使用
startsWith
检查名词,因为它比正则表达式快。例如:

if( tag != null && tag.toUpperCase().startsWith( "NN" ) ) {
  System.out.printf( "%s is a noun\n", word.word() );
}

要成为真正的OO,请为tagger注入一个子类TaggedWord。然后子类将公开
isNoun
方法。

indexOf
在字符串中找不到您提供的参数时返回-1。在这一行:

String ofInterest2 = s.substring(0, s.indexOf("_NN"));

s.indexOf
可能在字符串
s
中未找到“\NN”。然后,当您请求从s的
0
-1
的子字符串时,这没有意义,因此您会得到一个异常。

您试图获取整个文本“ground\u VBP”的子字符串,但您传入了
s.indexOf(“\u NN”)
的结果。未找到子字符串,因此它返回
-1
。但是
-1
不是
子字符串
函数的有效索引,因此
子字符串
抛出了您报告的
StringIndexOutOfBoundsException


只有当
indexOf
方法返回0或更大的值(即找到了它)时,才应该使用子字符串。

您能提供简单的示例吗?我用斯坦福德邮戳。给我一些NLAPI。谢谢你的建议,我试着用另一种方法。比如使用regex检查字符串的结尾,比如像_NNP、_VRB等结尾,然后使用replace all删除整个字符串。这个方法正确吗?除了我的方法之外,还有更好的方法吗?可能有一个“postagger”方法来做到这一点;我不知道。我对它不熟悉。我刚刚看到了您的方法导致
StringIndexOutOfBoundsException
的原因。
if( tag != null && tag.toUpperCase().startsWith( "NN" ) ) {
  System.out.printf( "%s is a noun\n", word.word() );
}
String ofInterest2 = s.substring(0, s.indexOf("_NN"));