From 9c8da512774ece1a15529a40d90f5e17db0e459a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 10 Feb 2016 18:32:39 +0000 Subject: [PATCH] Cache SeriesNameMatcher objects --- source/net/filebot/cli/CmdlineOperations.java | 4 ++-- source/net/filebot/media/MediaDetection.java | 11 +++++------ source/net/filebot/similarity/EpisodeMetrics.java | 10 ++++++---- source/net/filebot/similarity/SeriesNameMatcher.java | 8 ++++++-- source/net/filebot/ui/list/ListPanel.java | 2 +- .../net/filebot/ui/subtitle/upload/MovieEditor.java | 2 +- test/net/filebot/similarity/DateMatcherTest.java | 6 +++--- test/net/filebot/similarity/DateMetricTest.java | 2 +- .../net/filebot/similarity/SeriesNameMatcherTest.java | 5 +---- 9 files changed, 26 insertions(+), 24 deletions(-) diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index b4e17763..47398719 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -118,7 +118,7 @@ public class CmdlineOperations implements CmdlineInterface { Collection cwsList = emptySet(); if (max >= 5) { - cwsList = getSeriesNameMatcher().matchAll(mediaFiles.toArray(new File[0])); + cwsList = getSeriesNameMatcher(true).matchAll(mediaFiles.toArray(new File[0])); } for (File f : mediaFiles) { @@ -129,7 +129,7 @@ public class CmdlineOperations implements CmdlineInterface { // count CWS matches for (String base : cwsList) { - if (base.equalsIgnoreCase(getSeriesNameMatcher().matchByFirstCommonWordSequence(base, f.getName()))) { + if (base.equalsIgnoreCase(getSeriesNameMatcher(true).matchByFirstCommonWordSequence(base, f.getName()))) { cws++; break; } diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 7cdedcf4..4a42fbb7 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -117,8 +117,7 @@ public class MediaDetection { private static final SeasonEpisodeMatcher seasonEpisodeMatcherStrict = new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.DEFAULT_SANITY, true); private static final SeasonEpisodeMatcher seasonEpisodeMatcherNonStrict = new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.DEFAULT_SANITY, false); - private static final DateMatcher dateMatcher = new DateMatcher(Locale.getDefault(), DateMatcher.DEFAULT_SANITY); - private static final SeriesNameMatcher seriesNameMatcher = new SeriesNameMatcher(Locale.ENGLISH, true); + private static final DateMatcher dateMatcher = new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.ENGLISH, Locale.getDefault()); public static SeasonEpisodeMatcher getSeasonEpisodeMatcher(boolean strict) { return strict ? seasonEpisodeMatcherStrict : seasonEpisodeMatcherNonStrict; @@ -128,8 +127,8 @@ public class MediaDetection { return dateMatcher; } - public static SeriesNameMatcher getSeriesNameMatcher() { - return seriesNameMatcher; + public static SeriesNameMatcher getSeriesNameMatcher(boolean strict) { + return new SeriesNameMatcher(strict ? seasonEpisodeMatcherStrict : seasonEpisodeMatcherNonStrict, dateMatcher); } public static boolean isEpisode(String name, boolean strict) { @@ -330,7 +329,7 @@ public class MediaDetection { } // strict series name matcher for recognizing 1x01 patterns - SeriesNameMatcher strictSeriesNameMatcher = new SeriesNameMatcher(locale, true); + SeriesNameMatcher strictSeriesNameMatcher = getSeriesNameMatcher(true); // cross-reference known series names against file structure try { @@ -396,7 +395,7 @@ public class MediaDetection { for (boolean strict : new boolean[] { true, false }) { if (matches.isEmpty()) { // check CWS matches - SeriesNameMatcher seriesNameMatcher = new SeriesNameMatcher(Locale.ENGLISH, strict); + SeriesNameMatcher seriesNameMatcher = getSeriesNameMatcher(strict); matches.addAll(strictSeriesNameMatcher.matchAll(files.toArray(new File[files.size()]))); // try before SxE pattern diff --git a/source/net/filebot/similarity/EpisodeMetrics.java b/source/net/filebot/similarity/EpisodeMetrics.java index dadb9d45..940df394 100644 --- a/source/net/filebot/similarity/EpisodeMetrics.java +++ b/source/net/filebot/similarity/EpisodeMetrics.java @@ -303,7 +303,7 @@ public enum EpisodeMetrics implements SimilarityMetric { // Match by generic name similarity (absolute) SeriesName(new NameSimilarityMetric() { - private SeriesNameMatcher seriesNameMatcher = new SeriesNameMatcher(Locale.ENGLISH, false); + private final SeriesNameMatcher seriesNameMatcher = getSeriesNameMatcher(false); @Override public float getSimilarity(Object o1, Object o2) { @@ -591,8 +591,10 @@ public enum EpisodeMetrics implements SimilarityMetric { // Match by (region) or (year) hints RegionHint(new SimilarityMetric() { - private Pattern hint = compile("[(](\\p{Alpha}+|\\p{Digit}+)[)]$"); - private Pattern punctuation = compile("[\\p{Punct}\\p{Space}]+"); + private final Pattern hint = compile("[(](\\p{Alpha}+|\\p{Digit}+)[)]$"); + private final Pattern punctuation = compile("[\\p{Punct}\\p{Space}]+"); + + private final SeriesNameMatcher seriesNameMatcher = getSeriesNameMatcher(true); @Override public float getSimilarity(Object o1, Object o2) { @@ -615,7 +617,7 @@ public enum EpisodeMetrics implements SimilarityMetric { for (File f : listPathTail((File) o, 3, true)) { // try to focus on series name String n = f.getName(); - String sn = getSeriesNameMatcher().matchByEpisodeIdentifier(n); + String sn = seriesNameMatcher.matchByEpisodeIdentifier(n); String[] tokens = punctuation.split(sn != null ? sn : n); for (String s : tokens) { diff --git a/source/net/filebot/similarity/SeriesNameMatcher.java b/source/net/filebot/similarity/SeriesNameMatcher.java index 8bd401a8..c7d5ba4f 100644 --- a/source/net/filebot/similarity/SeriesNameMatcher.java +++ b/source/net/filebot/similarity/SeriesNameMatcher.java @@ -36,8 +36,12 @@ public class SeriesNameMatcher { protected final DateMatcher dateMatcher; protected final CommonSequenceMatcher commonSequenceMatcher; - public SeriesNameMatcher(Locale locale, boolean strict) { - this(new NameSimilarityMetric(), getLenientCollator(locale), new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.DEFAULT_SANITY, strict), new DateMatcher(locale, DateMatcher.DEFAULT_SANITY)); + public SeriesNameMatcher(boolean strict) { + this(new NameSimilarityMetric(), getLenientCollator(Locale.ENGLISH), new SmartSeasonEpisodeMatcher(SeasonEpisodeMatcher.DEFAULT_SANITY, strict), new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.ENGLISH)); + } + + public SeriesNameMatcher(SeasonEpisodeMatcher seasonEpisodeMatcher, DateMatcher dateMatcher) { + this(new NameSimilarityMetric(), getLenientCollator(Locale.ENGLISH), seasonEpisodeMatcher, dateMatcher); } public SeriesNameMatcher(SimilarityMetric metric, Collator collator, SeasonEpisodeMatcher seasonEpisodeMatcher, DateMatcher dateMatcher) { diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index 903c968f..aff1b228 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -129,7 +129,7 @@ public class ListPanel extends JComponent { } // try to match title from the first five names - Collection title = getSeriesNameMatcher().matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0])); + Collection title = getSeriesNameMatcher(true).matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0])); list.setTitle(title.isEmpty() ? "List" : title.iterator().next()); diff --git a/source/net/filebot/ui/subtitle/upload/MovieEditor.java b/source/net/filebot/ui/subtitle/upload/MovieEditor.java index 954fc358..af77581b 100644 --- a/source/net/filebot/ui/subtitle/upload/MovieEditor.java +++ b/source/net/filebot/ui/subtitle/upload/MovieEditor.java @@ -35,7 +35,7 @@ class MovieEditor implements TableCellEditor { String fn = FileUtilities.getName(mapping.getVideo() != null ? mapping.getVideo() : mapping.getSubtitle()); // check if query contain an episode identifier - String sn = getSeriesNameMatcher().matchByEpisodeIdentifier(fn); + String sn = getSeriesNameMatcher(true).matchByEpisodeIdentifier(fn); if (sn != null) { return stripReleaseInfo(sn, true); } diff --git a/test/net/filebot/similarity/DateMatcherTest.java b/test/net/filebot/similarity/DateMatcherTest.java index b20abbff..23206ba4 100644 --- a/test/net/filebot/similarity/DateMatcherTest.java +++ b/test/net/filebot/similarity/DateMatcherTest.java @@ -8,7 +8,7 @@ import org.junit.Test; public class DateMatcherTest { - DateMatcher m = new DateMatcher(Locale.ENGLISH, DateMatcher.DEFAULT_SANITY); + DateMatcher m = new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.ENGLISH); @Test public void parse() { @@ -25,8 +25,8 @@ public class DateMatcherTest { @Test public void parseLocale() { - assertEquals("2016-03-01", new DateMatcher(Locale.GERMAN, DateMatcher.DEFAULT_SANITY).match("01 März 2016").toString()); - assertEquals("2016-03-01", new DateMatcher(Locale.GERMAN, DateMatcher.DEFAULT_SANITY).match("1 March 2016").toString()); // always parse English + assertEquals("2016-03-01", new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.GERMAN).match("01 März 2016").toString()); + assertEquals("2016-03-01", new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.GERMAN).match("1 March 2016").toString()); // always parse English } @Test diff --git a/test/net/filebot/similarity/DateMetricTest.java b/test/net/filebot/similarity/DateMetricTest.java index 874c67cb..06d1817d 100644 --- a/test/net/filebot/similarity/DateMetricTest.java +++ b/test/net/filebot/similarity/DateMetricTest.java @@ -8,7 +8,7 @@ import org.junit.Test; public class DateMetricTest { - DateMetric metric = new DateMetric(new DateMatcher(Locale.ENGLISH, DateMatcher.DEFAULT_SANITY)); + DateMetric metric = new DateMetric(new DateMatcher(DateMatcher.DEFAULT_SANITY, Locale.ENGLISH)); @Test public void getSimilarity() { diff --git a/test/net/filebot/similarity/SeriesNameMatcherTest.java b/test/net/filebot/similarity/SeriesNameMatcherTest.java index bed4eb74..47ce9130 100644 --- a/test/net/filebot/similarity/SeriesNameMatcherTest.java +++ b/test/net/filebot/similarity/SeriesNameMatcherTest.java @@ -1,16 +1,13 @@ package net.filebot.similarity; import static org.junit.Assert.*; - -import java.util.Locale; - import net.filebot.similarity.SeriesNameMatcher.SeriesNameCollection; import org.junit.Test; public class SeriesNameMatcherTest { - private static SeriesNameMatcher matcher = new SeriesNameMatcher(Locale.ENGLISH, true); + SeriesNameMatcher matcher = new SeriesNameMatcher(true); @Test public void whitelist() {