/** * Return the best score for a title when compared to the search string. It uses 2 passes to find the best match. the first pass uses the matchTitle * as is, and the second pass uses the matchTitle will non search characters removed. * * @param searchTitle * the search title * @param matchTitle * the match title * @return the best out of the 2 scored attempts */ public static float calculateScore(String searchTitle, String matchTitle) { float score1 = Similarity.compareStrings(searchTitle, matchTitle); float score2 = Similarity.compareStrings(searchTitle, removeNonSearchCharacters(matchTitle)); float score3 = 0; if (searchTitle != null && searchTitle.matches(".* \\d{4}$")) { // ends with space+year score3 = Similarity.compareStrings(searchTitle.replaceFirst(" \\d{4}$", ""), matchTitle); } float score = Math.max(score1, Math.max(score3, score2)); LOGGER.debug(String.format("Similarity Score: [%s][%s]=[%s]", searchTitle, matchTitle, score)); return score; }
/** * Word letter pairs. * * @param str * the str * @return an ArrayList of 2-character Strings. */ private static ArrayList<String> wordLetterPairs(String str) { ArrayList<String> allPairs = new ArrayList<>(); // Tokenize the string and put the tokens/words into an array String[] words = str.split("\\s"); // For each word for (int w = 0; w < words.length; w++) { // Find the pairs of characters String[] pairsInWord = letterPairs(words[w]); for (int p = 0; p < pairsInWord.length; p++) { allPairs.add(pairsInWord[p]); } } return allPairs; }
ArrayList<String> pairs1 = wordLetterPairs(str1.toUpperCase(Locale.ROOT)); ArrayList<String> pairs2 = wordLetterPairs(str2.toUpperCase(Locale.ROOT));
for (Entry<String, List<AniDBShow>> entry : showsForLookup.entrySet()) { String title = entry.getKey(); float score = Similarity.compareStrings(title, searchString); if (score > 0.4) { for (AniDBShow show : entry.getValue()) {
for (Info.MovieInfo movieInfo : info.getMovieInfo()) { float score = 0.8f * Similarity.compareStrings(options.getQuery(), movieInfo.movieTitle); SubtitleSearchResult result = new SubtitleSearchResult(providerInfo.getId(), score); result.setId(movieInfo.id);