//1.创建语句
NSString * question = @"What is the weather in San Francisco?";
//2.创建筛选条件
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerJoinNames |NSLinguisticTaggerOmitPunctuation;
//3.创建自然语言标签
NSLinguisticTagger * tagger = [[NSLinguisticTagger alloc]initWithTagSchemes:[NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options];
//4.给标签附字符串
tagger.string = question;
//5.执行筛选
[tagger enumerateTagsInRange:NSMakeRange(0, question.length) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock:^(NSString * _Nonnull tag, NSRange tokenRange, NSRange sentenceRange, BOOL * _Nonnull stop) {
//6.获取结果
NSString*token = [question substringWithRange:tokenRange];
//7.打印结果
NSLog(@"%@:%@",token,tag);
}];
/**
* 打印结果
2015-10-23 10:07:04.599 NSLinguisticTaggerDemo[34466:941126] What:Pronoun
2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] is:Verb
2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] the:Determiner
2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] weather:Noun
2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] in:Preposition
2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] San Francisco:PlaceName
*/
网友评论