SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation