diff --git a/src/simplnx/Filter/IFilter.cpp b/src/simplnx/Filter/IFilter.cpp index 1c8e99ae42..c6daf561e2 100644 --- a/src/simplnx/Filter/IFilter.cpp +++ b/src/simplnx/Filter/IFilter.cpp @@ -2,15 +2,12 @@ #include "simplnx/Filter/DataParameter.hpp" #include "simplnx/Filter/ValueParameter.hpp" +#include "simplnx/Utilities/StringUtilities.hpp" #include #include -#include -#include // For std::numeric_limits #include -#include -#include // For std::pair #include using namespace nx::core; @@ -260,47 +257,6 @@ nlohmann::json IFilter::toJson(const Arguments& args) const return json; } -// Assuming levenshteinDistance function is defined as before -int levenshteinDistance(const std::string& s1, const std::string& s2) -{ - const size_t len1 = s1.size(), len2 = s2.size(); - std::vector> d(len1 + 1, std::vector(len2 + 1)); - - d[0][0] = 0; - for(unsigned int i = 1; i <= len1; ++i) - d[i][0] = i; - for(unsigned int i = 1; i <= len2; ++i) - d[0][i] = i; - - for(unsigned int i = 1; i <= len1; ++i) - for(unsigned int j = 1; j <= len2; ++j) - d[i][j] = std::min({d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1)}); - - return d[len1][len2]; -} - -// Function to find best matching word pairs based on Levenshtein distance -std::vector> findBestMatches(const std::vector& vec1, const std::vector& vec2) -{ - std::vector> bestPairs; - for(const auto& word1 : vec1) - { - int bestDistance = std::numeric_limits::max(); - std::string bestMatch; - for(const auto& word2 : vec2) - { - int currentDistance = levenshteinDistance(word1, word2); - if(currentDistance < bestDistance) - { - bestDistance = currentDistance; - bestMatch = word2; - } - } - bestPairs.emplace_back(word1, bestMatch); - } - return bestPairs; -} - Result IFilter::fromJson(const nlohmann::json& json) const { Parameters params = parameters(); @@ -340,7 +296,7 @@ Result IFilter::fromJson(const nlohmann::json& json) const } } - auto bestMatches = findBestMatches(jsonKeyNotFound, paramKeyNotFound); + auto bestMatches = StringUtilities::FindBestMatches(jsonKeyNotFound, paramKeyNotFound); for(const auto& match : bestMatches) { if(!match.first.empty() && !match.second.empty()) diff --git a/src/simplnx/Utilities/StringUtilities.hpp b/src/simplnx/Utilities/StringUtilities.hpp index d8978ff84b..5a5cc5e8fd 100644 --- a/src/simplnx/Utilities/StringUtilities.hpp +++ b/src/simplnx/Utilities/StringUtilities.hpp @@ -32,12 +32,17 @@ #pragma once #include "simplnx/Common/StringLiteral.hpp" +#include "simplnx/Common/Types.hpp" #include #include +#include #include +#include #include +#include +#include #include /*' '(0x20)space(SPC) @@ -439,4 +444,62 @@ inline std::string toLower(std::string input) return input; } +/** + * @brief Calculates the Levenshtein distance between two strings. + * @param s1 + * @param s2 + * @return uint32 + */ +inline uint32 CalculateLevenshteinDistance(const std::string& s1, const std::string& s2) +{ + usize len1 = s1.size(); + usize len2 = s2.size(); + std::vector> d(len1 + 1, std::vector(len2 + 1)); + + d[0][0] = 0; + for(uint32 i = 1; i <= len1; ++i) + { + d[i][0] = i; + } + for(uint32 i = 1; i <= len2; ++i) + { + d[0][i] = i; + } + + for(uint32 i = 1; i <= len1; ++i) + { + for(uint32 j = 1; j <= len2; ++j) + { + d[i][j] = std::min({d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1)}); + } + } + return d[len1][len2]; +} + +/** + * @brief Finds the best matches between two lists of strings using the Levenshtein distance. + * @param vec1 + * @param vec2 + * @return std::vector> + */ +inline std::vector> FindBestMatches(const std::vector& vec1, const std::vector& vec2) +{ + std::vector> bestPairs; + for(const auto& word1 : vec1) + { + uint32 bestDistance = std::numeric_limits::max(); + std::string bestMatch; + for(const auto& word2 : vec2) + { + uint32 currentDistance = CalculateLevenshteinDistance(word1, word2); + if(currentDistance < bestDistance) + { + bestDistance = currentDistance; + bestMatch = word2; + } + } + bestPairs.emplace_back(word1, bestMatch); + } + return bestPairs; +} } // namespace nx::core::StringUtilities