From 8b887055aaccb2309c4727d764eb8f503b16733f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 28 Jun 2012 15:48:09 +0000 Subject: [PATCH] * make movie detection smarter (hopefully) and cover more cases --- .../filebot/format/MediaBindingBean.java | 2 +- .../filebot/media/MediaDetection.java | 23 ++++++++++++++++-- .../filebot/media/ReleaseInfo.java | 2 +- .../filebot/similarity/MetricAvg.java | 24 +++++++++++++++++++ source/net/sourceforge/filebot/web/Movie.java | 2 +- .../sourceforge/filebot/web/TMDbClient.java | 2 +- 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 source/net/sourceforge/filebot/similarity/MetricAvg.java diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 8348b115..7bd253c3 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -147,7 +147,7 @@ public class MediaBindingBean { public String getImdbId() { int imdb = getMovie().getImdbId(); - if (imdb < 0) + if (imdb <= 0) return null; return String.format("%07d", imdb); diff --git a/source/net/sourceforge/filebot/media/MediaDetection.java b/source/net/sourceforge/filebot/media/MediaDetection.java index 08bf19a9..e174bc1b 100644 --- a/source/net/sourceforge/filebot/media/MediaDetection.java +++ b/source/net/sourceforge/filebot/media/MediaDetection.java @@ -40,8 +40,10 @@ import net.sourceforge.filebot.MediaTypes; import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.similarity.CommonSequenceMatcher; import net.sourceforge.filebot.similarity.DateMatcher; +import net.sourceforge.filebot.similarity.MetricAvg; import net.sourceforge.filebot.similarity.NameSimilarityMetric; import net.sourceforge.filebot.similarity.SeasonEpisodeMatcher; +import net.sourceforge.filebot.similarity.SequenceMatchSimilarity; import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.similarity.SimilarityComparator; import net.sourceforge.filebot.similarity.SimilarityMetric; @@ -331,7 +333,24 @@ public class MediaDetection { // query by file / folder name if (queryLookupService != null) { - options.addAll(queryMovieByFileName(terms, queryLookupService, locale)); + Collection results = queryMovieByFileName(terms, queryLookupService, locale); + + // try query without year as it sometimes messes up results if years don't match properly (movie release years vs dvd release year, etc) + if (results.isEmpty() && !strict) { + List termsWithoutYear = new ArrayList(); + Pattern yearPattern = Pattern.compile("(?:19|20)\\d{2}"); + for (String term : terms) { + Matcher m = yearPattern.matcher(term); + if (m.find()) { + termsWithoutYear.add(m.replaceAll("").trim()); + } + } + if (termsWithoutYear.size() > 0) { + results = queryMovieByFileName(termsWithoutYear, queryLookupService, locale); + } + } + + options.addAll(results); } // add local matching after online search @@ -339,7 +358,7 @@ public class MediaDetection { // sort by relevance List optionsByRelevance = new ArrayList(options); - sort(optionsByRelevance, new SimilarityComparator(new NameSimilarityMetric(), stripReleaseInfo(terms, true).toArray())); + sort(optionsByRelevance, new SimilarityComparator(new MetricAvg(new SequenceMatchSimilarity(), new NameSimilarityMetric()), stripReleaseInfo(terms, true).toArray())); return optionsByRelevance; } diff --git a/source/net/sourceforge/filebot/media/ReleaseInfo.java b/source/net/sourceforge/filebot/media/ReleaseInfo.java index fd78dcfd..ce576e91 100644 --- a/source/net/sourceforge/filebot/media/ReleaseInfo.java +++ b/source/net/sourceforge/filebot/media/ReleaseInfo.java @@ -115,7 +115,7 @@ public class ReleaseInfo { Pattern resolution = getResolutionPattern(); Pattern queryBlacklist = getBlacklistPattern(); - Pattern[] stopwords = new Pattern[] { getReleaseGroupPattern(true), languageTag, videoSource, videoFormat, resolution, languageSuffix }; + Pattern[] stopwords = new Pattern[] { languageTag, videoSource, videoFormat, resolution, languageSuffix }; Pattern[] blacklist = new Pattern[] { clutterBracket, releaseGroup, languageTag, videoSource, videoFormat, resolution, languageSuffix, queryBlacklist }; List output = new ArrayList(items.size()); diff --git a/source/net/sourceforge/filebot/similarity/MetricAvg.java b/source/net/sourceforge/filebot/similarity/MetricAvg.java new file mode 100644 index 00000000..e043f2c1 --- /dev/null +++ b/source/net/sourceforge/filebot/similarity/MetricAvg.java @@ -0,0 +1,24 @@ + +package net.sourceforge.filebot.similarity; + + +public class MetricAvg implements SimilarityMetric { + + private final SimilarityMetric[] metrics; + + + public MetricAvg(SimilarityMetric... metrics) { + this.metrics = metrics; + } + + + @Override + public float getSimilarity(Object o1, Object o2) { + float f = 0; + for (SimilarityMetric metric : metrics) { + f += metric.getSimilarity(o1, o2); + } + return f / metrics.length; + } + +} diff --git a/source/net/sourceforge/filebot/web/Movie.java b/source/net/sourceforge/filebot/web/Movie.java index e83c770b..c64a39ab 100644 --- a/source/net/sourceforge/filebot/web/Movie.java +++ b/source/net/sourceforge/filebot/web/Movie.java @@ -64,7 +64,7 @@ public class Movie extends SearchResult { @Override public String toString() { - return String.format("%s (%d)", name, year); + return String.format("%s (%04d)", name, year < 0 ? 0 : year); } } diff --git a/source/net/sourceforge/filebot/web/TMDbClient.java b/source/net/sourceforge/filebot/web/TMDbClient.java index 21dfc07d..c0bc08bc 100644 --- a/source/net/sourceforge/filebot/web/TMDbClient.java +++ b/source/net/sourceforge/filebot/web/TMDbClient.java @@ -129,7 +129,7 @@ public class TMDbClient implements MovieIdentificationService { try { imdbid = new Scanner(getTextContent("imdb_id", node)).useDelimiter("\\D+").nextInt(); } catch (RuntimeException e) { - throw new IllegalArgumentException("Missing data: imdbid"); + // ignore } result.add(new Movie(name, year, imdbid));