NSLinguisticTagger enumerateTagsInRange doesn'

2020-03-04 04:53发布

Here's the code I'm using, it prints nothing no matter what sentence I use on the device. On simulator it works fine!

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

When I try to print out the available schemes on my iPhone, Lexical is not an option. How come!?

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

Using iPhone 6+ with iOS 11.

1条回答
Luminary・发光体
2楼-- · 2020-03-04 05:15

Unfortunately, the answer was changing devices. My iPhone X does not have this issue. It may be due to having a dedicated ML chip that the 6s and newer phones have.

查看更多
登录 后发表回答