1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace stojg\recommend; |
4
|
|
|
|
5
|
|
|
use stojg\recommend\strategy\Cosine; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Description of ArticleData. |
9
|
|
|
*/ |
10
|
|
|
class ArticleData extends Data |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $stopWords = ['a', 'about', 'above', 'above', 'across', 'after', 'afterwards', 'again', 'against', |
16
|
|
|
'all', 'almost', 'alone', 'along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst', |
17
|
|
|
'amoungst', 'amount', 'an', 'and', 'another', 'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', |
18
|
|
|
'are', 'around', 'as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been', |
19
|
|
|
'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both', |
20
|
|
|
'bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de', |
21
|
|
|
'describe', 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight', 'either', 'eleven', 'else', |
22
|
|
|
'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone', 'everything', 'everywhere', |
23
|
|
|
'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly', |
24
|
|
|
'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has', 'hasnt', |
25
|
|
|
'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers', 'herself', 'him', |
26
|
|
|
'himself', 'his', 'how', 'however', 'hundred', "i'd", 'ie', 'if', 'in', 'inc', 'indeed', 'interest', 'into', |
27
|
|
|
'is', 'it', 'its', 'itself', 'keep', 'last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many', |
28
|
|
|
'may', 'me', 'meanwhile', 'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must', |
29
|
|
|
'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine', 'no', 'nobody', 'none', |
30
|
|
|
'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once', 'one', 'only', 'onto', |
31
|
|
|
'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'part', 'per', |
32
|
|
|
'perhaps', 'please', 'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious', |
33
|
|
|
'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow', |
34
|
|
|
'someone', 'something', 'sometime', 'sometimes', 'somewhere', 'still', 'such', 'system', 'take', 'ten', 'than', |
35
|
|
|
'that', 'the', 'their', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', |
36
|
|
|
'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this', 'those', 'though', 'three', |
37
|
|
|
'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward', 'towards', 'twelve', |
38
|
|
|
'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we', 'well', 'were', 'what', |
39
|
|
|
'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', |
40
|
|
|
'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'will', |
41
|
|
|
'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves', 'the', ]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $set |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
// Noop - We want to start off with a clean set and use the push method to add |
49
|
|
|
// data to the set |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $identifier |
54
|
|
|
* @param string $content |
55
|
|
|
*/ |
56
|
|
|
public function push($identifier, $content) |
57
|
|
|
{ |
58
|
|
|
$this->set[$identifier] = $this->getWordCount($content); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $for |
63
|
|
|
* @param object $strategy |
64
|
|
|
*/ |
65
|
|
|
public function findNearest($for, $strategy = null) |
66
|
|
|
{ |
67
|
|
|
if ($strategy === null) { |
68
|
|
|
$strategy = new Cosine(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return parent::findNearest($for, $strategy); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return a list of recommendations. |
76
|
|
|
* |
77
|
|
|
* @param string $for - the item we want recommendations for |
78
|
|
|
* @param object $strategy |
79
|
|
|
* |
80
|
|
|
* @return array - return a list of identifier ordered by closest |
81
|
|
|
*/ |
82
|
|
|
public function recommend($for, $strategy = null) |
83
|
|
|
{ |
84
|
|
|
if ($strategy === null) { |
85
|
|
|
$strategy = new Cosine(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$distances = []; |
89
|
|
|
foreach ($this->set as $key => $itemData) { |
90
|
|
|
if ($key == $for) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
$distance = $strategy->run($itemData, $this->set[$for]); |
94
|
|
|
if ($distance === false) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
$distances[$key] = ['key' => $key, 'value' => $distance]; |
98
|
|
|
} |
99
|
|
|
if (!count($distances)) { |
100
|
|
|
return false; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
$this->sort($distances, true); |
103
|
|
|
|
104
|
|
|
$data = []; |
105
|
|
|
foreach ($distances as $article) { |
106
|
|
|
if ($article['value'] == 0) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$data[] = $article['key']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get an array of words from the content and a count of how many times |
117
|
|
|
* they appear in the text. |
118
|
|
|
* |
119
|
|
|
* Note that this method is naive and can't tell the similarity between 'bird' and 'birds'. |
120
|
|
|
* |
121
|
|
|
* @param string $content |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getWordCount($content) |
126
|
|
|
{ |
127
|
|
|
$content = strip_tags(strtolower((preg_replace('/\s+/', ' ', $content)))); |
128
|
|
|
$words = str_word_count($content, 1); |
129
|
|
|
$filteredWords = array_diff($words, $this->stopWords); |
130
|
|
|
$countedWords = array_count_values($filteredWords); |
131
|
|
|
ksort($countedWords); |
132
|
|
|
|
133
|
|
|
return $countedWords; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.