1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Marcin Pudełek <[email protected]> |
4
|
|
|
* Date: 11.12.2017 |
5
|
|
|
* Time: 12:42 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace mrcnpdlk\Xmdb\Model; |
9
|
|
|
|
10
|
|
|
use Oefenweb\DamerauLevenshtein\DamerauLevenshtein; |
11
|
|
|
|
12
|
|
|
class Ratio |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $title; |
18
|
|
|
/** |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
public $directorsDisplay; |
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
public $releaseYear; |
26
|
|
|
/** |
27
|
|
|
* @var \mrcnpdlk\Xmdb\Model\RatioElement[] |
28
|
|
|
*/ |
29
|
|
|
public $items = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ratio constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $title |
35
|
|
|
* @param string|null $directorsDisplay |
36
|
|
|
* @param string|null $releaseYear |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $title, string $directorsDisplay = null, string $releaseYear = null) |
39
|
|
|
{ |
40
|
|
|
$this->title = $title; |
41
|
|
|
$this->directorsDisplay = $directorsDisplay; |
42
|
|
|
$this->releaseYear = $releaseYear; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \mrcnpdlk\Xmdb\Model\Imdb\Title[] $tTitles |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function calculateRatio(array $tTitles) |
51
|
|
|
{ |
52
|
|
|
$this->items = []; |
53
|
|
|
foreach ($tTitles as $item) { |
54
|
|
|
$fTitleRatio = 0; |
55
|
|
|
$fDirectorRatio = 0; |
56
|
|
|
$fYearRatio = 0; |
57
|
|
View Code Duplication |
if (!empty($this->title) && !empty($item->titleOrg)) { |
|
|
|
|
58
|
|
|
$oL = new DamerauLevenshtein($this->title, $item->titleOrg); |
59
|
|
|
$fTitleRatio = $oL->getRelativeDistance(); |
60
|
|
|
} |
61
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
62
|
|
View Code Duplication |
if (!empty($this->director) && !empty($item->directorsDisplay)) { |
|
|
|
|
63
|
|
|
$oL = new DamerauLevenshtein($this->directorsDisplay, $item->directorsDisplay); |
64
|
|
|
$fDirectorRatio = $oL->getRelativeDistance(); |
65
|
|
|
} |
66
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
67
|
|
|
if (!empty($this->releaseYear) && !empty($item->releaseYear)) { |
68
|
|
|
$oL = new DamerauLevenshtein($this->releaseYear, $item->releaseYear); |
69
|
|
|
$fYearRatio = $oL->getRelativeDistance(); |
70
|
|
|
} |
71
|
|
|
$score = round(($fTitleRatio * 0.5 + $fDirectorRatio * 0.3 + $fYearRatio * 0.2) * 100, 3); |
72
|
|
|
|
73
|
|
|
$this->items[] = new RatioElement($score, $item); |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
usort( |
77
|
|
|
$this->items, |
78
|
|
|
function (RatioElement $itemOne, RatioElement $itemTwo) { |
79
|
|
|
return $itemTwo->score <=> $itemOne->score; |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.