SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MatchWordStringKernel.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>
13 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 {
22  init();
23 }
24 
26 : CStringKernel<uint16_t>(size)
27 {
28  init();
29  degree=d;
30 }
31 
34 : CStringKernel<uint16_t>()
35 {
36  init();
37  degree=d;
38  init(l, r);
39 }
40 
42 {
43  cleanup();
44 }
45 
46 bool CMatchWordStringKernel::init(CFeatures* l, CFeatures* r)
47 {
49  return init_normalizer();
50 }
51 
52 float64_t CMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b)
53 {
54  int32_t alen, blen;
55  bool free_avec, free_bvec;
56 
57  uint16_t* avec=((CStringFeatures<uint16_t>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
58  uint16_t* bvec=((CStringFeatures<uint16_t>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
59  // can only deal with strings of same length
60  ASSERT(alen==blen)
61 
62  float64_t sum=0;
63  for (int32_t i=0; i<alen; i++)
64  sum+= (avec[i]==bvec[i]) ? 1 : 0;
65 
66  ((CStringFeatures<uint16_t>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
67  ((CStringFeatures<uint16_t>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
68 
69  return CMath::pow(sum, degree);
70 }
71 
72 void CMatchWordStringKernel::init()
73 {
74  degree=0;
76  SG_ADD(&degree, "degree", "Degree of poly kernel", MS_AVAILABLE);
77 }
virtual void cleanup()
Definition: Kernel.cpp:173
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
friend class CAvgDiagKernelNormalizer
Definition: Kernel.h:163
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
virtual bool init(CFeatures *l, CFeatures *r)
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
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:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_ADD(...)
Definition: SGObject.h:84
Template class StringKernel, is the base class of all String Kernels.
Definition: StringKernel.h:26
static int32_t pow(bool x, int32_t n)
Definition: Math.h:535

SHOGUN Machine Learning Toolbox - Documentation