10 template <
typename ValueT>
11 float normalizedAbsDistanceSimilarity(ValueT oldValue, ValueT newValue)
13 const double oldAsDouble =
static_cast<double>(oldValue);
14 const double newAsDouble =
static_cast<double>(newValue);
16 std::max(std::max(std::abs(oldAsDouble), std::abs(newAsDouble)), 0.0001);
18 return static_cast<float>(1.0 - (std::abs(oldAsDouble - newAsDouble) / denom));
22 int levenshteinDist(
const std::string& word1,
const std::string& word2) {
23 const int size1 = word1.size();
24 const int size2 = word2.size();
25 std::vector<std::vector<int>> verif(size1 + 1, std::vector<int>(size2 + 1));
34 for (
int i = 0; i <= size1; i++)
36 for (
int j = 0; j <= size2; j++)
40 for (
int i = 1; i <= size1; i++) {
41 for (
int j = 1; j <= size2; j++) {
44 int cost = (word2[j - 1] == word1[i - 1]) ? 0 : 1;
50 verif[i][j] = std::min(
51 std::min(verif[i - 1][j] + 1, verif[i][j - 1] + 1),
52 verif[i - 1][j - 1] + cost
58 return verif[size1][size2];
71 return normalizedAbsDistanceSimilarity(oldValue.
getValue(), newValue.
getValue());
77 return normalizedAbsDistanceSimilarity(oldValue.
getValue(), newValue.
getValue());
83 return normalizedAbsDistanceSimilarity(oldValue.
getValue(), newValue.
getValue());
89 return normalizedAbsDistanceSimilarity(oldValue.
getValue(), newValue.
getValue());
95 const std::string& oldString = oldValue.
getValue();
96 const std::string& newString = newValue.
getValue();
98 std::max(
static_cast<float>(std::max(oldString.size(), newString.size())), 0.0001f);
100 return 1.0f - (
static_cast<float>(levenshteinDist(oldString, newString)) / denom);