gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:05:27 25 4
gpt4 key购买 nike

我使用斯坦福的 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 标签。但是它删除了前 2words 标签并从中得到了异常

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

}
}

异常(exception):

 java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)

那么我的方法有什么问题呢?或者如何进一步进行?

最佳答案

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

生成 TaggedWord 对象的 List 然后使用 TaggedWord API直接提取词性:

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

另请参阅斯坦福的 NLP API:

要检查名词,您应该避免以下情况:

if( "NN".equalsIgnoreCase( tag ) ) {
System.out.printf( "%s is a noun\n", word.word() );
}

这是因为可以用多种方式标记词性(例如,NN、NNS)。您可以使用正则表达式或 startsWith .

您应该要求TaggedWord 的作者提供一个isNounisVerb, isNounPlural 等方法。也就是说,是的,您可以使用正则表达式来匹配字符串。我还在我的代码中使用 startsWith 来检查名词,因为它比正则表达式更快。例如:

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

要成为真正的 OO,请注入(inject) TaggedWord 的子类供标注器使用。然后子类将公开 isNoun 方法。

关于java - 在 java 中使用 postagger 后从单词中删除标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com