-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbi-sim.cpp
More file actions
53 lines (44 loc) · 1.28 KB
/
bi-sim.cpp
File metadata and controls
53 lines (44 loc) · 1.28 KB
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
// Copyright 2012 Florian Petran
#include"bi-sim.h"
#include<vector>
#include<algorithm>
using std::vector;
using std::max;
namespace bi_sim {
typedef unsigned int uint;
typedef vector<num_ty> mat_ty;
inline num_ty id(uint x, uint y) {
return x == y ? 1 : 0;
}
num_ty bi_sim(const string_impl& w1, const string_impl& w2) {
uint m = w1.length(),
n = w2.length();
string_impl x = w1,
y = w2;
lower_case(&x);
lower_case(&y);
char_impl x1 = x[0],
y1 = y[0];
upper_case(&x1);
upper_case(&y1);
x = x1 + x;
y = y1 + y;
// initialize and fill matrix
vector<vector<num_ty>> f;
f.resize(m+1);
for (uint i = 0; i <= m; ++i) {
f[i].resize(n+1);
f[i][0] = 0.0;
}
for (uint i = 0; i <= n; ++i)
f[0][i] = 0.0;
// calculate bi_sim
for (uint i = 1; i <= m; ++i)
for (uint j = 1; j <= n; ++j)
f[i][j] = max(max(f[i-1][j], f[i][j-1]),
f[i-1][j-1]
+ id(x[i-1], y[j-1])
+ id(x[i], y[j]));
return f[m][n] / (2 * max(m, n));
}
} // namespace bi_sim