-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlevenshtein.php
80 lines (63 loc) · 1.81 KB
/
levenshtein.php
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
<?php
function distance($word1, $word2){
if(strlen($word1) == 0)
return strlen($word2);
elseif(strlen($word2) == 0)
return strlen($word1);
else{
$cost = $word1[0] == $word2[0]?0:1;
return min(distance(substr($word1, 1), $word2) + 1,
distance($word1, substr($word2, 1)) + 1,
distance(substr($word1, 1), substr($word2, 1)) + $cost);
}
}
function idistance($word1, $word2){
$e = array(array());
for($i = 0; $i <= strlen($word1); $i++)
$e[$i][0] = $i;
for($j = 1; $j <= strlen($word2); $j++)
$e[0][$j] = $j;
for($i = 1; $i <= strlen($word1); $i++)
for($j = 1; $j <= strlen($word2); $j++){
$cost = $word1[$i-1] == $word2[$j-1]?0:1;
$e[$i][$j] = min($e[$i-1][$j] + 1,
$e[$i][$j-1] + 1,
$e[$i-1][$j-1] + $cost);
}
return end(end($e));
}
$function = 'distance';
$function = 'idistance';
//$function = 'levenshtein';
$tests = array(
$function('a', 'a') == 0,
$function('', 'a') == 1,
$function('', 'abc') == 3,
$function('a', '') == 1,
$function('abc', '') == 3,
$function('a', 'b') == 1,
$function('aa', 'b') == 2,
$function('casa', 'caso') == 1,
$function('casarao', 'casa') == 3,
$function('camarao', 'casa') == 4,
);
foreach($tests as $test){
print $test?'.':'F';
}
//die;
$word = $argv[1];
if(empty($word)){
print "You have forgoten the word";
die;
}
$lines = file("ptbr.txt");
print count($lines) . " loaded lines;\n";
print "Looking for $word\n";
$start = microtime(true);
foreach($lines as $line){
if($function($word, $line) < 1)
echo($line);
}
$end = microtime(true);
$duration = $end - $start;
print "\niDuration: $duration";