简体   繁体   中英

NSLinguisticTagger enumerateTagsInRange doesn't work on device with NSLinguisticTagSchemeNameTypeOrLexicalClass

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM