From 6746fc8b2692ca42d7b4cbfa60f33f90998d1f22 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 7 Jan 2014 13:01:49 +0000 Subject: [PATCH] * generate CollationKey objects as needed to improved perceived performance --- .../filebot/media/MediaDetection.java | 77 ++++++++++++------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/source/net/sourceforge/filebot/media/MediaDetection.java b/source/net/sourceforge/filebot/media/MediaDetection.java index d288e7f9..3f91089a 100644 --- a/source/net/sourceforge/filebot/media/MediaDetection.java +++ b/source/net/sourceforge/filebot/media/MediaDetection.java @@ -432,13 +432,13 @@ public class MediaDetection { for (CollationKey[] name : names) { IndexEntry bestMatch = null; for (IndexEntry it : getSeriesIndex()) { - CollationKey[] commonName = nameMatcher.matchFirstCommonSequence(name, it.lenientKey); - if (commonName != null && commonName.length >= it.lenientKey.length && (bestMatch == null || commonName.length > bestMatch.lenientKey.length)) { + CollationKey[] commonName = nameMatcher.matchFirstCommonSequence(name, it.getLenientKey()); + if (commonName != null && commonName.length >= it.getLenientKey().length && (bestMatch == null || commonName.length > bestMatch.getLenientKey().length)) { bestMatch = it; } } if (bestMatch != null) { - matches.add(bestMatch.lenientName); + matches.add(bestMatch.getLenientName()); } } @@ -472,7 +472,7 @@ public class MediaDetection { List seriesList = new ArrayList(); for (IndexEntry it : getSeriesIndex()) { - String name = spacing.matcher(it.lenientName).replaceAll("").toLowerCase(); + String name = spacing.matcher(it.getLenientName()).replaceAll("").toLowerCase(); for (String term : terms) { if (term.contains(name)) { if (metric.getSimilarity(term, name) >= similarityThreshold) { @@ -785,15 +785,15 @@ public class MediaDetection { for (IndexEntry movie : getMovieIndex()) { for (CollationKey[] name : names) { - CollationKey[] commonName = nameMatcher.matchFirstCommonSequence(name, movie.lenientKey); - if (commonName != null && commonName.length >= movie.lenientKey.length) { - CollationKey[] strictCommonName = nameMatcher.matchFirstCommonSequence(name, movie.strictKey); - if (strictCommonName != null && strictCommonName.length >= movie.strictKey.length) { + CollationKey[] commonName = nameMatcher.matchFirstCommonSequence(name, movie.getLenientKey()); + if (commonName != null && commonName.length >= movie.getLenientKey().length) { + CollationKey[] strictCommonName = nameMatcher.matchFirstCommonSequence(name, movie.getStrictKey()); + if (strictCommonName != null && strictCommonName.length >= movie.getStrictKey().length) { // prefer strict match - matchMap.put(movie.object, movie.strictName); + matchMap.put(movie.getObject(), movie.getStrictName()); } else if (!strict) { // make sure the common identifier is not just the year - matchMap.put(movie.object, movie.lenientName); + matchMap.put(movie.getObject(), movie.getLenientName()); } } } @@ -830,14 +830,14 @@ public class MediaDetection { LinkedList movies = new LinkedList(); for (IndexEntry it : getMovieIndex()) { - String name = spacing.matcher(it.lenientName).replaceAll("").toLowerCase(); + String name = spacing.matcher(it.getLenientName()).replaceAll("").toLowerCase(); for (String term : terms) { if (term.contains(name)) { - String year = String.valueOf(it.object.getYear()); + String year = String.valueOf(it.getObject().getYear()); if (term.contains(year) && metric.getSimilarity(term, name + year) > similarityThreshold) { - movies.addFirst(it.object); + movies.addFirst(it.getObject()); } else if (metric.getSimilarity(term, name) > similarityThreshold) { - movies.addLast(it.object); + movies.addLast(it.getObject()); } break; } @@ -1086,18 +1086,43 @@ public class MediaDetection { public static class IndexEntry implements Serializable { - private final T object; - private final String lenientName; - private final String strictName; - private final CollationKey[] lenientKey; - private final CollationKey[] strictKey; + private T object; + private String lenientName; + private String strictName; - public IndexEntry(T object, String lenientName, String strictName, CollationKey[] lenientKey, CollationKey[] strictKey) { + private transient CollationKey[] lenientKey; + private transient CollationKey[] strictKey; + + public IndexEntry(T object, String lenientName, String strictName) { this.object = object; this.lenientName = lenientName; this.strictName = strictName; - this.lenientKey = lenientKey; - this.strictKey = strictKey; + } + + public T getObject() { + return object; + } + + public String getLenientName() { + return lenientName; + } + + public String getStrictName() { + return strictName; + } + + public CollationKey[] getLenientKey() { + if (lenientKey == null && lenientName != null) { + lenientKey = HighPerformanceMatcher.prepare(lenientName); + } + return lenientKey; + } + + public CollationKey[] getStrictKey() { + if (strictKey == null && strictName != null) { + strictKey = HighPerformanceMatcher.prepare(strictName); + } + return strictKey; } } @@ -1110,7 +1135,7 @@ public class MediaDetection { private static final Pattern space = Pattern.compile("\\s+"); public static CollationKey[] prepare(String sequence) { - String[] words = space.split(normalizePunctuation(sequence)); + String[] words = space.split(sequence); CollationKey[] keys = new CollationKey[words.length]; for (int i = 0; i < words.length; i++) { keys[i] = collator.getCollationKey(words[i]); @@ -1121,7 +1146,7 @@ public class MediaDetection { public static List prepare(Collection sequences) { List result = new ArrayList(sequences.size()); for (String it : sequences) { - result.add(prepare(it)); + result.add(prepare(normalizePunctuation(it))); } return result; } @@ -1134,7 +1159,7 @@ public class MediaDetection { for (int i = 0; i < effectiveNames.size(); i++) { String lenientName = normalizePunctuation(effectiveNamesWithoutYear.get(i)); String strictName = normalizePunctuation(effectiveNames.get(i)); - index.add(new IndexEntry(m, lenientName, strictName, prepare(lenientName), prepare(strictName))); + index.add(new IndexEntry(m, lenientName, strictName)); } return index; } @@ -1145,7 +1170,7 @@ public class MediaDetection { for (int i = 0; i < effectiveNames.size(); i++) { String lenientName = normalizePunctuation(effectiveNames.get(i)); - index.add(new IndexEntry(r, lenientName, null, prepare(lenientName), null)); + index.add(new IndexEntry(r, lenientName, null)); } return index; }