/
levenshtein.pir
142 lines (114 loc) · 2.48 KB
/
levenshtein.pir
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
# Copyright (C) 2006-2009, Parrot Foundation.
#
# Levenshtein distance
#
# implementation based on http://www.merriampark.com/ld.htm
# First implementation by Alberto Simoes (ambs <at> cpan.org)
#
# .sub main :main
# $S1 = "purl"
# $S2 = "perl"
# $I1 = levenshtein($S1,$S2)
# print $I1
# print "\n"
# end
# .end
# Levenshtein distance. Pass two strings in
.sub levenshtein
.param string s
.param string t
.local int n # s size
.local int m # t size
.local pmc matrix
.local int i
.local int j
.local string schar
.local string tchar
.local int cost
.local int a
.local int b
.local int c
n = length s
m = length t
if n == 0 goto return_m
if m == 0 goto return_n
new matrix, 'ResizablePMCArray'
i = 0
init_matrix:
new $P0, 'ResizableIntegerArray'
matrix[i] = $P0
i += 1
if i <= m goto init_matrix
i = 0
init_matrix_1:
matrix[i;0] = i
i += 1
if i <= m goto init_matrix_1
i = 0
init_matrix_2:
matrix[0;i] = i
i += 1
if i <= n goto init_matrix_2
init_matrix_done:
i = 1
cycle:
j = 1
inner_cycle:
$I0 = i - 1
$I1 = j - 1
schar = substr s, $I1, 1
tchar = substr t, $I0, 1
cost = calculate_cost(schar, tchar)
# calculate a
a = matrix[$I0;j]
a += 1
# calculate b
b = matrix[i;$I1]
b += 1
# calculate c
c = matrix[$I0;$I1]
c += cost
cost = minimum(a,b,c)
matrix[i;j] = cost
j += 1
if j <= n goto inner_cycle
i += 1
if i <= m goto cycle
cost = matrix[m;n]
.return(cost)
return_m:
.return(m)
return_n:
.return(n)
.end
# return (first==second)?1:0
.sub calculate_cost
.param string first
.param string second
if first == second goto zero
.return(1)
zero:
.return(0)
.end
# return minimum(a,b,c)
.sub minimum
.param int a
.param int b
.param int c
if a < b goto other
if b < c goto b_label
c_label:
.return(c)
a_label:
.return(a)
b_label:
.return(b)
other:
if a < c goto a_label
goto c_label
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: