Created
October 22, 2015 06:41
-
-
Save dolphinSuPixnet/b953d181930fea894a79 to your computer and use it in GitHub Desktop.
NSLinguisticTagger 的一個簡單範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*判斷這個 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