From 4038e7a6af1825fc590a53ff56817928bb711ff7 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 7 Apr 2014 06:00:14 +0000 Subject: [PATCH] * make sure SxE matcher doesn't fall back on release info patterns like x264 or 720p --- .../filebot/media/MediaDetection.java | 31 +------------- .../media/SmartSeasonEpisodeMatcher.java | 40 +++++++++++++++++++ .../filebot/similarity/EpisodeMetrics.java | 3 +- .../similarity/SeasonEpisodeMetric.java | 33 ++++++++------- .../similarity/SeasonEpisodeMatcherTest.java | 12 ++++-- 5 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 source/net/sourceforge/filebot/media/SmartSeasonEpisodeMatcher.java diff --git a/source/net/sourceforge/filebot/media/MediaDetection.java b/source/net/sourceforge/filebot/media/MediaDetection.java index 7fcfb2de..db4dbe6f 100644 --- a/source/net/sourceforge/filebot/media/MediaDetection.java +++ b/source/net/sourceforge/filebot/media/MediaDetection.java @@ -116,8 +116,8 @@ public class MediaDetection { return releaseInfo.getLanguageSuffix(getName(file)); } - private static final SeasonEpisodeMatcher seasonEpisodeMatcherStrict = new SeasonEpisodeMatcherWithFilter(true); - private static final SeasonEpisodeMatcher seasonEpisodeMatcherNonStrict = new SeasonEpisodeMatcherWithFilter(false); + private static final SeasonEpisodeMatcher seasonEpisodeMatcherStrict = new SmartSeasonEpisodeMatcher(true); + private static final SeasonEpisodeMatcher seasonEpisodeMatcherNonStrict = new SmartSeasonEpisodeMatcher(false); public static SeasonEpisodeMatcher getSeasonEpisodeMatcher(boolean strict) { return strict ? seasonEpisodeMatcherStrict : seasonEpisodeMatcherNonStrict; @@ -1283,33 +1283,6 @@ public class MediaDetection { } } - private static class SeasonEpisodeMatcherWithFilter extends SeasonEpisodeMatcher { - - private final Pattern ignorePattern = MediaDetection.releaseInfo.getVideoFormatPattern(false); - - public SeasonEpisodeMatcherWithFilter(boolean strict) { - super(DEFAULT_SANITY, strict); - } - - protected String clean(CharSequence name) { - return ignorePattern.matcher(name).replaceAll(""); - } - - @Override - public List match(CharSequence name) { - return super.match(clean(name)); - } - - @Override - protected List tokenizeTail(File file) { - List tail = super.tokenizeTail(file); - for (int i = 0; i < tail.size(); i++) { - tail.set(i, clean(tail.get(i))); - } - return tail; - } - } - public static Comparator VIDEO_SIZE_ORDER = new Comparator() { @Override diff --git a/source/net/sourceforge/filebot/media/SmartSeasonEpisodeMatcher.java b/source/net/sourceforge/filebot/media/SmartSeasonEpisodeMatcher.java new file mode 100644 index 00000000..e2560607 --- /dev/null +++ b/source/net/sourceforge/filebot/media/SmartSeasonEpisodeMatcher.java @@ -0,0 +1,40 @@ +package net.sourceforge.filebot.media; + +import java.io.File; +import java.util.List; +import java.util.regex.Pattern; + +import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher; + +public class SmartSeasonEpisodeMatcher extends SeasonEpisodeMatcher { + + // make sure certain patterns like x264 or 720p will never be interpreted as SxE numbers + private Pattern ignorePattern = new ReleaseInfo().getVideoFormatPattern(false); + + public SmartSeasonEpisodeMatcher(SeasonEpisodeFilter sanity, boolean strict) { + super(sanity, strict); + } + + public SmartSeasonEpisodeMatcher(boolean strict) { + super(DEFAULT_SANITY, strict); + } + + protected String clean(CharSequence name) { + return ignorePattern.matcher(name).replaceAll(""); + } + + @Override + public List match(CharSequence name) { + return super.match(clean(name)); + } + + @Override + protected List tokenizeTail(File file) { + List tail = super.tokenizeTail(file); + for (int i = 0; i < tail.size(); i++) { + tail.set(i, clean(tail.get(i))); + } + return tail; + } + +} diff --git a/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java b/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java index a8a6cff0..9e57715a 100644 --- a/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java +++ b/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java @@ -29,6 +29,7 @@ import java.util.regex.Pattern; import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.media.ReleaseInfo; +import net.sourceforge.filebot.media.SmartSeasonEpisodeMatcher; import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; import net.sourceforge.filebot.vfs.FileInfo; import net.sourceforge.filebot.web.Date; @@ -43,7 +44,7 @@ import com.ibm.icu.text.Transliterator; public enum EpisodeMetrics implements SimilarityMetric { // Match by season / episode numbers - SeasonEpisode(new SeasonEpisodeMetric() { + SeasonEpisode(new SeasonEpisodeMetric(new SmartSeasonEpisodeMatcher(null, false)) { private final Map> transformCache = synchronizedMap(new HashMap>(64, 4)); diff --git a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java index 133e8391..925bc502 100644 --- a/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java +++ b/source/net/sourceforge/filebot/similarity/SeasonEpisodeMetric.java @@ -1,28 +1,32 @@ - package net.sourceforge.filebot.similarity; - import java.io.File; import java.util.Collection; import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; - public class SeasonEpisodeMetric implements SimilarityMetric { - - private final SeasonEpisodeMatcher seasonEpisodeMatcher = new SeasonEpisodeMatcher(null, false); - - + + private SeasonEpisodeMatcher seasonEpisodeMatcher; + + public SeasonEpisodeMetric() { + this.seasonEpisodeMatcher = new SeasonEpisodeMatcher(null, false); + } + + public SeasonEpisodeMetric(SeasonEpisodeMatcher seasonEpisodeMatcher) { + this.seasonEpisodeMatcher = seasonEpisodeMatcher; + } + @Override public float getSimilarity(Object o1, Object o2) { Collection sxeVector1 = parse(o1); if (sxeVector1 == null || sxeVector1.isEmpty()) return 0; - + Collection sxeVector2 = parse(o2); if (sxeVector2 == null || sxeVector2.isEmpty()) return 0; - + float similarity = -1; for (SxE sxe1 : sxeVector1) { for (SxE sxe2 : sxeVector2) { @@ -30,24 +34,23 @@ public class SeasonEpisodeMetric implements SimilarityMetric { // vectors have at least one perfect episode match in common (require season >= 0 as to put less trust in single-number matches) return 1; } - + if ((sxe1.season >= 0 && sxe1.season == sxe2.season) || (sxe1.episode >= 0 && sxe1.episode == sxe2.episode)) { // at least we have a partial match similarity = 0.5f; } } } - + return similarity; } - - + protected Collection parse(Object object) { if (object instanceof File) { return seasonEpisodeMatcher.match((File) object); } - + return seasonEpisodeMatcher.match(object.toString()); } - + } diff --git a/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java index 0207520f..aa40054e 100644 --- a/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java +++ b/test/net/sourceforge/filebot/similarity/SeasonEpisodeMatcherTest.java @@ -3,6 +3,7 @@ package net.sourceforge.filebot.similarity; import static java.util.Arrays.*; import static net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE.*; import static org.junit.Assert.*; +import net.sourceforge.filebot.media.MediaDetection; import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher.SxE; import org.junit.Test; @@ -78,9 +79,6 @@ public class SeasonEpisodeMatcherTest { // test season digits <= 19 assertEquals(new SxE(null, 16), matcher.match("E16").get(0)); - // test look-ahead - assertEquals(asList(new SxE(7, 20)), matcher.match("720p")); - // test ambiguous match processing assertEquals(asList(new SxE(1, 1), new SxE(UNDEFINED, 101)), matcher.match("Test.101")); @@ -112,4 +110,12 @@ public class SeasonEpisodeMatcherTest { assertEquals(new SxE(1, 4), matcher.match("1x01.1x02.1x03.1x04").get(3)); } + @Test + public void withReleaseInfo() { + assertEquals("[7x20]", matcher.match("720p").toString()); + + SeasonEpisodeMatcher smartMatcher = MediaDetection.getSeasonEpisodeMatcher(true); + assertEquals(null, smartMatcher.match("720p")); + } + }