SHOGUN
v2.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
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
>
12
#include <
shogun/mathematics/Math.h
>
13
#include <
shogun/io/SGIO.h
>
14
#include <
shogun/kernel/string/MatchWordStringKernel.h
>
15
#include <
shogun/kernel/normalizer/AvgDiagKernelNormalizer.h
>
16
#include <
shogun/features/StringFeatures.h
>
17
18
using namespace
shogun;
19
20
CMatchWordStringKernel::CMatchWordStringKernel
() :
CStringKernel
<uint16_t>()
21
{
22
init
();
23
}
24
25
CMatchWordStringKernel::CMatchWordStringKernel
(int32_t size, int32_t d)
26
:
CStringKernel
<uint16_t>(size)
27
{
28
init
();
29
degree
=d;
30
}
31
32
CMatchWordStringKernel::CMatchWordStringKernel
(
33
CStringFeatures<uint16_t>
* l,
CStringFeatures<uint16_t>
* r, int32_t d)
34
:
CStringKernel
<uint16_t>()
35
{
36
init
();
37
degree
=d;
38
init
(l, r);
39
}
40
41
CMatchWordStringKernel::~CMatchWordStringKernel
()
42
{
43
cleanup
();
44
}
45
46
bool
CMatchWordStringKernel::init(
CFeatures
* l,
CFeatures
* r)
47
{
48
CStringKernel<uint16_t>::init
(l, r);
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;
75
set_normalizer
(
new
CAvgDiagKernelNormalizer
());
76
SG_ADD
(&
degree
,
"degree"
,
"Degree of poly kernel"
,
MS_AVAILABLE
);
77
}
SHOGUN
Machine Learning Toolbox - Documentation