天天看点

iOS利用系统API实现中文分词

不多说,直接贴代码,

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

转载请注明出处:http://blog.csdn.net/chensjjj/article/details/50885310,谢谢