-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConceptSearchResultsGroup.inc.php
executable file
·321 lines (286 loc) · 8.99 KB
/
ConceptSearchResultsGroup.inc.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* Object to represent the search results associated with a single
* ConceptSearchGroup object.
*/
class ConceptSearchResultsGroup
{
/**
* Reference to the ConceptSearchGroup used to create this object.
*/
public $csg = null;
/**
* Collection of sql objects executed to populate this results group.
*/
public $coll_sql_obj = null;
/**
* Array of references to Concept objects linked to this results group. Of the format:
* [<dict_db>][<concept_id>]
*/
private $arr_concept = array();
/**
* Array of relevancy ratings
*/
private $arr_relevancy = array();
/**
* Constructor - requires a ConceptSearchGroup
*/
public function __construct(ConceptSearchGroup $csg)
{
$this->csg = $csg;
}
/**
* Add Concept to this search results group.
* @param Concept $c Concept object
* @param mixed $relevancy Integer of the concept's relevancy rating or null
*/
public function addConcept(Concept $c, $relevancy = null)
{
// Concepts are internally partitioned by their dictionary source
$dict_db = '';
if ( $c->css_dict ) $dict_db = strtolower($c->css_dict->dict_db);
$this->arr_concept [ $dict_db ][ $c->concept_id ] = $c ;
$this->arr_relevancy[ $dict_db ][ $c->concept_id ] = $relevancy ;
}
public function setRelevancy(Concept $c, $relevancy)
{
$dict_db = '';
if ( $c->css_dict ) $dict_db = strtolower($c->css_dict->dict_db);
$this->arr_relevancy[ $dict_db ][ $c->concept_id ] = $relevancy ;
}
public function getRelevancy(Concept $c)
{
$dict_db = '';
if ( $c->css_dict ) $dict_db = strtolower($c->css_dict->dict_db);
if ( isset($this->arr_relevancy[ $dict_db ][ $c->concept_id ]) ) {
return $this->arr_relevancy[ $dict_db ][ $c->concept_id ];
}
return null;
}
public function getMaxRelevancy(ConceptSearchSource $css_dict = null)
{
// Set the dictionary source
if ($css_dict) $dict_db = $css_dict->dict_db;
else $dict_db = '';
// Calculate the max for this dictionary
$max = 0;
if (isset($this->arr_relevancy[$dict_db]))
{
foreach (array_keys($this->arr_relevancy[$dict_db]) as $concept_id)
{
if (!is_null($this->arr_relevancy[$dict_db][$concept_id]) &&
$this->arr_relevancy[$dict_db][$concept_id] > $max)
{
$max = $this->arr_relevancy[$dict_db][$concept_id];
}
}
}
return $max;
}
/**
* Get the specified concept
* @param int $concept_id
* @param mixed $dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY or a
* string of the name of a dictionary database
* @return Concept
*/
public function getConcept($concept_id, $dict = '')
{
// Determine the dictionary
if ($dict instanceof ConceptSearchSource) {
$dict_db = strtolower($dict->dict_db);
} else {
$dict_db = $dict;
}
// Get the concept
$c = null;
if ( isset($this->arr_concept[ $dict_db ][ $concept_id ] ))
{
$c = $this->arr_concept[ $dict_db ][ $concept_id ];
}
return $c;
}
/**
* Get the number of concepts stored in this search results group.
* @param mixed $css_dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY, a string of the name of a dictionary database, or null to indicate all dictionary sources.
* @return int
*/
public function getCount($css_dict = null)
{
// Determine the dictionary
if ($css_dict instanceof ConceptSearchSource) {
$dict_db = strtolower( $css_dict->dict_db );
} else {
$dict_db = $css_dict;
}
// Count
$num = 0;
if (is_null($dict_db))
{
// Return concepts for all dictionaries if null
foreach (array_keys($this->arr_concept) as $key_dict) {
$num += count($this->arr_concept[$key_dict]);
}
} elseif (isset($this->arr_concept[$dict_db])) {
// Return concepts for only the specified dictionary if anything other null
$num = count($this->arr_concept[$dict_db]);
}
return $num;
}
/**
* Get the number of visible concepts stored in this collection. The collection
* may store a number of "invisible" concepts (i.e. concept sets or Q/A sets) that
* were not part of the base search query.
* @return int
*/
public function getVisibleCount()
{
$num = 0;
foreach ( array_keys($this->arr_concept) as $key_dict )
{
foreach ( array_keys($this->arr_concept[ $key_dict ]) as $concept_id )
{
if ( $this->arr_concept[ $key_dict ][ $concept_id ]->display ) $num++;
}
}
return $num;
}
/***************************************************************************************************
* HELPER FUNCTIONS
**************************************************************************************************/
/**
* Get an arary of keys for dictionary sources used in this collection.
* @return array
*/
public function getDictionarySources()
{
return array_keys($this->arr_concept);
}
/**
* Get array of concept IDs contained in the search results group
* @param ConceptSearchSource $css_dict
* @return array
*/
public function getConceptIds(ConceptSearchSource $css_dict = null)
{
// Set the dictionary source
if ($css_dict) $dict_db = $css_dict->dict_db;
else $dict_db = '';
// Get the IDs
if (isset($this->arr_concept[$dict_db])) {
return array_keys($this->arr_concept[$dict_db]);
}
return array();
}
/**
* Returns multidimensional array of concepts sorted by relevancy. The returned array is of the form:
* [n] = array(
* 'concept_id' => #
* 'dict_db' => ''
* )
* @param mixed $dict ConceptSearchSource object of type dictionary, the dictionary database name, or null for all dictionary sources
*/
public function getRelevancySortedConceptArray($dict = null)
{
// Create array of dictionary keys
if ( $dict instanceof ConceptSearchSource ) {
$arr_dict_db = array($dict->dict_db);
} elseif ( is_null($dict) ) {
$arr_dict_db = array_keys($this->arr_concept);
} else {
$arr_dict_db = array($dict);
}
// Build the unsorted relevancy array
$key_delimiter = '~~~~~';
$arr_relevancy = array();
foreach ($arr_dict_db as $dict_db)
{
// Get visible concept id array for the current source
$arr_concept_id = $this->getVisibleConceptIds($dict_db);
// Get relevancies for the current source
foreach ($arr_concept_id as $concept_id)
{
$key = $dict_db . $key_delimiter . $concept_id;
$relevancy = null;
if ( isset($this->arr_relevancy[$dict_db][$concept_id]) )
{
$relevancy = $this->arr_relevancy[$dict_db][$concept_id];
}
$arr_relevancy[ $key ] = $relevancy;
}
}
// Build sorted concept array
arsort($arr_relevancy, SORT_NUMERIC);
$arr_sort_concept_id = array();
foreach ($arr_relevancy as $key => $relevancy) {
list($dict_db, $concept_id) = explode($key_delimiter, $key, 2);
$arr_sort_concept_id[] = array(
'concept_id' => $concept_id,
'dict_db' => $dict_db
);
}
return $arr_sort_concept_id;
}
/**
* Returns a string of comma-separated IDs for all concepts that are members of the specified dictionary source.
* @param ConceptSearchSource $css_dict
* @return string
*/
public function getConceptIdCsv(ConceptSearchSource $css_dict = null)
{
return implode( ',' , $this->getConceptIds($css_dict) );
}
/**
* Returns a string of comma-separated IDs of all visible concepts in this collection.
* @param mixed $css_dict ConceptSearchSource object of type dictionary or the dictionary database name
*/
public function getVisibleConceptIds($dict)
{
// Set the dictionary source
if ($dict instanceof ConceptSearchSource) {
$dict_db = $dict->dict_db;
} else {
$dict_db = $dict;
}
// Get the IDs
if (isset($this->arr_concept[$dict_db]))
{
$arr_concept_id = array();
foreach (array_keys($this->arr_concept[$dict_db]) as $concept_id)
{
if ($this->arr_concept[$dict_db][$concept_id]->display) {
$arr_concept_id[$concept_id] = $concept_id;
}
}
return $arr_concept_id;
}
else
{
trigger_error('Unrecognized dictionary source: ' . $dict_db, E_USER_ERROR);
}
}
/**
* Returns a string of comma-separated IDs of all visible concepts in this collection.
* @param ConceptSearchSource $css_dict
*/
public function getVisibleConceptIdCsv(ConceptSearchSource $css_dict = null)
{
return implode(',', $this->getVisibleConceptIds($css_dict));
}
public function toString()
{
$s = '<pre>Concepts:<br>';
foreach (array_keys($this->arr_concept) as $dict_db) {
foreach (array_keys($this->arr_concept[$dict_db]) as $concept_id) {
$c = $this->getConcept($concept_id, $dict_db);
$s .= $dict_db . ':' . $concept_id . ' - ' . $c->getPreferredName() . "\n";
ob_start();
var_dump($c);
$s .= ob_get_clean();
}
}
$s .= '</pre>';
return $s;
}
}
?>