3#include <boost/algorithm/string/join.hpp>
4#include <boost/algorithm/string/split.hpp>
5#include <boost/algorithm/string/trim.hpp>
12 : reference(reference), similarityFactor(similarityFactor), caseSensitive(caseSensitive) {
18 return distance <= static_cast<double>(std::max(reference.size(),
string.size())) * (1.0 - similarityFactor);
30 std::vector<std::string> result;
31 for (
auto const& dist : distances) {
32 result.push_back(dist.second);
38 uint64_t size = distances.size();
39 std::string result = boost::algorithm::join(
toList(),
", ");
42 }
else if (size == 1) {
43 return "Did you mean '" + result +
"'?";
45 return "Did you mean any of [" + result +
"] ?";
50 std::vector<std::vector<uint64_t>> d(lhs.size() + 1, std::vector<uint64_t>(rhs.size() + 1, 0ull));
51 for (uint64_t row = 1; row < d.size(); ++row) {
54 for (uint64_t col = 1; col < d.front().size(); ++col) {
58 for (uint64_t row = 1; row < d.size(); ++row) {
59 for (uint64_t col = 1; col < d[row].size(); ++col) {
62 if (lhs[row - 1] == rhs[col - 1]) {
66 if (tolower(lhs[row - 1]) == tolower(rhs[col - 1])) {
70 d[row][col] = std::min({d[row - 1][col] + 1, d[row][col - 1] + 1, d[row - 1][col - 1] + cost});
73 return d.back().back();
77 std::vector<std::string> result;
79 boost::split(result, input, boost::is_any_of(
","));
80 for (
auto& entry : result) {
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.
std::vector< std::string > parseCommaSeparatedStrings(std::string const &input)
Splits the input string on commas and trims whitespace from each element.