EventAnalysis  1.3.0
StringUtils.hpp
Go to the documentation of this file.
1 /*
2  * StringUtils.hpp
3  *
4  * Created on: Sep 15, 2020
5  * Author: Nicola Mori
6  */
7 
10 #ifndef STRINGUTILS_HPP_
11 #define STRINGUTILS_HPP_
12 
13 #include "core/Traits.h"
15 
16 namespace EA {
17 
18 namespace StringUtils {
19 
20 template <typename Container>
21 std::enable_if_t<!(EA::is_pointer<typename Container::value_type>::value), std::string>
22 FindSimilar(const std::string &str, const Container &cont) {
23  std::size_t minDist = std::numeric_limits<std::size_t>::max();
24  typename Container::const_iterator mostSimilarIter;
25  for (auto stringIter = cont.begin(); stringIter != cont.end(); ++stringIter) {
26  std::size_t dist = levenshteinSSE::levenshtein(str, *stringIter);
27  if (dist < minDist) {
28  mostSimilarIter = stringIter;
29  if (dist == 0) {
30  break;
31  } else {
32  minDist = dist;
33  }
34  }
35  }
36  return *mostSimilarIter;
37 }
38 
39 template <typename Container>
40 std::enable_if_t<EA::is_pointer<typename Container::value_type>::value, std::string>
41 FindSimilar(const std::string &str, const Container &cont) {
42  std::size_t minDist = std::numeric_limits<std::size_t>::max();
43  typename Container::const_iterator mostSimilarIter;
44  for (auto stringIter = cont.begin(); stringIter != cont.end(); ++stringIter) {
45  std::size_t dist = levenshteinSSE::levenshtein(str, *(*stringIter));
46  if (dist < minDist) {
47  mostSimilarIter = stringIter;
48  if (dist == 0) {
49  break;
50  } else {
51  minDist = dist;
52  }
53  }
54  }
55  return *(*mostSimilarIter);
56 }
57 
58 } // namespace StringUtils
59 
60 } // namespace EA
61 
62 #endif /* STRINGUTILS_HPP_ */
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::size_t levenshtein(Iterator1 a, Iterator1 aEnd, Iterator2 b, Iterator2 bEnd)
Definition: levenshtein-sse.hpp:984
std::enable_if_t<!(is_pointer< typename Container::value_type >::value), std::string > FindSimilar(const std::string &str, const Container &cont)
Find a similar string inside a container.
Definition: StringUtils.hpp:22