This commit is contained in:
Reinhard Pointner 2016-03-12 14:09:14 +00:00
parent 117b3be318
commit 907bc11aad
1 changed files with 23 additions and 40 deletions

View File

@ -6,6 +6,7 @@ import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.ResourceBundle.*; import static java.util.ResourceBundle.*;
import static java.util.regex.Pattern.*; import static java.util.regex.Pattern.*;
import static java.util.stream.Collectors.*;
import static net.filebot.similarity.Normalization.*; import static net.filebot.similarity.Normalization.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
@ -24,7 +25,6 @@ import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -155,52 +155,35 @@ public class ReleaseInfo {
} }
// cached patterns // cached patterns
private final Map<Boolean, Pattern[]> stopwords = new HashMap<Boolean, Pattern[]>(2); private final Pattern[][] stopwords = new Pattern[2][];
private final Map<Boolean, Pattern[]> blacklist = new HashMap<Boolean, Pattern[]>(2); private final Pattern[][] blacklist = new Pattern[2][];
public List<String> cleanRelease(Collection<String> items, boolean strict) throws Exception { public List<String> cleanRelease(Collection<String> items, boolean strict) throws Exception {
Pattern[] stopwords; int b = strict ? 1 : 0;
Pattern[] blacklist;
// initialize cached patterns // initialize cached patterns
synchronized (this.stopwords) { if (stopwords[b] == null || blacklist[b] == null) {
stopwords = this.stopwords.get(strict); Set<String> languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault()).keySet();
blacklist = this.blacklist.get(strict); Pattern clutterBracket = getClutterBracketPattern(strict);
Pattern releaseGroup = getReleaseGroupPattern(strict);
Pattern languageSuffix = getLanguageSuffixPattern(languages, strict);
Pattern languageTag = getLanguageTagPattern(languages);
Pattern videoSource = getVideoSourcePattern();
Pattern videoTags = getVideoTagPattern();
Pattern videoFormat = getVideoFormatPattern(strict);
Pattern stereoscopic3d = getStereoscopic3DPattern();
Pattern resolution = getResolutionPattern();
Pattern queryBlacklist = getBlacklistPattern();
if (stopwords == null || blacklist == null) { stopwords[b] = new Pattern[] { languageTag, videoSource, videoTags, videoFormat, resolution, stereoscopic3d, languageSuffix };
Set<String> languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault()).keySet(); blacklist[b] = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoTags, videoFormat, resolution, stereoscopic3d, languageSuffix };
Pattern clutterBracket = getClutterBracketPattern(strict);
Pattern releaseGroup = getReleaseGroupPattern(strict);
Pattern languageSuffix = getLanguageSuffixPattern(languages, strict);
Pattern languageTag = getLanguageTagPattern(languages);
Pattern videoSource = getVideoSourcePattern();
Pattern videoTags = getVideoTagPattern();
Pattern videoFormat = getVideoFormatPattern(strict);
Pattern stereoscopic3d = getStereoscopic3DPattern();
Pattern resolution = getResolutionPattern();
Pattern queryBlacklist = getBlacklistPattern();
stopwords = new Pattern[] { languageTag, videoSource, videoTags, videoFormat, resolution, stereoscopic3d, languageSuffix };
blacklist = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoTags, videoFormat, resolution, stereoscopic3d, languageSuffix };
// cache compiled patterns for common usage
this.stopwords.put(strict, stopwords);
this.blacklist.put(strict, blacklist);
}
} }
List<String> output = new ArrayList<String>(items.size()); return items.stream().map(it -> {
for (String it : items) { it = strict ? clean(it, stopwords[b]) : substringBefore(it, stopwords[b]);
it = strict ? clean(it, stopwords) : substringBefore(it, stopwords); it = normalizePunctuation(clean(it, blacklist[b]));
it = normalizePunctuation(clean(it, blacklist)); return it;
}).filter(s -> s.length() > 0).collect(toList());
// ignore empty values
if (it.length() > 0) {
output.add(it);
}
}
return output;
} }
public String clean(String item, Pattern... blacklisted) { public String clean(String item, Pattern... blacklisted) {