2#include <boost/algorithm/string/join.hpp>
9 : reference(reference), similarityFactor(similarityFactor), caseSensitive(caseSensitive) {
15 return distance <= static_cast<double>(std::max(reference.size(),
string.size())) * (1.0 - similarityFactor);
27 std::vector<std::string> result;
28 for (
auto const& dist : distances) {
29 result.push_back(dist.second);
35 uint64_t size = distances.size();
36 std::string result = boost::algorithm::join(
toList(),
", ");
39 }
else if (size == 1) {
40 return "Did you mean '" + result +
"'?";
42 return "Did you mean any of [" + result +
"] ?";
47 std::vector<std::vector<uint64_t>> d(lhs.size() + 1, std::vector<uint64_t>(rhs.size() + 1, 0ull));
48 for (uint64_t row = 1; row < d.size(); ++row) {
51 for (uint64_t col = 1; col < d.front().size(); ++col) {
55 for (uint64_t row = 1; row < d.size(); ++row) {
56 for (uint64_t col = 1; col < d[row].size(); ++col) {
59 if (lhs[row - 1] == rhs[col - 1]) {
63 if (tolower(lhs[row - 1]) == tolower(rhs[col - 1])) {
67 d[row][col] = std::min({d[row - 1][col] + 1, d[row][col - 1] + 1, d[row - 1][col - 1] + cost});
70 return d.back().back();
bool add(std::string const &string)
Adds the given string to the set of similar strings (if it is similar)
std::string toDidYouMeanString() const
Returns a "Did you mean abc?" string.
bool isSimilar(std::string const &string) const
SimilarStrings(std::string reference, double similarityFactor=0.6, bool caseSensitive=true)
Gathers strings that are similar to the given reference string.
std::vector< std::string > toList() const
Gets a list of all added strings that are similar to the reference string.
uint64_t levenshteinDistance(std::string const &lhs, std::string const &rhs, bool caseSensitive)
Levenstein distance to find similar strings.