-
Notifications
You must be signed in to change notification settings - Fork 1
/
dldist.cpp
125 lines (104 loc) · 3.44 KB
/
dldist.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
// modified uf8-supported version of
// https://github.com/PierreBoyeau/levenshtein_distance
#include<string>
#include<algorithm>
#include<iostream>
#include "utf8_unicode.hpp"
using std::string;
using std::vector;
using std::min;
using std::max;
int mini(int a, int b, int c){
return(min(a, min(b,c)));
}
int levenshtein_dist(const string &word1, const string &word2){
///
/// Please use lower-case strings
/// word1 : first word
/// word2 : second word
///
//int size1 = word1.size(), size2 = word2.size();
vector<string> word1_ = utf8_split(word1);
vector<string> word2_ = utf8_split(word2);
int size1 = word1_.size();
int size2 = word2_.size();
int suppr_dist, insert_dist, subs_dist;
int* dist = new int[(size1+1)*(size2+1)];
for(int i=0; i<size1+1; ++i)
dist[(size2+1)*i] = i;
for(int j=0; j<size2+1; ++j)
dist[j] = j;
for(int i=1; i<size1+1; ++i){
for(int j=1; j<size2+1; ++j){
suppr_dist = dist[(size2+1)*(i-1)+j] + 1;
insert_dist = dist[(size2+1)*i+j-1] + 1;
subs_dist = dist[(size2+1)*(i-1)+j-1];
if(word1_[i-1]!=word2_[j-1]){ // word indexes are implemented differently.
subs_dist += 1;
}
dist[(size2+1)*i+j] = mini(suppr_dist, insert_dist, subs_dist);
}
}
// --------------------------------------------------------
int res = dist[(size1+1)*(size2+1) - 1];
delete dist;
return(res);
}
int dl_dist(const string &word1, const string &word2){
/// Damerau-Levenshtein distance
/// Please use lower-case strings
/// word1 : first word
/// word2 : second word
///
//int size1 = word1.size(), size2 = word2.size();
vector<string> word1_ = utf8_split(word1);
vector<string> word2_ = utf8_split(word2);
int size1 = word1_.size();
int size2 = word2_.size();
int suppr_dist, insert_dist, subs_dist, val;
int* dist = new int[(size1+1)*(size2+1)];
for(int i=0; i<size1+1; ++i)
dist[(size2+1)*i] = i;
for(int j=0; j<size2+1; ++j)
dist[j] = j;
for(int i=1; i<size1+1; ++i){
for(int j=1; j<size2+1; ++j){
suppr_dist = dist[(size2+1)*(i-1)+j] + 1;
insert_dist = dist[(size2+1)*i+j-1] + 1;
subs_dist = dist[(size2+1)*(i-1)+j-1];
if(word1_[i-1]!=word2_[j-1]) // word indexes are implemented differently.
subs_dist += 1;
val = mini(suppr_dist, insert_dist, subs_dist);
if(((i>=2) && (j>=2)) && ((word1_[i-1]==word2_[j-2]) && (word1_[i-2]==word2_[j-1])))
val = min(dist[(size2+1)*(i-2)+j-2]+1, val);
dist[(size2+1)*i+j] = val;
}
}
int res = dist[(size1+1)*(size2+1) - 1];
delete dist;
return(res);
}
// L. Schiffmann, 2021
double levenshtein_distp(const string &str1, const string &str2) {
int l = levenshtein_dist(str1,str2);
vector<string> str1_ = utf8_split(str1);
vector<string> str2_ = utf8_split(str2);
int m = str1_.size();
int n = str2_.size();
if (max(m,n) == 0) {
return 0;
}
return 1.0 - (double)l / max(m,n);
}
// L. Schiffmann, 2021
double dl_distp(const string &str1, const string &str2) {
int l = dl_dist(str1,str2);
vector<string> str1_ = utf8_split(str1);
vector<string> str2_ = utf8_split(str2);
int m = str1_.size();
int n = str2_.size();
if (max(m,n) == 0) {
return 0;
}
return 1.0 - (double)l / max(m,n);
}