-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
HammingWordDistance.cpp
175 lines (142 loc) · 3.46 KB
/
HammingWordDistance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Evan Shelhamer
*/
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parameter.h>
#include <shogun/distance/HammingWordDistance.h>
#include <shogun/features/Features.h>
#include <shogun/features/StringFeatures.h>
using namespace shogun;
CHammingWordDistance::CHammingWordDistance()
{
init();
}
CHammingWordDistance::CHammingWordDistance(bool sign)
: CStringDistance<uint16_t>()
{
init();
use_sign=sign;
SG_DEBUG("CHammingWordDistance with sign: %d created\n", (sign) ? 1 : 0)
}
CHammingWordDistance::CHammingWordDistance(
CStringFeatures<uint16_t>* l, CStringFeatures<uint16_t>* r, bool sign)
: CStringDistance<uint16_t>()
{
init();
use_sign=sign;
SG_DEBUG("CHammingWordDistance with sign: %d created\n", (sign) ? 1 : 0)
init(l, r);
}
CHammingWordDistance::~CHammingWordDistance()
{
cleanup();
}
bool CHammingWordDistance::init(CFeatures* l, CFeatures* r)
{
bool result=CStringDistance<uint16_t>::init(l,r);
return result;
}
void CHammingWordDistance::cleanup()
{
}
float64_t CHammingWordDistance::compute(int32_t idx_a, int32_t idx_b)
{
int32_t alen, blen;
bool free_avec, free_bvec;
uint16_t* avec=((CStringFeatures<uint16_t>*) lhs)->
get_feature_vector(idx_a, alen, free_avec);
uint16_t* bvec=((CStringFeatures<uint16_t>*) rhs)->
get_feature_vector(idx_b, blen, free_bvec);
int32_t result=0;
int32_t left_idx=0;
int32_t right_idx=0;
if (use_sign)
{
// hamming of: if words appear in both vectors
while (left_idx < alen && right_idx < blen)
{
uint16_t sym=avec[left_idx];
if (avec[left_idx]==bvec[right_idx])
{
while (left_idx< alen && avec[left_idx]==sym)
left_idx++;
while (right_idx< blen && bvec[right_idx]==sym)
right_idx++;
}
else if (avec[left_idx]<bvec[right_idx])
{
result++;
while (left_idx< alen && avec[left_idx]==sym)
left_idx++;
}
else
{
sym=bvec[right_idx];
result++;
while (right_idx< blen && bvec[right_idx]==sym)
right_idx++;
}
}
}
else
{
//hamming of: if words appear in both vectors _the same number_ of times
while (left_idx < alen && right_idx < blen)
{
uint16_t sym=avec[left_idx];
if (avec[left_idx]==bvec[right_idx])
{
int32_t old_left_idx=left_idx;
int32_t old_right_idx=right_idx;
while (left_idx< alen && avec[left_idx]==sym)
left_idx++;
while (right_idx< blen && bvec[right_idx]==sym)
right_idx++;
if ((left_idx-old_left_idx)!=(right_idx-old_right_idx))
result++;
}
else if (avec[left_idx]<bvec[right_idx])
{
result++;
while (left_idx< alen && avec[left_idx]==sym)
left_idx++;
}
else
{
sym=bvec[right_idx];
result++;
while (right_idx< blen && bvec[right_idx]==sym)
right_idx++;
}
}
}
while (left_idx < alen)
{
uint16_t sym=avec[left_idx];
result++;
while (left_idx< alen && avec[left_idx]==sym)
left_idx++;
}
while (right_idx < blen)
{
uint16_t sym=bvec[right_idx];
result++;
while (right_idx< blen && bvec[right_idx]==sym)
right_idx++;
}
((CStringFeatures<uint16_t>*) lhs)->
free_feature_vector(avec, idx_a, free_avec);
((CStringFeatures<uint16_t>*) rhs)->
free_feature_vector(bvec, idx_b, free_bvec);
return result;
}
void CHammingWordDistance::init()
{
use_sign = false;
SG_ADD(
&use_sign, "use_sign", "If signum(counts) is used instead of counts.",
MS_NOT_AVAILABLE);
}