Skip to content

Instantly share code, notes, and snippets.

@dolphinSuPixnet
Created October 22, 2015 06:41
Show Gist options
  • Save dolphinSuPixnet/b953d181930fea894a79 to your computer and use it in GitHub Desktop.
Save dolphinSuPixnet/b953d181930fea894a79 to your computer and use it in GitHub Desktop.
NSLinguisticTagger 的一個簡單範例
/*判斷這個 error message 是不是中文:http://stackoverflow.com/questions/6325073/detect-language-of-nsstring*/
//告訴 tagger,我們想要它判斷哪些東西
NSArray *tagsSchemes = @[NSLinguisticTagSchemeLanguage];
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagsSchemes options:0];
//告訴 tagger,我們要它判斷哪句話
[tagger setString:errorMessage];
NSRange range = NSMakeRange(0, errorMessage.length - 1);
//告訴 tagger,我們要它判斷的那句話會是哪種語言,不然它可能會判斷成日文或簡中
NSOrthography *orthography = [NSOrthography orthographyWithDominantScript:@"Hant" languageMap:@{@"Hant":@[@"zh-Hant"]}];
[tagger setOrthography:orthography range:range];
NSArray <NSString *>*stringArray = [tagger tagsInRange:range scheme:NSLinguisticTagSchemeLanguage options:NSLinguisticTaggerJoinNames tokenRanges:nil];
if (!([stringArray containsObject:@"zh-Hant"])) {
failure([NSString stringWithFormat:@"%@ is not Chinese, it's: %@", key, stringArray]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment