SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PolyMatchWordStringKernel.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<uint16_t>()
22 {
23  init();
24 }
25 
27 : CStringKernel<uint16_t>(size)
28 {
29  init();
30 
31  degree=d;
32  inhomogene=i;
33 }
34 
36  CStringFeatures<uint16_t>* l, CStringFeatures<uint16_t>* r, int32_t d, bool i)
37 : CStringKernel<uint16_t>()
38 {
39  init();
40 
41  degree=d;
42  inhomogene=i;
43 
44  init(l, r);
45 }
46 
48 {
49  cleanup();
50 }
51 
52 bool CPolyMatchWordStringKernel::init(CFeatures* l, CFeatures* r)
53 {
55  return init_normalizer();
56 }
57 
59 {
61 }
62 
63 float64_t CPolyMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b)
64 {
65  int32_t alen, blen;
66  bool free_avec, free_bvec;
67 
68  uint16_t* avec=((CStringFeatures<uint16_t>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
69  uint16_t* bvec=((CStringFeatures<uint16_t>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
70 
71  ASSERT(alen==blen)
72 
73  int32_t sum=0;
74 
75  for (int32_t i=0; i<alen; i++)
76  sum+= (avec[i]==bvec[i]) ? 1 : 0;
77 
78  if (inhomogene)
79  sum+=1;
80 
81  float64_t result=sum;
82 
83  for (int32_t j=1; j<degree; j++)
84  result*=sum;
85 
86  ((CStringFeatures<uint16_t>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
87  ((CStringFeatures<uint16_t>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
88  return result;
89 }
90 
91 void CPolyMatchWordStringKernel::init()
92 {
93  degree=0;
94  inhomogene=false;
96 
97  SG_ADD(&degree, "degree", "Degree of poly-kernel.", MS_AVAILABLE);
98  SG_ADD(&inhomogene, "inhomogene", "True for inhomogene poly-kernel.",
100 }
virtual void cleanup()
Definition: Kernel.cpp:173
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:150
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init_normalizer()
Definition: Kernel.cpp:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
#define SG_ADD(...)
Definition: SGObject.h:81
friend class CSqrtDiagKernelNormalizer
Definition: Kernel.h:161
Template class StringKernel, is the base class of all String Kernels.
Definition: StringKernel.h:26
virtual bool init(CFeatures *l, CFeatures *r)

SHOGUN Machine Learning Toolbox - Documentation