forked from Innoz-Technologies/SMSGyan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.CompareString.php
75 lines (62 loc) · 2.34 KB
/
class.CompareString.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
<?php
class CompareString {
private $str1, $str2;
/**
*
* @var int levenshtein distance between input strings
*/
public $levenshtein;
/**
*
* @var array value, percentage
*/
public $similarity = array('value' => false, 'percentage' => false);
/**
*
* @var array levenshtein, similarity, percentage similariry
*/
public $soundex = array('levenshtein' => false, 'similarity' => false, 'percentage' => false);
/**
*
* @var array levenshtein, similarity, percentage similariry
*/
public $metaphone = array('levenshtein' => false, 'similarity' => false, 'percentage' => false);
public function __construct($first, $second) {
$this->str1 = strtolower($first);
$this->str2 = strtolower($second);
}
function compare($debug = false) {
$first = $this->str1;
$second = $this->str2;
$this->levenshtein = levenshtein($first, $second);
$this->similarity['value'] = $sim = similar_text($first, $second, $perc);
$this->similarity['percentage'] = $perc;
if ($debug) {
echo "$first | $second<br>";
echo "leven: " . $this->levenshtein;
echo '<br>similarity: ' . $sim . ', ' . $perc . '%<br><br>';
}
$soundex1 = soundex($first);
$soundex2 = soundex($second);
$this->soundex['levenshtein'] = levenshtein($soundex1, $soundex2);
$this->soundex['similarity'] = $sim = similar_text($soundex1, $soundex2, $perc);
$this->soundex['percentage'] = $perc;
if ($debug) {
echo "Soundex: " . $soundex1 . ", " . $soundex2 . "<BR>";
echo 'levenshtein: ' . $this->soundex['levenshtein'] . '<br>';
echo 'similarity: ' . $sim . ', ' . $perc . '%<br><br>';
}
$m1 = metaphone($first);
$m2 = metaphone($second);
$this->metaphone['levenshtein'] = levenshtein($m1, $m2);
$this->metaphone['similarity'] = $sim = similar_text($m1, $m2, $perc);
$this->metaphone['percentage'] = $perc;
if ($debug) {
echo "metaphone: " . $m1 . ", " . $m2 . "<br>";
echo 'levenshtein: ' . $this->metaphone['levenshtein'] . '<br>';
echo 'similarity: ' . $sim . ', ' . $perc . '%<br>';
echo '<br>-------------------<br>';
}
}
}
?>