From b08ec77642db1fec0d740545870cab2b0d73afbe Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 22 Nov 2011 14:44:54 +0000 Subject: [PATCH] * sanity check for SxE values --- .../similarity/SeasonEpisodeMatcher.java | 52 +++++++++++++++---- .../similarity/SeasonEpisodeMetric.java | 2 +- .../filebot/similarity/SeriesNameMatcher.java | 3 +- .../similarity/SeasonEpisodeMatcherTest.java | 2 +- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java index 9e07ab43..ca0a268f 100644 --- a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java +++ b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMatcher.java @@ -17,17 +17,17 @@ public class SeasonEpisodeMatcher { private final SeasonEpisodePattern[] patterns; - public SeasonEpisodeMatcher() { + public SeasonEpisodeMatcher(SeasonEpisodeFilter sanity) { patterns = new SeasonEpisodePattern[3]; // match patterns like S01E01, s01e02, ... [s01]_[e02], s01.e02, s01e02a, s2010e01 ... - patterns[0] = new SeasonEpisodePattern("(? process(MatchResult match) { @@ -147,13 +147,35 @@ public class SeasonEpisodeMatcher { } - protected static class SeasonEpisodePattern { + public static class SeasonEpisodeFilter { - protected final Pattern pattern; + public final int seasonLimit; + public final int seasonEpisodeLimit; + public final int absoluteEpisodeLimit; - public SeasonEpisodePattern(String pattern) { + public SeasonEpisodeFilter(int seasonLimit, int seasonEpisodeLimit, int absoluteEpisodeLimit) { + this.seasonLimit = seasonLimit; + this.seasonEpisodeLimit = seasonEpisodeLimit; + this.absoluteEpisodeLimit = absoluteEpisodeLimit; + } + + + boolean filter(SxE sxe) { + return (sxe.season >= 0 && sxe.season < seasonLimit && sxe.episode < seasonEpisodeLimit) || (sxe.season < 0 && sxe.episode < absoluteEpisodeLimit); + } + } + + + public static class SeasonEpisodePattern { + + protected final Pattern pattern; + protected final SeasonEpisodeFilter sanity; + + + public SeasonEpisodePattern(SeasonEpisodeFilter sanity, String pattern) { this.pattern = Pattern.compile(pattern); + this.sanity = sanity; } @@ -174,7 +196,11 @@ public class SeasonEpisodeMatcher { Matcher matcher = matcher(name); while (matcher.find()) { - matches.addAll(process(matcher)); + for (SxE value : process(matcher)) { + if (sanity == null || sanity.filter(value)) { + matches.add(value); + } + } } return matches; @@ -182,10 +208,14 @@ public class SeasonEpisodeMatcher { public int find(CharSequence name, int fromIndex) { - Matcher matcher = matcher(name); + Matcher matcher = matcher(name).region(fromIndex, name.length()); - if (matcher.find(fromIndex)) { - return matcher.start(); + while (matcher.find()) { + for (SxE value : process(matcher)) { + if (sanity == null || sanity.filter(value)) { + return matcher.start(); + } + } } return -1; diff --git a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java index 627c0579..12cf36ee 100644 --- a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java +++ b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java @@ -10,7 +10,7 @@ import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; public class SeasonEpisodeMetric implements SimilarityMetric { - private final SeasonEpisodeMatcher seasonEpisodeMatcher = new SeasonEpisodeMatcher(); + private final SeasonEpisodeMatcher seasonEpisodeMatcher = new SeasonEpisodeMatcher(null); @Override diff --git a/source/net/sourceforge/filebot/similarity/SeriesNameMatcher.java b/source/net/sourceforge/filebot/similarity/SeriesNameMatcher.java index 0150d1c9..6ea9184d 100644 --- a/source/net/sourceforge/filebot/similarity/SeriesNameMatcher.java +++ b/source/net/sourceforge/filebot/similarity/SeriesNameMatcher.java @@ -21,13 +21,14 @@ import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SeasonEpisodeFilter; import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; import net.sourceforge.tuned.FileUtilities; public class SeriesNameMatcher { - protected final SeasonEpisodeMatcher seasonEpisodeMatcher = new SeasonEpisodeMatcher(); + protected final SeasonEpisodeMatcher seasonEpisodeMatcher = new SeasonEpisodeMatcher(new SeasonEpisodeFilter(30, 50, 1000)); protected final NameSimilarityMetric nameSimilarityMetric = new NameSimilarityMetric(); protected final int commonWordSequenceMaxStartIndex = 3; diff --git a/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java index 29f46771..3fa250b6 100644 --- a/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java +++ b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java @@ -13,7 +13,7 @@ import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; public class SeasonEpisodeMatcherTest { - private static SeasonEpisodeMatcher matcher = new SeasonEpisodeMatcher(); + private static SeasonEpisodeMatcher matcher = new SeasonEpisodeMatcher(null); @Test