1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Options; |
14
|
|
|
|
15
|
|
|
use function is_array; |
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
18
|
|
|
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionDecorator; |
19
|
|
|
use Maslosoft\Manganel\Helpers\Strings; |
20
|
|
|
use Maslosoft\Manganel\Helpers\TypeNamer; |
21
|
|
|
use Maslosoft\Manganel\Manganel; |
22
|
|
|
use redirect_interval; |
23
|
|
|
use ReflectionClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Holder for more like options |
27
|
|
|
* @package Maslosoft\Manganel\Options |
28
|
|
|
*/ |
29
|
|
|
class MoreLike |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Models to search from similar |
33
|
|
|
* @var AnnotatedInterface[] |
34
|
|
|
*/ |
35
|
|
|
public $models = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A list of fields to fetch and analyze the text from. Defaults to the _all field for free text and to all |
39
|
|
|
* possible fields for document inputs. |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $fields = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* The unlike parameter is used in conjunction with like in order not to select terms found in a chosen set of |
47
|
|
|
* documents. In other words, we could ask for documents like: "Apple", but unlike: "cake crumble tree". The syntax |
48
|
|
|
* is the same as like. |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
public $unlike = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Extra text to search for |
55
|
|
|
* @var string[] |
56
|
|
|
*/ |
57
|
|
|
public $texts = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The maximum number of query terms that will be selected. Increasing this value gives greater accuracy at the |
61
|
|
|
* expense of query execution speed. |
62
|
|
|
* @var int |
63
|
|
|
*/ |
64
|
|
|
public $maxQueryTerms = 25; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The minimum term frequency below which the terms will be ignored from the input document. |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
public $minTermFreq = 2; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The minimum document frequency below which the terms will be ignored from the input document. |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
public $minDocFreq = 5; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The maximum document frequency above which the terms will be ignored from the input document. This could be |
80
|
|
|
* useful in order to ignore highly frequent words such as stop words |
81
|
|
|
* @var int |
82
|
|
|
*/ |
83
|
|
|
public $maxDocFreq = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The minimum word length below which the terms will be ignored. The old name min_word_len is deprecated. |
87
|
|
|
* @var int |
88
|
|
|
*/ |
89
|
|
|
public $minWordLength = 0; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The maximum word length above which the terms will be ignored. The old name max_word_len is deprecated. |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
public $maxWordLength = null; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* An array of stop words. Any word in this set is considered "uninteresting" and ignored. If the analyzer allows |
99
|
|
|
* for stop words, you might want to tell MLT to explicitly ignore them, as for the purposes of document similarity |
100
|
|
|
* it seems reasonable to assume that "a stop word is never interesting". |
101
|
|
|
* @var array |
102
|
|
|
*/ |
103
|
|
|
public $stopWords = []; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* MoreLikeOptions constructor. |
107
|
|
|
* @param AnnotatedInterface|AnnotatedInterface[]|DataProviderInterface|null $models |
108
|
|
|
*/ |
109
|
|
|
public function __construct($models = null) |
110
|
|
|
{ |
111
|
|
|
if ($models instanceof DataProviderInterface) |
112
|
|
|
{ |
113
|
|
|
$this->models = $models->getData(); |
114
|
|
|
} |
115
|
|
|
elseif ($models instanceof AnnotatedInterface) |
116
|
|
|
{ |
117
|
|
|
$this->models = [$models]; |
118
|
|
|
} |
119
|
|
|
elseif (is_array($models)) |
120
|
|
|
{ |
121
|
|
|
$this->models = $models; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function toArray() |
126
|
|
|
{ |
127
|
|
|
$options = []; |
128
|
|
|
$like = []; |
129
|
|
|
if (!empty($this->texts)) |
130
|
|
|
{ |
131
|
|
|
foreach ((array)$this->texts as $text) |
132
|
|
|
{ |
133
|
|
|
$like[] = $text; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if (!empty($this->models)) |
137
|
|
|
{ |
138
|
|
|
foreach ($this->models as $model) |
139
|
|
|
{ |
140
|
|
|
$like[] = [ |
141
|
|
|
'_index' => Manganel::create($model)->index, |
142
|
|
|
'_type' => TypeNamer::nameType($model), |
143
|
|
|
'_id' => (string)$model->_id |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!empty($like)) |
149
|
|
|
{ |
150
|
|
|
$options['like'] = $like; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!empty($this->fields)) |
154
|
|
|
{ |
155
|
|
|
// TODO Must decorate, ie `title` to be `title.en` etc. |
156
|
|
|
$options['fields'] = $this->fields; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if(!empty($this->unlike)) |
160
|
|
|
{ |
161
|
|
|
// TODO Must consider also models array etc. |
162
|
|
|
$options['unlike'] = $this->unlike; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$keys = [ |
166
|
|
|
'maxQueryTerms', |
167
|
|
|
'minTermFreq', |
168
|
|
|
'minDocFreq', |
169
|
|
|
'maxDocFreq', |
170
|
|
|
'minWordLength', |
171
|
|
|
'maxWordLength', |
172
|
|
|
'stopWords', |
173
|
|
|
]; |
174
|
|
|
$info = new ReflectionClass($this); |
175
|
|
|
$defaults = $info->getDefaultProperties(); |
176
|
|
|
|
177
|
|
|
foreach ($keys as $name) |
178
|
|
|
{ |
179
|
|
|
$value = $this->$name; |
180
|
|
|
if (null === $value) |
181
|
|
|
{ |
182
|
|
|
continue; |
183
|
|
|
} |
184
|
|
|
if (is_array($value) && empty($value)) |
185
|
|
|
{ |
186
|
|
|
continue; |
187
|
|
|
} |
188
|
|
|
if ($defaults[$name] === $value) |
189
|
|
|
{ |
190
|
|
|
continue; |
191
|
|
|
} |
192
|
|
|
$key = Strings::decamelize($name, '_'); |
193
|
|
|
$options[$key] = $value; |
194
|
|
|
} |
195
|
|
|
return $options; |
196
|
|
|
} |
197
|
|
|
} |