mlpack
pspectrum_string_kernel_impl.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_CORE_KERNELS_PSPECTRUM_STRING_KERNEL_IMPL_HPP
17 #define MLPACK_CORE_KERNELS_PSPECTRUM_STRING_KERNEL_IMPL_HPP
18 
19 // In case it has not been included yet.
21 
22 namespace mlpack {
23 namespace kernel {
24 
35 template<typename VecType>
36 double PSpectrumStringKernel::Evaluate(const VecType& a,
37  const VecType& b) const
38 {
39  // Get the map of substrings for the two strings we are interested in.
40  const std::map<std::string, int>& aMap = counts[a[0]][a[1]];
41  const std::map<std::string, int>& bMap = counts[b[0]][b[1]];
42 
43  double eval = 0;
44 
45  // Loop through the two maps (which, when iterated through, are sorted
46  // alphabetically).
47  std::map<std::string, int>::const_iterator aIt = aMap.begin();
48  std::map<std::string, int>::const_iterator bIt = bMap.begin();
49 
50  while ((aIt != aMap.end()) && (bIt != bMap.end()))
51  {
52  // Compare alphabetically (this is how std::map is ordered).
53  int result = (*aIt).first.compare((*bIt).first);
54 
55  if (result == 0) // The same substring.
56  {
57  eval += ((*aIt).second * (*bIt).second);
58 
59  // Now increment both.
60  ++aIt;
61  ++bIt;
62  }
63  else if (result > 0)
64  {
65  // aIt is "ahead" of bIt (alphabetically); so increment bIt to "catch up".
66  ++bIt;
67  }
68  else
69  {
70  // bIt is "ahead" of aIt (alphabetically); so increment aIt to "catch up".
71  ++aIt;
72  }
73  }
74 
75  return eval;
76 }
77 } // namespace kernel
78 } // namespace mlpack
79 
80 #endif
double Evaluate(const VecType &a, const VecType &b) const
Evaluate the kernel for the string indices given.
Definition: pspectrum_string_kernel_impl.hpp:36
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1