SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
FixedDegreeStringKernel.cpp
浏览该文件的文档.
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>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
20 void
21 CFixedDegreeStringKernel::init()
22 {
23  SG_ADD(&degree, "degree", "The degree.", MS_AVAILABLE);
25 }
26 
28 : CStringKernel<char>(0), degree(0)
29 {
30  init();
31 }
32 
34 : CStringKernel<char>(size), degree(d)
35 {
36  init();
37 }
38 
41 : CStringKernel<char>(10), degree(d)
42 {
43  init();
44  init(l, r);
45 }
46 
48 {
49  cleanup();
50 }
51 
52 bool CFixedDegreeStringKernel::init(CFeatures* l, CFeatures* r)
53 {
55  return init_normalizer();
56 }
57 
59 {
61 }
62 
63 float64_t CFixedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b)
64 {
65  int32_t alen, blen;
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  // can only deal with strings of same length
72  ASSERT(alen==blen)
73 
74  int64_t sum = 0;
75  for (int32_t i = 0; i<alen-degree+1; i++)
76  {
77  bool match = true;
78 
79  for (int32_t j = i; j<i+degree && match; j++)
80  match = avec[j]==bvec[j];
81  if (match)
82  sum++;
83  }
84  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
85  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
86 
87  return sum;
88 }
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
float64_t compute(int32_t idx_a, int32_t idx_b)
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
#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

SHOGUN 机器学习工具包 - 项目文档