Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4#include <boost/algorithm/string/split.hpp>
5#include <boost/algorithm/string/trim.hpp>
6
7namespace storm {
8namespace utility {
9namespace string {
10
11SimilarStrings::SimilarStrings(std::string reference, double similarityFactor, bool caseSensitive)
12 : reference(reference), similarityFactor(similarityFactor), caseSensitive(caseSensitive) {
13 // intentionally left empty.
14}
15
16bool SimilarStrings::isSimilar(std::string const& string) const {
17 double distance = levenshteinDistance(reference, string, caseSensitive);
18 return distance <= static_cast<double>(std::max(reference.size(), string.size())) * (1.0 - similarityFactor);
19}
20
21bool SimilarStrings::add(std::string const& string) {
22 if (isSimilar(string)) {
23 distances.emplace(storm::utility::string::levenshteinDistance(reference, string, caseSensitive), string);
24 return true;
25 }
26 return false;
27}
28
29std::vector<std::string> SimilarStrings::toList() const {
30 std::vector<std::string> result;
31 for (auto const& dist : distances) {
32 result.push_back(dist.second);
33 }
34 return result;
35}
36
38 uint64_t size = distances.size();
39 std::string result = boost::algorithm::join(toList(), ", ");
40 if (size == 0) {
41 return "";
42 } else if (size == 1) {
43 return "Did you mean '" + result + "'?";
44 } else {
45 return "Did you mean any of [" + result + "] ?";
46 }
47}
48
49uint64_t levenshteinDistance(std::string const& lhs, std::string const& rhs, bool caseSensitive) {
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) {
52 d[row].front() = row;
53 }
54 for (uint64_t col = 1; col < d.front().size(); ++col) {
55 d.front()[col] = col;
56 }
57
58 for (uint64_t row = 1; row < d.size(); ++row) {
59 for (uint64_t col = 1; col < d[row].size(); ++col) {
60 uint64_t cost = 1;
61 if (caseSensitive) {
62 if (lhs[row - 1] == rhs[col - 1]) {
63 cost = 0;
64 }
65 } else {
66 if (tolower(lhs[row - 1]) == tolower(rhs[col - 1])) {
67 cost = 0;
68 }
69 }
70 d[row][col] = std::min({d[row - 1][col] + 1, d[row][col - 1] + 1, d[row - 1][col - 1] + cost});
71 }
72 }
73 return d.back().back();
74}
75
76std::vector<std::string> parseCommaSeparatedStrings(std::string const& input) {
77 std::vector<std::string> result;
78 if (!input.empty()) {
79 boost::split(result, input, boost::is_any_of(","));
80 for (auto& entry : result) {
81 boost::trim(entry);
82 }
83 }
84 return result;
85}
86} // namespace string
87} // namespace utility
88} // namespace storm
bool add(std::string const &string)
Adds the given string to the set of similar strings (if it is similar).
Definition string.cpp:21
std::string toDidYouMeanString() const
Returns a "Did you mean abc?" string.
Definition string.cpp:37
bool isSimilar(std::string const &string) const
Definition string.cpp:16
SimilarStrings(std::string reference, double similarityFactor=0.6, bool caseSensitive=true)
Gathers strings that are similar to the given reference string.
Definition string.cpp:11
std::vector< std::string > toList() const
Gets a list of all added strings that are similar to the reference string.
Definition string.cpp:29
uint64_t levenshteinDistance(std::string const &lhs, std::string const &rhs, bool caseSensitive)
Levenstein distance to find similar strings.
Definition string.cpp:49
std::vector< std::string > parseCommaSeparatedStrings(std::string const &input)
Splits the input string on commas and trims whitespace from each element.
Definition string.cpp:76