1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* @Name : similar-text |
6
|
|
|
* @Programmer : Akpé Aurelle Emmanuel Moïse Zinsou |
7
|
|
|
* @Date : 2019-04-01 |
8
|
|
|
* @Released under : https://github.com/manuwhat/similar-text/blob/master/LICENSE |
9
|
|
|
* @Repository : https://github.com/manuwhat/similar |
10
|
|
|
* |
11
|
|
|
**/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace EZAMA{ |
15
|
|
|
class diceDistance extends distance |
16
|
|
|
{ |
17
|
|
|
public static function dice($a, $b, $round=2) |
18
|
|
|
{ |
19
|
|
|
if ($distance=in_array(self::handleVeryCommonDiceCases($a, $b), array(false,0.0,1.0), true)) { |
20
|
|
|
return $distance; |
21
|
|
|
} |
22
|
|
|
static $distance=0; |
23
|
|
|
static $previous=array(); |
24
|
|
|
if (array($a,$b)===$previous) { |
25
|
|
|
return $distance; |
26
|
|
|
} |
27
|
|
|
$previous=array($a,$b); |
28
|
|
|
$a=self::split($a, 2); |
29
|
|
|
$b=self::split($b, 2); |
30
|
|
|
return self::diceDistance($distance, $a, $b, $round); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static function handleVeryCommonDiceCases(&$a, &$b) |
34
|
|
|
{ |
35
|
|
|
if (!is_string($a)||!is_string($b)) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
if (empty($a)||empty($b)) { |
39
|
|
|
return 0.0; |
40
|
|
|
} |
41
|
|
|
if ($a===$b) { |
42
|
|
|
return 1.0; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private static function diceDistance(&$distance, &$a, &$b, $round) |
47
|
|
|
{ |
48
|
|
|
$ca=($caGrams=count($a))*2-self::getEndStrLen($a); |
49
|
|
|
$cb=($cbGrams=count($b))*2-self::getEndStrLen($b); |
50
|
|
|
$distance=round(2*count($caGrams>$cbGrams?array_intersect($a, $b):array_intersect($b, $a))/($ca+$cb), $round); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private static function getEndStrLen($a) |
54
|
|
|
{ |
55
|
|
|
if (function_exists('array_key_last')) { |
56
|
|
|
$end=array_key_last($a); |
57
|
|
|
$end=(isset($end[1]))?0:1; |
58
|
|
|
} else { |
59
|
|
|
$end=end($a); |
60
|
|
|
$end=(isset($end[1]))?0:1; |
61
|
|
|
reset($a); |
62
|
|
|
} |
63
|
|
|
return $end; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.