iOS分词

最近锤子科技发布新手机,手机推出重大功能分词(Big Bang),其实 iOS 拥有分词的 API,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

- (NSArray *)stringTokenizerWithWord:(NSString *)word {
NSMutableArray *keyWords = [NSMutableArray new];
CFStringTokenizerRef ref = CFStringTokenizerCreate(NULL, (__bridge CFStringRef)word, CFRangeMake(0, word.length),kCFStringTokenizerUnitWord,NULL);
CFRange range;
NSString *keyWord;
do {
CFStringTokenizerAdvanceToNextToken(ref);
range = CFStringTokenizerGetCurrentTokenRange(ref);
if (range.length == 0) break;
keyWord = [word substringWithRange:NSMakeRange(range.location, range.length)];
[keyWords addObject:keyWord];
} while (range.length > 0);
return keyWords;
}