|
29 | 29 | using namespace std;
|
30 | 30 | using namespace dev;
|
31 | 31 |
|
32 |
| -namespace dev |
| 32 | +bool dev::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance) |
33 | 33 | {
|
34 |
| - |
35 |
| -bool stringWithinDistance(string const& _name1, string const& _name2, size_t _maxDistance) |
36 |
| -{ |
37 |
| - if (_name1 == _name2) |
| 34 | + if (_str1 == _str2) |
38 | 35 | return true;
|
39 | 36 |
|
40 |
| - size_t n1 = _name1.size(), n2 = _name2.size(); |
41 |
| - vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1)); |
| 37 | + size_t n1 = _str1.size(), n2 = _str2.size(); |
| 38 | + size_t distance = stringDistance(_str1, _str2); |
| 39 | + |
| 40 | + // if distance is not greater than _maxDistance, and distance is strictly less than length of both names, they can be considered similar |
| 41 | + // this is to avoid irrelevant suggestions |
| 42 | + return distance <= _maxDistance && distance < n1 && distance < n2; |
| 43 | +} |
| 44 | + |
| 45 | +size_t dev::stringDistance(string const& _str1, string const& _str2) |
| 46 | +{ |
| 47 | + size_t n1 = _str1.size(), n2 = _str2.size(); |
| 48 | + // Optimize by storing only last 2 rows and current row. So first index is considered modulo 3 |
| 49 | + vector<vector<size_t>> dp(3, vector<size_t>(n2 + 1)); |
42 | 50 |
|
43 | 51 | // In this dp formulation of Damerau–Levenshtein distance we are assuming that the strings are 1-based to make base case storage easier.
|
44 | 52 | // So index accesser to _name1 and _name2 have to be adjusted accordingly
|
45 | 53 | for (size_t i1 = 0; i1 <= n1; ++i1)
|
46 | 54 | {
|
47 | 55 | for (size_t i2 = 0; i2 <= n2; ++i2)
|
48 | 56 | {
|
49 |
| - if (min(i1, i2) == 0) |
50 |
| - // Base case |
51 |
| - dp[i1][i2] = max(i1, i2); |
52 |
| - else |
53 |
| - { |
54 |
| - dp[i1][i2] = min(dp[i1 - 1][i2] + 1, dp[i1][i2 - 1] + 1); |
55 |
| - // Deletion and insertion |
56 |
| - if (_name1[i1 - 1] == _name2[i2 - 1]) |
57 |
| - // Same chars, can skip |
58 |
| - dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1]); |
| 57 | + if (min(i1, i2) == 0) // base case |
| 58 | + dp[i1 % 3][i2] = max(i1, i2); |
59 | 59 | else
|
60 |
| - // Different chars so try substitution |
61 |
| - dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1] + 1); |
| 60 | + { |
| 61 | + dp[i1 % 3][i2] = min(dp[(i1-1) % 3][i2] + 1, dp[i1 % 3][i2-1] + 1); // deletion and insertion |
| 62 | + if (_str1[i1-1] == _str2[i2-1]) // same chars, can skip |
| 63 | + dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][i2-1]); |
| 64 | + else // different chars so try substitution |
| 65 | + dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][i2-1] + 1); |
62 | 66 |
|
63 |
| - if (i1 > 1 && i2 > 1 && _name1[i1 - 1] == _name2[i2 - 2] && _name1[i1 - 2] == _name2[i2 - 1]) |
64 |
| - // Try transposing |
65 |
| - dp[i1][i2] = min(dp[i1][i2], dp[i1 - 2][i2 - 2] + 1); |
66 |
| - } |
| 67 | + if (i1 > 1 && i2 > 1 && _str1[i1-1] == _str2[i2-2] && _str1[i1-2] == _str2[i2-1]) // Try transposing |
| 68 | + dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-2) % 3][i2-2] + 1); |
| 69 | + } |
67 | 70 | }
|
68 | 71 | }
|
69 | 72 |
|
70 |
| - size_t distance = dp[n1][n2]; |
71 |
| - |
72 |
| - // if distance is not greater than _maxDistance, and distance is strictly less than length of both names, |
73 |
| - // they can be considered similar this is to avoid irrelevant suggestions |
74 |
| - return distance <= _maxDistance && distance < n1 && distance < n2; |
| 73 | + return dp[n1 % 3][n2]; |
75 | 74 | }
|
76 | 75 |
|
| 76 | +string dev::quotedAlternativesList(vector<string> const& suggestions) { |
| 77 | + if (suggestions.empty()) |
| 78 | + return ""; |
| 79 | + if (suggestions.size() == 1) |
| 80 | + return "\"" + suggestions.front() + "\""; |
| 81 | + |
| 82 | + string choices = "\"" + suggestions.front() + "\""; |
| 83 | + for (size_t i = 1; i + 1 < suggestions.size(); ++i) |
| 84 | + choices += ", \"" + suggestions[i] + "\""; |
| 85 | + |
| 86 | + choices += " or \"" + suggestions.back() + "\""; |
| 87 | + |
| 88 | + return choices; |
77 | 89 | }
|
| 90 | + |
0 commit comments