SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PolyMatchStringKernel.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>
12 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 : CStringKernel<char>()
22 {
23  init();
24 }
25 
26 CPolyMatchStringKernel::CPolyMatchStringKernel(int32_t size, int32_t d, bool i)
27 : CStringKernel<char>(size)
28 {
29  init();
30 
31  degree=d;
32  inhomogene=i;
33 }
34 
36  CStringFeatures<char>* l, CStringFeatures<char>* r, int32_t d, bool i)
37 : CStringKernel<char>(10)
38 {
39  init();
40 
41  degree=d;
42  inhomogene=i;
43 
44  init(l, r);
45 }
46 
48 {
49  cleanup();
50 }
51 
52 bool CPolyMatchStringKernel::init(CFeatures* l, CFeatures* r)
53 {
55  return init_normalizer();
56 }
57 
59 {
61 }
62 
63 float64_t CPolyMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
64 {
65  int32_t i, alen, blen, sum;
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  ASSERT(alen==blen);
72  for (i = 0, sum = inhomogene; i<alen; i++)
73  {
74  if (avec[i]==bvec[i])
75  sum++;
76  }
77  float64_t result = ((float64_t) sum);
78 
79  if (rescaling)
80  result/=alen;
81 
82  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
83  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
84  return CMath::pow(result , degree);
85 }
86 
87 void CPolyMatchStringKernel::init()
88 {
89  degree=0;
90  inhomogene=false;
91  rescaling=false;
93 
94  SG_ADD(&degree, "degree", "Degree of poly-kernel.", MS_AVAILABLE);
95  SG_ADD(&inhomogene, "inhomogene", "True for inhomogene poly-kernel.",
97  SG_ADD(&rescaling, "rescaling",
98  "True to rescale kernel with string length.", MS_AVAILABLE);
99 }

SHOGUN Machine Learning Toolbox - Documentation