SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianMatchStringKernel.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>(0), width(0.0)
22 {
25 }
26 
28 : CStringKernel<char>(size), width(w)
29 {
32 }
33 
36 : CStringKernel<char>(10), width(w)
37 {
39  init(l, r);
41 }
42 
44 {
45  cleanup();
46 }
47 
48 bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r)
49 {
51  return init_normalizer();
52 }
53 
55 {
57 }
58 
59 float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
60 {
61  int32_t i, alen, blen ;
62  bool free_avec, free_bvec;
63 
64  char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
65  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
66 
67  float64_t result=0;
68 
69  ASSERT(alen==blen)
70 
71  for (i = 0; i<alen; i++)
72  result+=(avec[i]==bvec[i]) ? 0:4;
73 
74  result=exp(-result/width);
75 
76 
77  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
78  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
79  return result;
80 }
81 
83 {
84  SG_ADD(&width, "width", "kernel width", MS_AVAILABLE);
85 }

SHOGUN Machine Learning Toolbox - Documentation