forked from erkanderon/matlab-menu-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editDist.m.bak
61 lines (48 loc) · 1.91 KB
/
editDist.m.bak
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
% function dist = editDist(inStr, dbStr, maxDist)
function dist = editDist(inStr, dbStr, maxDist)
% Base Case 1: When either strings is at the null characters
if ((numel(inStr)== 0) || (numel(dbStr)==0))
dist = numel(inStr) + numel(dbStr);
return;
end
% Base Case 2: Optimize recursive function with shortcircuiting
if (maxDist < 0)
dist = 0;
return;
end
% When first character has a match, return the edit distance from the shorten string
if (inStr(1) == dbStr(1))
dist = editDist(inStr(2:end), dbStr(2:end), maxDist);
return;
end
% When first character has no match, find the smallest edit distance of
% 3 cases: 1. replace 2. add 3. delete
d_rep = editDist(inStr(2:end), dbStr(2:end), maxDist-1);
d_add = editDist(inStr, dbStr(2:end), maxDist-1);
d_del = editDist(inStr(2:end), dbStr, maxDist-1);
% m = findMin(d_rep, d_add, d_del);
m = d_rep;
if (d_add < m)
m = d_add;
end
if (d_del < m)
m = d_del;
end
dist = m+1;
end
// When first character has a match, return the edit distance from the shorten string
if (str1[0] == str2[0]) {
return FindEditDistance(str1 + 1, str2 + 1, limit);
}
// When first character has no match, find the smallest edit distance of
// 3 cases: 1) replace 2) add 3) delete
int d_rep = FindEditDistance((str1 + 1), (str2 + 1), limit - 1);
int d_add = FindEditDistance(str1, (str2 + 1), limit - 1);
int d_del = FindEditDistance((str1 + 1), str2, limit - 1);
int min = FindMin(d_rep, d_add, d_del);
return min+1;
}
key = "Egg";
misspelled = "Eabgg";
limit = 10; // Maximum number of edit distance before returning (if more than limit, it will return edit distance over limit.
curCorrect.edit = FindEditDistance(key, misspelled, limit);