Cleanup ``TimeAgoParser``

This commit is contained in:
litetex 2022-03-26 21:09:31 +01:00
parent bb49f7d857
commit 3bf7aa3762
1 changed files with 27 additions and 40 deletions

View File

@ -7,7 +7,6 @@ import org.schabi.newpipe.extractor.utils.Parser;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -58,36 +57,27 @@ public class TimeAgoParser {
} }
} }
int timeAgoAmount; return getResultFor(parseTimeAgoAmount(textualDate), parseChronoUnit(textualDate));
try {
timeAgoAmount = parseTimeAgoAmount(textualDate);
} catch (final NumberFormatException e) {
// If there is no valid number in the textual date,
// assume it is 1 (as in 'a second ago').
timeAgoAmount = 1;
}
final ChronoUnit chronoUnit = parseChronoUnit(textualDate);
return getResultFor(timeAgoAmount, chronoUnit);
} }
private int parseTimeAgoAmount(final String textualDate) throws NumberFormatException { private int parseTimeAgoAmount(final String textualDate) {
return Integer.parseInt(textualDate.replaceAll("\\D+", "")); try {
return Integer.parseInt(textualDate.replaceAll("\\D+", ""));
} catch (final NumberFormatException ignored) {
// If there is no valid number in the textual date,
// assume it is 1 (as in 'a second ago').
return 1;
}
} }
private ChronoUnit parseChronoUnit(final String textualDate) throws ParsingException { private ChronoUnit parseChronoUnit(final String textualDate) throws ParsingException {
for (final Map.Entry<ChronoUnit, Collection<String>> entry return patternsHolder.asMap().entrySet().stream()
: patternsHolder.asMap().entrySet()) { .filter(e -> e.getValue().stream()
final ChronoUnit chronoUnit = entry.getKey(); .anyMatch(agoPhrase -> textualDateMatches(textualDate, agoPhrase)))
.map(Map.Entry::getKey)
for (final String agoPhrase : entry.getValue()) { .findFirst()
if (textualDateMatches(textualDate, agoPhrase)) { .orElseThrow(() ->
return chronoUnit; new ParsingException("Unable to parse the date: " + textualDate));
}
}
}
throw new ParsingException("Unable to parse the date: " + textualDate);
} }
private boolean textualDateMatches(final String textualDate, final String agoPhrase) { private boolean textualDateMatches(final String textualDate, final String agoPhrase) {
@ -97,24 +87,21 @@ public class TimeAgoParser {
if (patternsHolder.wordSeparator().isEmpty()) { if (patternsHolder.wordSeparator().isEmpty()) {
return textualDate.toLowerCase().contains(agoPhrase.toLowerCase()); return textualDate.toLowerCase().contains(agoPhrase.toLowerCase());
} else { }
final String escapedPhrase = Pattern.quote(agoPhrase.toLowerCase());
final String escapedSeparator; final String escapedPhrase = Pattern.quote(agoPhrase.toLowerCase());
if (patternsHolder.wordSeparator().equals(" ")) { final String escapedSeparator = patternsHolder.wordSeparator().equals(" ")
// From JDK8 \h - Treat horizontal spaces as a normal one // From JDK8 \h - Treat horizontal spaces as a normal one
// (non-breaking space, thin space, etc.) // (non-breaking space, thin space, etc.)
escapedSeparator = "[ \\t\\xA0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]"; ? "[ \\t\\xA0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]"
} else { : Pattern.quote(patternsHolder.wordSeparator());
escapedSeparator = Pattern.quote(patternsHolder.wordSeparator());
}
// (^|separator)pattern($|separator) // (^|separator)pattern($|separator)
// Check if the pattern is surrounded by separators or start/end of the string. // Check if the pattern is surrounded by separators or start/end of the string.
final String pattern = final String pattern =
"(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")"; "(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")";
return Parser.isMatch(pattern, textualDate.toLowerCase()); return Parser.isMatch(pattern, textualDate.toLowerCase());
}
} }
private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) { private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) {