lengthSimilarity method
Returns the similarity in length between two terms, defined as:
lengthSimilarity = 1 minus the log of the ratio between the term lengths,
with a floor at 0.0:
1-(log(this.length/other.length))
Returns:
- 1.0 if this and
other
are the same length; and - 0.0 if the ratio between term lengths is more than 10 or less than 0.1.
The String and other
are converted to lower-case and trimmed for the
comparison.
Implementation
double lengthSimilarity(String other) {
final term = trim().toLowerCase();
other = other.trim().toLowerCase();
final logRatio = term.isEmpty
? other.isEmpty
? 0
: 1
: other.isEmpty
? 1
: (log(other.length / term.length)).abs();
final similarity = logRatio > 1 ? 0.0 : 1.0 - logRatio;
if (similarity > 1.0) {
return 1.0;
}
return similarity;
}