32 constexpr int scoreMatch = 1;
33 constexpr int scoreMismatch = -1;
34 constexpr int scoreGap = -1;
37 std::vector<std::vector<int>> scoreMatrix(s1.length() + 1, std::vector<int>(s2.length() + 1, 0));
40 for (
size_t i = 0; i < scoreMatrix.size(); ++i) {
41 scoreMatrix[i][0] = i * scoreGap;
43 for (
size_t j = 0; j < scoreMatrix[0].size(); ++j) {
44 scoreMatrix[0][j] = j * scoreGap;
48 for (
size_t i = 1; i < scoreMatrix.size(); ++i) {
49 for (
size_t j = 1; j < scoreMatrix[0].size(); ++j) {
50 auto matchValue = s1[i - 1] == s2[j - 1] ? scoreMatch : scoreMismatch;
51 auto scoreDiagonal = scoreMatrix[i - 1][j - 1] + matchValue;
52 auto scoreLeft = scoreMatrix[i - 1][j] + scoreGap;
53 auto scoreTop = scoreMatrix[i][j - 1] + scoreGap;
55 std::array<
decltype(scoreDiagonal), 3> scores = {scoreDiagonal, scoreLeft, scoreTop};
56 auto scoreMax = std::max_element(scores.begin(), scores.end());
58 scoreMatrix[i][j] = *scoreMax;
64 return scoreMatrix[scoreMatrix.size() - 1][scoreMatrix[scoreMatrix.size() - 1].size() - 1];
77inline std::string
matchStrings(
const std::vector<std::string> &haystack, std::string needle) {
78 std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
79 auto bestDistance = std::numeric_limits<int>::min();
80 std::vector<std::string> matchedStrings;
81 for (
auto &s : haystack) {
82 auto distance = needlemanWunschScore(needle, s);
84 if (distance > bestDistance) {
85 matchedStrings.clear();
86 bestDistance = distance;
89 if (distance >= bestDistance) {
90 matchedStrings.push_back(s);
93 if (matchedStrings.size() > 1) {
95 [](
auto arr) -> std::string {
96 std::ostringstream ss;
105 return matchedStrings[0];
141inline std::vector<std::string>
tokenize(
const std::string &searchString,
const std::string &
delimiters) {
142 std::vector<std::string> wordVector;
144 std::size_t prev = 0, pos;
145 while ((pos = searchString.find_first_of(
delimiters, prev)) != std::string::npos) {
146 if (pos > prev) wordVector.push_back(searchString.substr(prev, pos - prev));
149 if (prev < searchString.length()) wordVector.push_back(searchString.substr(prev, std::string::npos));
164 std::array<double, 3> parsedArray{};
166 if (strings.size() > 3) {
169 for (
int i = 0; i < 3; i++) {
171 parsedArray[i] = std::stod(strings[i]);
172 }
catch (
const std::exception &e) {
188 if (booleanOption ==
"on" or booleanOption ==
"true" or booleanOption ==
"enabled") {
190 }
else if (booleanOption ==
"off" or booleanOption ==
"false" or booleanOption ==
"disabled") {
205 std::set<double> doubles;
210 for (
auto number = std::sregex_iterator(doubleString.begin(), doubleString.end(), regexDouble);
211 number != std::sregex_iterator(); ++number) {
213 double value = stod(number->str());
214 doubles.insert(value);
215 }
catch (
const std::exception &) {
233inline std::unique_ptr<autopas::NumberSet<double>>
parseNumberSet(
const std::string &setString) {
235 std::regex regexInterval(
"("
246 if (std::regex_match(setString, matches, regexInterval)) {
249 double min = stod(matches.str(1));
250 double max = stod(matches.str(2));
251 return std::make_unique<autopas::NumberInterval<double>>(min, max);
252 }
catch (
const std::exception &) {
258 return std::make_unique<autopas::NumberSetFinite<double>>(values);
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
int needlemanWunschScore(std::string s1, std::string s2)
Calculates a similarity score of s1 and s2 based on the Needleman-Wunsch string alignment algorithm.
Definition: StringUtils.h:30
Some functions to parse enums from (input-) strings.
Definition: namespaces.h:64
constexpr char delimitersRgxInv[]
Regex for all but delimiters to split input strings as regex.
Definition: StringUtils.h:119
bool parseBoolOption(const std::string &booleanOption)
Converts a string to bool.
Definition: StringUtils.h:187
std::string matchStrings(const std::vector< std::string > &haystack, std::string needle)
Finds best match of needle in haystack.
Definition: StringUtils.h:77
constexpr char delimitersRgx[]
Regex for all delimiters to split input strings.
Definition: StringUtils.h:115
std::unique_ptr< autopas::NumberSet< double > > parseNumberSet(const std::string &setString)
Converts a string to a NumberSet<double>.
Definition: StringUtils.h:233
constexpr char delimiters[]
All accepted delimiters to split input strings.
Definition: StringUtils.h:111
std::array< double, 3 > parseArrayD3(const std::string &string)
Converts a string to std::array<double,3>.
Definition: StringUtils.h:163
std::vector< std::string > tokenize(const std::string &searchString, const std::string &delimiters)
Splits a string by multiple delimiters.
Definition: StringUtils.h:141
static const std::string regexDoubleStr
Regex for a double e.g.
Definition: StringUtils.h:124
std::set< double > parseDoubles(const std::string &doubleString)
Converts a string to a set of doubles.
Definition: StringUtils.h:204