From 01f54f8e86179c14f008c60717304a2eb17350c5 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 24 Oct 2012 15:20:47 +0000 Subject: [PATCH] * make use of MetaAttributes to nudge matching a little bit in that direction --- .../similarity/CrossPropertyMetric.java | 57 +++++++++++++++++++ .../filebot/similarity/EpisodeMetrics.java | 29 +++++++++- .../similarity/StringEqualsMetric.java | 21 +++++++ .../filebot/media/ReleaseInfoTest.java | 4 +- 4 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 source/net/sourceforge/filebot/similarity/CrossPropertyMetric.java create mode 100644 source/net/sourceforge/filebot/similarity/StringEqualsMetric.java diff --git a/source/net/sourceforge/filebot/similarity/CrossPropertyMetric.java b/source/net/sourceforge/filebot/similarity/CrossPropertyMetric.java new file mode 100644 index 00000000..333d1df7 --- /dev/null +++ b/source/net/sourceforge/filebot/similarity/CrossPropertyMetric.java @@ -0,0 +1,57 @@ + +package net.sourceforge.filebot.similarity; + + +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import net.sourceforge.filebot.format.PropertyBindings; + + +public class CrossPropertyMetric implements SimilarityMetric { + + private SimilarityMetric metric; + + + public CrossPropertyMetric(SimilarityMetric metric) { + this.metric = metric; + } + + + public CrossPropertyMetric() { + this.metric = new StringEqualsMetric(); + } + + + @Override + public float getSimilarity(Object o1, Object o2) { + Map m1 = getProperties(o1); + Map m2 = getProperties(o2); + + Set keys = new TreeSet(String.CASE_INSENSITIVE_ORDER); + keys.addAll(m1.keySet()); + keys.retainAll(m2.keySet()); + + if (keys.isEmpty()) { + return 0; + } + + float feedback = 0; + for (String k : keys) { + try { + feedback += metric.getSimilarity(m1.get(k), m2.get(k)); + } catch (Exception e) { + // ignore + } + } + + return feedback / keys.size(); + } + + + protected Map getProperties(Object object) { + return new PropertyBindings(object, null); + } + +} diff --git a/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java b/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java index d90c3ae2..b01fd551 100644 --- a/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java +++ b/source/net/sourceforge/filebot/similarity/EpisodeMetrics.java @@ -308,6 +308,31 @@ public enum EpisodeMetrics implements SimilarityMetric { return super.getTimeStamp(object); } + }), + + // Match by stored MetaAttributes if possible + MetaAttributes(new CrossPropertyMetric() { + + @Override + protected Map getProperties(Object object) { + // Episode / Movie objects + if (object instanceof Episode || object instanceof Movie) { + return super.getProperties(object); + } + + // deserialize MetaAttributes if available + if (object instanceof File) { + try { + return super.getProperties(new net.sourceforge.filebot.media.MetaAttributes((File) object).getMetaData()); + } catch (Throwable e) { + // ignore + } + } + + // ignore everything else + return emptyMap(); + }; + }); // inner metric @@ -365,9 +390,9 @@ public enum EpisodeMetrics implements SimilarityMetric { // 7 pass: prefer episodes that were aired closer to the last modified date of the file // 8 pass: resolve remaining collisions via absolute string similarity if (includeFileMetrics) { - return new SimilarityMetric[] { FileSize, new MetricCascade(FileName, EpisodeFunnel), EpisodeBalancer, SubstringFields, new MetricCascade(SubstringSequence, Name), Numeric, Name, TimeStamp, new NameSimilarityMetric() }; + return new SimilarityMetric[] { FileSize, new MetricCascade(FileName, EpisodeFunnel), EpisodeBalancer, SubstringFields, MetaAttributes, new MetricCascade(SubstringSequence, Name), Numeric, Name, TimeStamp, new NameSimilarityMetric() }; } else { - return new SimilarityMetric[] { EpisodeFunnel, EpisodeBalancer, SubstringFields, new MetricCascade(SubstringSequence, Name), Numeric, Name, TimeStamp, new NameSimilarityMetric() }; + return new SimilarityMetric[] { EpisodeFunnel, EpisodeBalancer, SubstringFields, MetaAttributes, new MetricCascade(SubstringSequence, Name), Numeric, Name, TimeStamp, new NameSimilarityMetric() }; } } diff --git a/source/net/sourceforge/filebot/similarity/StringEqualsMetric.java b/source/net/sourceforge/filebot/similarity/StringEqualsMetric.java new file mode 100644 index 00000000..cabd887d --- /dev/null +++ b/source/net/sourceforge/filebot/similarity/StringEqualsMetric.java @@ -0,0 +1,21 @@ + +package net.sourceforge.filebot.similarity; + + +public class StringEqualsMetric implements SimilarityMetric { + + @Override + public float getSimilarity(Object o1, Object o2) { + if (o1 == null || o2 == null) + return 0; + + String s1 = o1.toString(); + String s2 = o2.toString(); + + if (s1.isEmpty() || s2.isEmpty()) + return 0; + + return s1.equalsIgnoreCase(s2) ? 1 : 0; + } + +} diff --git a/test/net/sourceforge/filebot/media/ReleaseInfoTest.java b/test/net/sourceforge/filebot/media/ReleaseInfoTest.java index 8351511d..af8a2bbb 100644 --- a/test/net/sourceforge/filebot/media/ReleaseInfoTest.java +++ b/test/net/sourceforge/filebot/media/ReleaseInfoTest.java @@ -16,7 +16,7 @@ public class ReleaseInfoTest { ReleaseInfo info = new ReleaseInfo(); File f = new File("Jurassic.Park[1993]DvDrip-aXXo.avi"); - assertEquals("DVDRip", info.getVideoSource(f)); + assertEquals("DVDRip", info.getVideoSource(f.getName())); } @@ -25,7 +25,7 @@ public class ReleaseInfoTest { ReleaseInfo info = new ReleaseInfo(); File f = new File("Jurassic.Park[1993]DvDrip-aXXo.avi"); - assertEquals("aXXo", info.getReleaseGroup(f)); + assertEquals("aXXo", info.getReleaseGroup(f.getName())); } }