SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FixedDegreeStringKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
20 void
21 CFixedDegreeStringKernel::init()
22 {
23  SG_ADD(&degree, "degree", "The degree.", MS_AVAILABLE);
25 }
26 
28 : CStringKernel<char>(0), degree(0)
29 {
30  init();
31 }
32 
34 : CStringKernel<char>(size), degree(d)
35 {
36  init();
37 }
38 
41 : CStringKernel<char>(10), degree(d)
42 {
43  init();
44  init(l, r);
45 }
46 
48 {
49  cleanup();
50 }
51 
52 bool CFixedDegreeStringKernel::init(CFeatures* l, CFeatures* r)
53 {
55  return init_normalizer();
56 }
57 
59 {
61 }
62 
63 float64_t CFixedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b)
64 {
65  int32_t alen, blen;
66  bool free_avec, free_bvec;
67 
68  char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
69  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
70 
71  // can only deal with strings of same length
72  ASSERT(alen==blen);
73 
74  int64_t sum = 0;
75  for (int32_t i = 0; i<alen-degree+1; i++)
76  {
77  bool match = true;
78 
79  for (int32_t j = i; j<i+degree && match; j++)
80  match = avec[j]==bvec[j];
81  if (match)
82  sum++;
83  }
84  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
85  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
86 
87  return sum;
88 }

SHOGUN Machine Learning Toolbox - Documentation