SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
kernel
string
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
>
13
#include <
shogun/kernel/string/PolyMatchWordStringKernel.h
>
14
#include <
shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h
>
15
#include <
shogun/features/Features.h
>
16
#include <
shogun/features/StringFeatures.h
>
17
18
using namespace
shogun;
19
20
CPolyMatchWordStringKernel::CPolyMatchWordStringKernel
()
21
:
CStringKernel
<uint16_t>()
22
{
23
init
();
24
}
25
26
CPolyMatchWordStringKernel::CPolyMatchWordStringKernel
(int32_t size, int32_t d,
bool
i)
27
:
CStringKernel
<uint16_t>(size)
28
{
29
init
();
30
31
degree
=d;
32
inhomogene
=i;
33
}
34
35
CPolyMatchWordStringKernel::CPolyMatchWordStringKernel
(
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
47
CPolyMatchWordStringKernel::~CPolyMatchWordStringKernel
()
48
{
49
cleanup
();
50
}
51
52
bool
CPolyMatchWordStringKernel::init(
CFeatures
* l,
CFeatures
* r)
53
{
54
CStringKernel<uint16_t>::init
(l,r);
55
return
init_normalizer
();
56
}
57
58
void
CPolyMatchWordStringKernel::cleanup
()
59
{
60
CKernel::cleanup
();
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
;
95
set_normalizer
(
new
CSqrtDiagKernelNormalizer
());
96
97
SG_ADD
(&
degree
,
"degree"
,
"Degree of poly-kernel."
,
MS_AVAILABLE
);
98
SG_ADD
(&
inhomogene
,
"inhomogene"
,
"True for inhomogene poly-kernel."
,
99
MS_NOT_AVAILABLE
);
100
}
SHOGUN
Machine Learning Toolbox - Documentation