自然语言NSLinguisticTagger

2016-11-01  本文已影响75人  muyang_js的简书

//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.打印结果

DLog(@"token---%@:tag---%@",token, tag);

}];

打印结果

上一篇 下一篇

猜你喜欢

热点阅读