我试图使用NSLinguisticTagger隔离句子中的动词,但是遇到了一个问题,根据代码是否在iOS和MacOS程序中运行,输出是不同的。

我的代码如下:

NSString* text = @"The person is a 50 year old gentleman with a book who presents us with a conundrum.";
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
NSLinguisticTagger* tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[NSLinguisticTagSchemeNameTypeOrLexicalClass]
                                                                    options:options];
tagger.string = text;
[tagger enumerateTagsInRange:NSMakeRange(0, [tagger.string length])
                      scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass
                     options:options
                  usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                      NSString *token = [text substringWithRange:tokenRange];
                      NSLog(@"%@: %@", token, tag);
                  }];

OSX 程序中运行此代码段,我正确地获得以下输出(“presents” 被正确地标识为动词):
[                    AppDelegate (0x101b0bcb0)]: The: Determiner
[                    AppDelegate (0x101b0bcb0)]: person: Noun
[                    AppDelegate (0x101b0bcb0)]: is: Verb
[                    AppDelegate (0x101b0bcb0)]: a: Determiner
[                    AppDelegate (0x101b0bcb0)]: 50: Number
[                    AppDelegate (0x101b0bcb0)]: year: Noun
[                    AppDelegate (0x101b0bcb0)]: old: Adjective
[                    AppDelegate (0x101b0bcb0)]: gentleman: Noun
[                    AppDelegate (0x101b0bcb0)]: with: Preposition
[                    AppDelegate (0x101b0bcb0)]: a: Determiner
[                    AppDelegate (0x101b0bcb0)]: book: Noun
[                    AppDelegate (0x101b0bcb0)]: who: Pronoun
[                    AppDelegate (0x101b0bcb0)]: presents: Verb
[                    AppDelegate (0x101b0bcb0)]: us: Pronoun
[                    AppDelegate (0x101b0bcb0)]: with: Preposition
[                    AppDelegate (0x101b0bcb0)]: a: Determiner
[                    AppDelegate (0x101b0bcb0)]: conundrum: Noun

但是, iOS 程序中相同的代码块会导致以下输出(“presents” 被错误地标识为名词):
[                      AppDelegate (0x8d2f000)]: The: Determiner
[                      AppDelegate (0x8d2f000)]: person: Noun
[                      AppDelegate (0x8d2f000)]: is: Verb
[                      AppDelegate (0x8d2f000)]: a: Determiner
[                      AppDelegate (0x8d2f000)]: 50: Number
[                      AppDelegate (0x8d2f000)]: year: Noun
[                      AppDelegate (0x8d2f000)]: old: Adjective
[                      AppDelegate (0x8d2f000)]: gentleman: Noun
[                      AppDelegate (0x8d2f000)]: with: Preposition
[                      AppDelegate (0x8d2f000)]: a: Determiner
[                      AppDelegate (0x8d2f000)]: book: Noun
[                      AppDelegate (0x8d2f000)]: who: Pronoun
[                      AppDelegate (0x8d2f000)]: presents: Noun
[                      AppDelegate (0x8d2f000)]: us: Pronoun
[                      AppDelegate (0x8d2f000)]: with: Preposition
[                      AppDelegate (0x8d2f000)]: a: Determiner
[                      AppDelegate (0x8d2f000)]: conundrum: Noun

有谁知道为什么我得到不同的输出,以及我如何正确获取iOS程序以将表示为动词?

最佳答案

标记器是随机的,即,它总是会出错。由于iOS设备的资源有限,与OS X版本相比,底层语言模型的精确度较低。如果要提高准确性,请使用基于规则的解析器。但是,您必须处理歧义。

10-05 21:00
查看更多