iphone sdk:将中文句子分成单词和字母

时间:2014-07-02 12:10:09

标签: ios nsstring chinese-locale word-processor

我有中文新闻提要,我想将句子分成更小的块以传递给API。

我怎么能在ios中做到这一点?我为英语设置了50个字符的字符长度。

目前我正在使用rangeOfString:函数查找点,逗号和分句。

NSString *str  = nil, *rem = nil;

str = [final substringToIndex:MAX_CHAR_Private];
rem = [final substringFromIndex:MAX_CHAR_Private];
NSRange rng = [rem rangeOfString:@"?"];
if (rng.location == NSNotFound) {
    rng = [rem rangeOfString:@"!"];
    if (rng.location == NSNotFound) {
        rng = [rem rangeOfString:@","];
        if (rng.location == NSNotFound) {
            rng = [rem rangeOfString:@"."];
            if (rng.location == NSNotFound) {
                rng = [rem rangeOfString:@" "];
            }
        }
    }
}
if (rng.location+1 + MAX_CHAR_Private > MAXIMUM_LIMIT_Private) {
    rng = [rem rangeOfString:@" "];
}

if (rng.location == NSNotFound) {
    remaining = [[final substringFromIndex:MAX_CHAR_Private] retain];
}
else{
    //NSRange rng = [rem rangeOfString:@" "];
    str = [str stringByAppendingString:[rem substringToIndex:rng.location]];
    remaining = [[final substringFromIndex:MAX_CHAR_Private + rng.location+1] retain];
}

这对中文和日文字符无效。

2 个答案:

答案 0 :(得分:1)

检查NSLinguisticTagger,它应该适用于中文:

来自Apple:" NSLinguisticTagger类用于自动分割自然语言文本并使用信息(例如词性)对其进行标记。它还可以标记语言,脚本,单词形式等等。"

Apple文档NSLinguisticTagger Class Reference

另见NSHipster NSLinguisticTagger

另见objc.io issue 7

答案 1 :(得分:0)

NSString提供开箱即用的NSStringEnumerationBySentences枚举选项:

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationBySentences
                       usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
    {
        NSString *sentence = [substring stringByTrimmingCharactersInSet:whiteSpaceSet];
        // process sentence
    }
];