Calculates the similarity (a number within 0 and 1) between two strings. https://stackoverflow.com/a/16018452/9187189
(String s1, String s2)
| 122 | * https://stackoverflow.com/a/16018452/9187189 |
| 123 | */ |
| 124 | public static double similarity(String s1, String s2) { |
| 125 | String longer = s1, shorter = s2; |
| 126 | if (s1.length() < s2.length()) { // longer should always have greater length |
| 127 | longer = s2; |
| 128 | shorter = s1; |
| 129 | } |
| 130 | int longerLength = longer.length(); |
| 131 | if (longerLength == 0) { |
| 132 | return 1.0; /* both strings are zero length */ |
| 133 | } |
| 134 | return (longerLength - getLevenshteinDistance(longer, shorter)) / (double) longerLength; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * java.org.apache.commons.lang3.StringUtils#getLevenshteinDistance(CharSequence, CharSequence) |
no test coverage detected