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