gpt4 book ai didi

ios - iOS检测NSString语言,英文无法准确检测如何处理?

转载 作者:行者123 更新时间:2023-11-29 04:12:16 26 4
gpt4 key购买 nike

方法:

- (NSString *)languageForString:(NSString *) text{
return (__bridge NSString *)CFStringTokenizerCopyBestStringLanguage((CFStringRef)[text cStringUsingEncoding:NSUnicodeStringEncoding], CFRangeMake(0, MIN(text.length,100)));
}

用途:

NSLog(@"\"%@\" language is %@",@"Tokenizer",[self languageForString:@"tokenizer"]);
NSLog(@"\"%@\" language is %@",@"Tokenizer detect",[self languageForString:@"Tokenizer detect"]);
NSLog(@"\"%@\" language is %@",@"detect",[self languageForString:@"detect"]);
NSLog(@"\"%@\" language is %@",@"我们",[self languageForString:@"我们"]);
NSLog(@"\"%@\" language is %@",@"집안일",[self languageForString:@"집안일"]);
NSLog(@"\"%@\" language is %@",@"Démocratie",[self languageForString:@"Démocratie"]);
NSLog(@"\"%@\" language is %@",@"Tokenizer English",[self languageForString:@"Tokenizer English"]);
NSLog(@"\"%@\" language is %@",@"ここはデパートです",[self languageForString:@"ここはデパートです"]);

输出:

2013-01-09 16:12:28.582 TestCommandLine[6478:c07] "Tokenizer" language is tr<br/>
2013-01-09 16:12:28.586 TestCommandLine[6478:c07] "Tokenizer detect" language is tr<br/>
2013-01-09 16:12:28.586 TestCommandLine[6478:c07] "detect" language is cs<br/>
2013-01-09 16:12:28.587 TestCommandLine[6478:c07] "我们" language is zh-Hans<br/>
2013-01-09 16:12:28.560 TestCommandLine[6478:c07] "집안일" language is ko<br/>
2013-01-09 16:12:28.577 TestCommandLine[6478:c07] "Démocratie" language is fr<br/>
2013-01-09 16:12:28.590 TestCommandLine[6478:c07] "Tokenizer English" language is en<br/>
2013-01-09 16:12:28.591 TestCommandLine[6478:c07] "ここはデパートです" language is ja<br/>

如何变得像:

2013-01-09 16:12:28.582 TestCommandLine[6478:c07] "Tokenizer" language is en<br/>
2013-01-09 16:12:28.586 TestCommandLine[6478:c07] "Tokenizer detect" language is en<br/>
2013-01-09 16:12:28.586 TestCommandLine[6478:c07] "detect" language is en<br/>
2013-01-09 16:12:28.587 TestCommandLine[6478:c07] "我们" language is zh-Hans<br/>
2013-01-09 16:12:28.560 TestCommandLine[6478:c07] "집안일" language is ko<br/>
2013-01-09 16:12:28.577 TestCommandLine[6478:c07] "Démocratie" language is fr<br/>
2013-01-09 16:12:28.590 TestCommandLine[6478:c07] "Tokenizer English" language is en<br/>
2013-01-09 16:12:28.591 TestCommandLine[6478:c07] "ここはデパートです" language is ja<br/>

最佳答案

这是我的解决方案

- (NSString *)detectLanguage {

if ([self isEmpty]) {
return nil;
}

NSString *string = nil;

// You can set a larger detect number here
if (self.length > 30) {
string = self;
} else {
NSMutableString *tempString = [NSMutableString stringWithString:self];

while (tempString.length < 30) {
[tempString appendFormat:@" %@",self];
}

string = tempString;
}

NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
[tagger setString:string];
NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];

if (![language isEqualToString:@"und"]) {
return language;
}

return (__bridge NSString *)CFStringTokenizerCopyBestStringLanguage((CFStringRef)string, CFRangeMake(0, MIN(string.length,400)));
}

关于ios - iOS检测NSString语言,英文无法准确检测如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com