From 151c43028d8b60cc43a1793ca23d678a81aa6328 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 29 Nov 2011 08:56:29 +0000 Subject: [PATCH] * improved search result auto-selection --- source/net/sourceforge/filebot/Main.java | 2 +- .../filebot/cli/CmdlineOperations.java | 45 +++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index f9b3b3d0..473cafcb 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -63,7 +63,7 @@ public class Main { if (args.clearUserData()) { // clear preferences and cache - System.out.println("Reset preferences and clear cache."); + System.out.println("Reset preferences and clear cache"); Settings.forPackage(Main.class).clear(); CacheManager.getInstance().clearAll(); } diff --git a/source/net/sourceforge/filebot/cli/CmdlineOperations.java b/source/net/sourceforge/filebot/cli/CmdlineOperations.java index 7e53b5d7..e86a04fd 100644 --- a/source/net/sourceforge/filebot/cli/CmdlineOperations.java +++ b/source/net/sourceforge/filebot/cli/CmdlineOperations.java @@ -20,6 +20,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -198,13 +199,16 @@ public class CmdlineOperations implements CmdlineInterface { // select search result if (results.size() > 0) { - SearchResult selectedSearchResult = selectSearchResult(query, results, strict); + List selectedSearchResults = selectSearchResult(query, results, strict); - if (selectedSearchResult != null) { - CLILogger.fine(format("Fetching episode data for [%s]", selectedSearchResult.getName())); - List episodes = db.getEpisodeList(selectedSearchResult, locale); + if (selectedSearchResults != null) { + List episodes = new ArrayList(); + for (SearchResult it : selectedSearchResults) { + CLILogger.fine(format("Fetching episode data for [%s]", it.getName())); + episodes.addAll(db.getEpisodeList(it, locale)); + Analytics.trackEvent(db.getName(), "FetchEpisodeList", it.getName()); + } - Analytics.trackEvent(db.getName(), "FetchEpisodeList", selectedSearchResult.getName()); return episodes; } } @@ -258,7 +262,7 @@ public class CmdlineOperations implements CmdlineInterface { // use user query if search by hash did not return any results, only one query for one movie though if (query != null && movieDescriptors.length == 1 && movieDescriptors[0] == null) { CLILogger.fine(format("Looking up movie by query [%s]", query)); - movieDescriptors[0] = (Movie) selectSearchResult(query, db.searchMovie(query, locale), strict); + movieDescriptors[0] = (Movie) selectSearchResult(query, db.searchMovie(query, locale), strict).get(0); } // map old files to new paths by applying formatting and validating filenames @@ -563,35 +567,48 @@ public class CmdlineOperations implements CmdlineInterface { } - private Collection findProbableMatches(String query, Iterable searchResults) { + private List findProbableMatches(final String query, Iterable searchResults) { // auto-select most probable search result Map probableMatches = new TreeMap(String.CASE_INSENSITIVE_ORDER); // use name similarity metric - SimilarityMetric metric = new NameSimilarityMetric(); + final SimilarityMetric metric = new NameSimilarityMetric(); // find probable matches using name similarity > 0.9 for (SearchResult result : searchResults) { - if (metric.getSimilarity(query, result.getName()) > 0.9) { + float f = metric.getSimilarity(query, result.getName()); + if (f >= 0.9 || (f >= 0.6 && result.getName().toLowerCase().startsWith(query.toLowerCase()))) { if (!probableMatches.containsKey(result.toString())) { probableMatches.put(result.toString(), result); } } } - return probableMatches.values(); + // sort results by similarity to query + List results = new ArrayList(probableMatches.values()); + sort(results, new Comparator() { + + @Override + public int compare(SearchResult o1, SearchResult o2) { + float f1 = metric.getSimilarity(o1.getName(), query); + float f2 = metric.getSimilarity(o2.getName(), query); + return f1 > f2 ? f1 == f2 ? 0 : -1 : 1; + } + }); + + return results; } - private SearchResult selectSearchResult(String query, Iterable searchResults, boolean strict) throws Exception { - Collection probableMatches = findProbableMatches(query, searchResults); + private List selectSearchResult(String query, Iterable searchResults, boolean strict) throws Exception { + List probableMatches = findProbableMatches(query, searchResults); if (probableMatches.isEmpty() || (strict && probableMatches.size() != 1)) { throw new Exception("Failed to auto-select search result: " + probableMatches); } // return first and only value - return probableMatches.iterator().next(); + return probableMatches; } @@ -773,7 +790,7 @@ public class CmdlineOperations implements CmdlineInterface { EpisodeListProvider service = (db == null) ? TVRage : getEpisodeListProvider(db); Locale locale = getLanguage(languageName).toLocale(); - SearchResult hit = selectSearchResult(query, service.search(query, locale), false); + SearchResult hit = selectSearchResult(query, service.search(query, locale), false).get(0); List episodes = new ArrayList(); for (Episode it : service.getEpisodeList(hit, locale)) {