Refactor NSLinguisticTagger code.

This commit is contained in:
Damiano Galassi committed 2017-10-02 19:20:17 +02:00
1 parent 0908670715
commit cfbf9bbf91
4 files changed
+64 -90

No files matched your search

+7 -44
View File
@@ -40,55 +40,18 @@
newTrack.alternateGroup = 2;
newTrack.language = getFilenameLanguage((__bridge CFStringRef)self.fileURL.path);
// Check if a 10.10 only class is available, NSLinguisticTagger crashes on 10.9
// if the string contains some characters.
if ([newTrack.language isEqualToString:@"und"] && NSClassFromString(@"NSVisualEffectView")) {
// we couldn't deduce language from the fileURL
// -> Let's look into the file itself
if ([newTrack.language isEqualToString:@"und"]) {
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:fileURL
encoding:NSUTF8StringEncoding
error:nil];
if (stringFromFileAtURL) { // try auto determining
NSArray *tagschemes = @[NSLinguisticTagSchemeLanguage];
NSCountedSet *languagesSet = [NSCountedSet new];
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
[stringFromFileAtURL enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
if (line.length > 1) {
tagger.string = line;
NSOrthography *ortho = [tagger orthographyAtIndex:0 effectiveRange:NULL];
NSString *dominantLanguage = ortho.dominantLanguage;
if (dominantLanguage && ![dominantLanguage isEqualToString:@"und"]) {
[languagesSet addObject:dominantLanguage];
}
}
}];
NSArray *sortedValues = [languagesSet.allObjects sortedArrayUsingComparator:^(id obj1, id obj2) {
NSUInteger n = [languagesSet countForObject:obj1];
NSUInteger m = [languagesSet countForObject:obj2];
return (n <= m)? (n < m)? NSOrderedAscending : NSOrderedSame : NSOrderedDescending;
}];
NSString *language = sortedValues.lastObject;
if (language) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSString *languageName = [locale displayNameForKey:NSLocaleLanguageCode
value:language];
if (languageName) {
newTrack.language = [MP42Languages.defaultManager extendedTagForLang:languageName];
}
NSString *guess = guessStringLanguage(stringFromFileAtURL);
if (guess) {
newTrack.language = guess;
}
}
}
}
}
_ss = [[SBSubSerializer alloc] init];
if ([self.fileURL.pathExtension caseInsensitiveCompare: @"srt"] == NSOrderedSame) {
@@ -97,7 +60,7 @@
success = LoadSMIFromURL(self.fileURL, _ss, 1);
}
[newTrack setDuration:duration];
newTrack.duration = duration;
if (!success) {
if (outError) {