startsWithSimilarity method
Compares the starting charcters of the String with that of other
,
limiting the comparison to a substring of this or other
that is
the shorter of this.length or other.length.
The two strings are converted to lower-case and trimmed for the comparison.
- returns 1.0 if the two strings are the same;
- returns 0.0 if the two strings do not start with the same character;
- returns 0.0 if either of the strings are empty, unless both are empty (equal);
- returns the edit distance between the starting characters in all other cases.
Not case sensitive.
Implementation
double startsWithSimilarity(String other) {
final term = toLowerCase().trim();
other = other.toLowerCase().trim();
if (term == other) {
return 1.0;
}
if (term.isEmpty ||
other.isEmpty ||
!other.startsWith(term.substring(0, 1))) {
return 0.0;
}
final startsWithLength = term.length > other.length ? other.length : length;
return term
.substring(0, startsWithLength)
.editSimilarity(other.substring(0, startsWithLength));
}