Add Levenshtein Difference for movie compare

master
Omertron 13 years ago
parent 1605a71528
commit e7ec580ea2

@ -164,43 +164,60 @@ public class TheMovieDbApi {
WebBrowser.setWebTimeoutRead(read); WebBrowser.setWebTimeoutRead(read);
} }
public static boolean compareMovies(MovieDb moviedb, String title, String year) {
return compareMovies(moviedb, title, year, 0);
}
/** /**
* Compare the MovieDB object with a title & year * Compare the MovieDB object with a title & year
* *
* @param moviedb The moviedb object to compare too * @param moviedb The moviedb object to compare too
* @param title The title of the movie to compare * @param title The title of the movie to compare
* @param year The year of the movie to compare * @param year The year of the movie to compare
* @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match
* @return True if there is a match, False otherwise. * @return True if there is a match, False otherwise.
*/ */
public static boolean compareMovies(MovieDb moviedb, String title, String year) { public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance) {
if ((moviedb == null) || (StringUtils.isBlank(title))) { if ((moviedb == null) || (StringUtils.isBlank(title))) {
return false; return Boolean.FALSE;
} }
if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) { if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) {
// Compare with year // Compare with year
String movieYear = moviedb.getReleaseDate().substring(0, 4); String movieYear = moviedb.getReleaseDate().substring(0, 4);
if (movieYear.equals(year)) { if (movieYear.equals(year)) {
if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) {
return true; return Boolean.TRUE;
} }
if (moviedb.getTitle().equalsIgnoreCase(title)) { if (compareDistance(moviedb.getTitle(), title, maxDistance)) {
return true; return Boolean.TRUE;
} }
} }
} }
// Compare without year // Compare without year
if (moviedb.getOriginalTitle().equalsIgnoreCase(title)) { if (compareDistance(moviedb.getOriginalTitle(), title, maxDistance)) {
return true; return Boolean.TRUE;
}
if (compareDistance(moviedb.getTitle(), title, maxDistance)) {
return Boolean.TRUE;
} }
if (moviedb.getTitle().equalsIgnoreCase(title)) { return Boolean.FALSE;
return true;
} }
return false; /**
* Compare the Levenshtein Distance between the two strings
*
* @param title1
* @param title2
* @param distance
* @return
*/
private static boolean compareDistance(String title1, String title2, int distance) {
return (StringUtils.getLevenshteinDistance(title1, title2) <= distance);
} }
/** /**

Loading…
Cancel
Save