这是我正在使用的代码,无论我在设备上使用什么句子,它都不会打印任何内容.在模拟器上它工作正常!
- (NSMutableArray *)getTagEntries:(NSString *)sentence {
NSArray<NSLinguisticTagScheme> *tagSchemes = [NSLinguisticTagger availableTagSchemesForLanguage:@"en"];
NSLinguisticTaggerOptions options = NSLinguisticTaggerJoinNames | NSLinguisticTaggerOmitWhitespace;
NSLinguisticTagger *linguisticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagSchemes options:options];
linguisticTagger.string = sentence;
__block NSMutableArray *tagEntries = [@[] mutableCopy];
[linguisticTagger enumerateTagsInRange:NSMakeRange(0, sentence.length) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock:^(NSLinguisticTag tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
NSString *token = [sentence substringWithRange:tokenRange];
NSLog(@"%@ -> %@", token, tag);
[tagEntries addObject:@{@"token":token, @"tag":tag}];
}];
return tagEntries;
}
当我尝试在iPhone上打印出可用的方案时,Lexical不是一个选项.怎么会!?
NSArray<NSLinguisticTagScheme> *availSchemes = [NSLinguisticTagger availableTagSchemesForLanguage:@"en"];
for (NSLinguisticTagScheme scheme in availSchemes) {
NSLog(@"Tag scheme %@", scheme);
}
// output:
// Tag scheme Language
// Tag scheme Script
// Tag scheme TokenType
使用iPhone 6和iOS 11.
最佳答案 不幸的是,答案是改变设备.我的iPhone X没有这个问题.这可能是由于拥有6s和更新手机的专用ML芯片.