Skip to content

Commit

Permalink
SubtitleManager: simpler logic and remove dupplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
courville committed May 8, 2024
1 parent 33128ef commit e0506a8
Showing 1 changed file with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public List<SubtitleFile> listLocalAndRemotesSubtitles(Uri video, boolean addAll
public static String getLanguage3(String basename) {
// extract the 2 or 3 letters language code in a string located at after the start of the string or character "_" or "." or "]" till the end of the string or till a closing ".HI"
// for some reason, some yts subtitles have a .HI at the end of the filename, and apparently this is not for Hindi but Hearing Impaired, note that they are preceded by SDH for Deaf and hard of Hearing
Pattern pattern = Pattern.compile("(?:[_\\-.\\]]|^)([a-zA-Z]{2,3})(" + SEP + "(HI|SDH)|$)");
Pattern pattern = Pattern.compile("(?:^|" + SEP + ")(" + COUNTRYCODE + ")(?:" + SEP + HI + "|$)");
Matcher matcher = pattern.matcher(basename);
if (matcher.find()) {
return matcher.group(1);
Expand All @@ -522,15 +522,9 @@ public static String getLanguage3(String basename) {
}
}

private static final String SEP = "(^|[\\p{Punct}\\s]++|$)";

public static boolean isHearingImpaired(String input) {
if (input == null) return false;
// HI and SDH case sensitive for hearing impaired
String regex = ".*" + SEP + "(HI|SDH)" + SEP + ".*";
log.debug("isHearingImpaired: " + input + " -> " + input.matches(regex));
return input.matches(regex);
}
private static final String SEP = "[\\p{Punct}\\s]++";
private static final String COUNTRYCODE = "[a-zA-Z]{2,3}";
private static final String HI = "(HI|SDH)";

public static String getSubLanguageFromSubPath(Context context, String path) {
String subFilenameWithoutExtension = stripExtensionFromName(getName(path));
Expand All @@ -549,7 +543,7 @@ public static String getSubLanguageFromSubPath(Context context, String path) {
String subLanguageName = null;
if (lang != null) {
// treat yts SDH and HI as hearing impaired e.g. SDH.eng.HI.srt
if (isHearingImpaired(subFilenameWithoutExtension))
if (isSubtitleHearingImpaired(subFilenameWithoutExtension))
subLanguageName = getLanguageNameForLetterCode(context, lang) + " (HI)";
else subLanguageName = getLanguageNameForLetterCode(context, lang);
} else subLanguageName = subFilenameWithoutExtension;
Expand All @@ -560,7 +554,7 @@ public static String getSubLanguageFromSubPath(Context context, String path) {
public static boolean isSubtitleHearingImpaired(String basename) {
// extract the 2 or 3 letters language code in a string located at after the start of the string or character "_" or "." or "]" till the end of the string or till a closing ".HI"
// for some reason, some yts subtitles have a .HI at the end of the filename, and apparently this is not for Hindi but Hearing Impaired, note that they are preceded by SDH for Deaf and hard of Hearing
Pattern pattern = Pattern.compile("(?:[_\\-.\\]]|^)([a-zA-Z]{2,3})\\.HI$");
Pattern pattern = Pattern.compile("(?:^|" + SEP + ")(" + COUNTRYCODE + ")" + SEP + HI + "$");
Matcher matcher = pattern.matcher(basename);
//log.debug("isSubtitleHearingImpaired: " + basename + " -> " + matcher.group(1));
return matcher.find();
Expand Down

0 comments on commit e0506a8

Please sign in to comment.