-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocsearch.php
executable file
·448 lines (388 loc) · 12.9 KB
/
docsearch.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
// FUNCTIONS
//####################
function cosine_sim($vecstring1, $vecstring2, $sumsq1, $sumsq2) {
//echo "vecstring1: ".$vecstring1."<br/>";
$vector1 = explode(" ", $vecstring1);
$vector2 = explode(" ", $vecstring2);
$numdims = count($vector1);
//echo "count: ".$numdims.", ";
$dotproduct = 0.0;
$increment = 0.0;
for ($d = 0; $d < $numdims; $d++) {
$increment = $vector1[$d] * $vector2[$d];
$dotproduct = $dotproduct + $increment;
}
$magnitude = ($sumsq1 * $sumsq2);
//echo "magn: ".$magnitude.", ";
$rawcorrelation = $dotproduct / $magnitude;
//echo "raw corr: ".$rawcorrelation.". ";
$correlation = round($rawcorrelation, 4);
return $correlation;
}
function combine_key($int1, $int2) {
$newkey = $int1 * 1000000000;
$newkey = $newkey + $int2;
return $newkey;
}
function extract_key2($keyint) {
$key2 = $keyint % 1000000000;
return $key2;
}
function extract_key1($keyint, $mod) {
$key0 = $keyint - $mod;
$key1 = $key0 / 1000000000;
return $key1;
}
// MAIN PROCEDURE
//####################
// run the search over the correlation matrix
// open the database
$textSite = "";
$connection = new mysqli();
require_once 'functions/mysql_connection.php';
$logfile = "log/docsearch.txt";
$log = fopen($logfile, "w");
/*if (!$log) {
echo "no log";
return;
}*/
fwrite($log, "opened docsearch: ".memory_get_usage()." at ".date("M d g:i:s")."\n");
$list = $_GET['list'];
$frags = $_GET['frags'];
$scope = $_GET['scope'];
$outf = $_GET['outf'];
$bound = $_GET['bound'];
$qs = $_GET['qs'];
$hash_value = md5($list."|".$frags."|".$scope."|".$bound."|".$qs);
if($outf == "graph" && file_exists("graphs/graph-".$hash_value.".nwb")) {
$downloadpath = "graphs/graph-".$hash_value.".nwb";
echo "<br/><br/>
<small>(Right-click the link and use <em>Save link as...</em> to get a copy for viewing in Network Workbench or an editor.)</small>
<br/><br/>
<table class='lsa-resultsTable'>
<tr>
<td>NWB network graph file (cached):</td><td><a href='".$downloadpath."'>Link for download.</a> Give it a new name.</td>
</tr>
</table>
<br/><br/>";
exit;
}
fwrite($log, "list=$list, frags=$frags, scope=$scope, outf=$outf, bound=$bound, qs=$qs\n");
//begin setup
$listTable = "doc250_list";
$correlationTable = "doc250_cosines";
if ($frags == "ch1000") {
$listTable = "doc1000_list";
$correlationTable = "doc1000_cosines";
}
fwrite($log, "initialized: memoryused: ".memory_get_usage()."\n");
// $qs will contain doc IDs (ALCHnn_ALCHnn) or chunk IDs (n_n_n) or ALL
// we'll use an array to track user's selections passed in $qs
// fill $selected by retrieving user choices from doclist
$selected = array();
if ($qs == "ALL") {
// grab everything from doclist
$selectalldocs = "SELECT id, ctitle FROM ".$listTable;
if (!empty($connection)) {
$allset = mysqli_query($connection, $selectalldocs);
}
fwrite($log, "allset query made. memory used: ".memory_get_usage()."\n");
while($row_all = mysqli_fetch_row($allset)) {
$selected[$row_all[0]] = $row_all[1];
}
mysqli_free_result($allset);
fwrite($log, "allset released, selected array loaded. memory used: ".memory_get_usage()."\n");
}
else {
$qset = explode("_", $qs);
fwrite($log, "user query string in qset array. memory used: ".memory_get_usage()."\n");
$matchID = "";
$matchcount = 0;
foreach($qset as $qn) {
if ($matchcount > 0) {
$matchID = $matchID.",'".$qn."'";
}
else {
$matchID = $matchID."'".$qn."'";
$matchcount = 1;
}
}
unset($qset);
fwrite($log, "qset unset. memory used: ".memory_get_usage()."\n");
$wherecolumn = "zz";
if ($list == "wholedocs") {
$wherecolumn = "alch";
}
else if ($list == "chunks") {
$wherecolumn = "id";
}
fwrite($log, "matchID: ".$matchID."\n");
$selectsome = "SELECT id, ctitle FROM ".$listTable." WHERE ".$wherecolumn." IN (".$matchID.")";
fwrite($log, "selectsome: $selectsome\n");
$someset = mysqli_query($connection, $selectsome);
fwrite($log, "someset query made. memory used: ".memory_get_usage()."\n");
fwrite($log, "number of rows returned: ".mysqli_num_rows($someset)."\n");
while($row_some = mysqli_fetch_row($someset)) {
$selected[$row_some[0]] = $row_some[1];
}
mysqli_free_result($someset);
fwrite($log, "someset released, selected array loaded. memory used: ".memory_get_usage()."\n");
}
$results = NULL;
$getresults = "";
if ($outf == "ranked" || $outf == "graph") {
$getresults = "SELECT * FROM ".$correlationTable." WHERE correlation >= ".$bound. " ORDER BY correlation DESC";
}
else if ($outf == "pages") {
// first build a WHERE-IN string
$matchchunks = "";
$matchchunkcount = 0;
foreach($selected as $sk => $sv) {
if ($matchchunkcount > 0) {
$matchchunks = $matchchunks.",".$sk;
}
else {
$matchchunks = $sk;
$matchchunkcount = 1;
}
}
fwrite($log, "matchchunks = ".$matchchunks."\n");
// create temporary table pages
$removepagestemp = "DROP TEMPORARY TABLE IF EXISTS pages";
mysqli_query($connection, $removepagestemp);
$makepagestemp = "CREATE TEMPORARY TABLE pages (
correlation TEXT,
doc1 INT NOT NULL,
doc2 INT NOT NULL)";
mysqli_query($connection, $makepagestemp);
// load the temporary table
$getdoc1 = "INSERT INTO pages (correlation, doc1, doc2)
SELECT correlation, doc1, doc2 FROM ".$correlationTable." WHERE doc1 IN (".$matchchunks.")";
$getdoc2 = "INSERT INTO pages (correlation, doc1, doc2)
SELECT correlation, doc2, doc1 FROM ".$correlationTable." WHERE doc2 IN (".$matchchunks.")";
mysqli_query($connection, $getdoc1);
mysqli_query($connection, $getdoc2);
unset($matchchunks);
$getresults = "SELECT * FROM pages WHERE correlation >=".$bound." ORDER BY doc1, doc2";
}
$results = mysqli_query($connection, $getresults);
fwrite($log, "results query executed. memory used: ".memory_get_usage()."\n");
// load the chunk list so we can write out titles and files names for displaycorrs.php
$getchunksdata = "SELECT id, ctitle, alch FROM doc250_list";
$displayscript = "javascript:openViewer(\"ch250\", \"";
if ($frags == "ch1000") {
$getchunksdata = "SELECT id, ctitle, alch FROM doc1000_list";
$displayscript = "javascript:openViewer(\"ch1000\", \"";
}
$chunksdata = mysqli_query($connection, $getchunksdata);
$chunks = array();
$chunks[] = "base"; // fills that pesky $chunks[0] member
while ($outchunk = mysqli_fetch_array($chunksdata)) {
$chunks[] = $outchunk;
}
fwrite($log, "chunks name array initialized. memory used: ".memory_get_usage()."\n");
// now we produce the outputs and send them to the user
if ($outf == "ranked") {
$outputcount = 0;
echo "<table class='lsa-resultsTable'>";
while ($outrow = mysqli_fetch_row($results)) {
$new1 = $outrow[1];
$new2 = $outrow[2];
$writethis = 0;
if ($qs == "ALL") {
$writethis = 1;
}
elseif ($scope == "allcorrs") {
if (array_key_exists($new1, $selected) || array_key_exists($new2, $selected)) {
$writethis = 1;
}
}
elseif ($scope == "onlyselected") {
if (array_key_exists($new1, $selected) && array_key_exists($new2, $selected)) {
if ($selected[$new1] != $selected[$new2]) {
$writethis = 1;
}
}
}
elseif ($scope == "internal") {
if (array_key_exists($new1, $selected) && array_key_exists($new2, $selected)) {
// the user interface only allows one document/ALCH value when scope is "internal"
// so if both keys are in $selected, both belong to the chosen document.
//if ($selected[$new1] == $selected[$new2]) {
$writethis = 1;
//}
}
}
if ($writethis == 0) {
continue;
}
$corr = $outrow[0];
$item1_title = $chunks[$new1][1];
$item1_file = $chunks[$new1][0];
$item2_title = $chunks[$new2][1];
$item2_file = $chunks[$new2][0];
echo "<tr><td><a href ='".$displayscript.$item1_file."\", \"".$item2_file."\", \"".$corr."\")'>";
echo $corr;
echo "</a></td><td>".$item1_title."</td><td>".$item2_title."</td></tr>";
$outputcount++;
}
if ($outputcount == 0) {
echo "<tr><td>No results greater than or equal to ".$bound." were found.</td></tr>";
}
echo "</table>";
} // end of "ranked" jobs
elseif ($outf == "pages") {
echo "<table class='lsa-resultsTable'>";
while ($outrow = mysqli_fetch_row($results)) {
$new1 = $outrow[1];
$new2 = $outrow[2];
if ($selected[$new2] == $qs) {
continue;
}
$writethis = 0;
if (array_key_exists($new1, $selected) || array_key_exists($new2, $selected)) {
if ($selected[$new1] != $selected[$new2]) {
$writethis = 1;
}
}
if ($writethis == 0) {
continue;
}
$outputcount = 0;
$corr = $outrow[0];
$item1_file = $chunks[$new1][0];
$item1_title = $chunks[$new1][1];
$item2_file = $chunks[$new2][0];
$item2_title = $chunks[$new2][1];
echo "<tr><td width='200'>".$item1_title."</td><td width='200'>".$item2_title."</td>";
echo "<td><a href ='".$displayscript.$item1_file."\", \"".$item2_file."\", \"".$corr."\")'>";
echo $corr;
echo "</a></td></tr>";
$outputcount++;
}
if ($outputcount == 0) {
echo "<tr><td>No results greater than or equal to ".$bound." were found.</td></tr>";
}
echo "</table>";
}
elseif ($outf == "graph") {
fwrite($log, "Entered graph.\n");
$nodes = array();
$edges = array();
// hidden loop to create nwb files with all nodes showing---connected and unconnected
// we can invoke this ouselves to produce specialty graphs but suppress it otherwise
/*
$filler = 1;
foreach ($chunks as $chunkxx) {
$nodes[] = $filler;
$filler++;
}
*/
$graphstring = "_________<br>";
$outputcount = 0;
while ($outrow = mysqli_fetch_row($results)) {
//fwrite($log, "Graph loop".$outputcount.". Memory: ".memory_get_usage()."\n");
$new1 = $outrow[1];
$new2 = $outrow[2];
$writethis = 0;
if ($qs == "ALL") {
$writethis = 1;
}
elseif ($scope == "allcorrs") {
if (array_key_exists($new1, $selected) || array_key_exists($new2, $selected)) {
$writethis = 1;
}
}
elseif ($scope == "onlyselected") {
if (array_key_exists($new1, $selected) && array_key_exists($new2, $selected)) {
//if ($selected[$new1] != $selected[$new2]) {
$writethis = 1;
//}
}
}
elseif ($scope == "internal") {
if (array_key_exists($new1, $selected) && array_key_exists($new2, $selected)) {
//if ($selected[$new1] == $selected[$new2]) {
$writethis = 1;
//}
}
}
if ($writethis == 0) {
continue;
}
$corr = $outrow[0];
$item1_title = $chunks[$new1][1];
$item1_file = $chunks[$new1][2];
$item2_title = $chunks[$new2][1];
$item2_file = $chunks[$new2][2];
$newkey = combine_key($new1, $new2);
$edges[$newkey] = $corr;
if (!in_array($new1, $nodes)) {
$nodes[] = $new1;
}
if (!in_array($new2, $nodes)) {
$nodes[] = $new2;
}
$outputcount++;
}
if ($outputcount == 0) {
echo "No results greater than or equal to ".$bound." were found.<br/>";
}
else {
// we can write the graph
$newgraph = "graph-". $hash_value.".nwb";
$downloadpath = "graphs/$newgraph";
#$graph = fopen($graphfile, 'w');
$graph = fopen("graphs/$newgraph", 'w');
if ($graph == '' || $graph == 0) {
fwrite($log, "Can't open graph file.\n");
}
chmod($graph, 0666); # make sure the file is user/group writable.
fwrite($graph, '*Nodes'."\n");
fwrite($graph, 'id*int label*string docid*string lemmaid*string'."\n");
$graphstring = $graphstring.'*Nodes'."<br>";
$graphstring = $graphstring.'id*int label*string docid*string lemmaid*string'."<br>";
$nodeIdx = array();
$nodecounter = 1;
// first write all the nodes that were selected
foreach ($nodes as $node) {
$nodeIdx[$node] = $nodecounter;
fwrite($graph, $nodecounter.' "'.$chunks[$node][1].'" "'.$chunks[$node][2].'" "z2z"'."\n");
$graphstring = $graphstring.$nodecounter.' "'.$chunks[$node][1].'" "'.$chunks[$node][2].'" "z2z"'."<br>";
$nodecounter++;
}
fwrite($graph, "*UndirectedEdges\n");
fwrite($graph, "source*int\ttarget*int\tweight*float");
$graphstring = $graphstring."*UndirectedEdges<br>";
$graphstring = $graphstring."source*int##target*int##weight*float";
foreach($edges as $bigkey => $edgecorr) {
$ekey2 = extract_key2($bigkey);
$ekey1 = extract_key1($bigkey, $ekey2);
fwrite($graph, "\n".$nodeIdx[$ekey1]."\t".$nodeIdx[$ekey2]."\t".$edgecorr);
$graphstring = $graphstring."<br>".$nodeIdx[$ekey1]."##".$nodeIdx[$ekey2]."##".$edgecorr;
}
fclose($graph);
fwrite($log, "Closed graph file.\n");
$graphstring = $graphstring."<br>___________";
// change the permissions for the new graph file
chmod("graphs/$newgraph", 0644);
echo "<br/><br/>
<small>(Right-click the link and use <em>Save link as...</em> to get a copy for viewing in Network Workbench or an editor.)</small>
<br/><br/>
<table class='lsa-resultsTable'>
<tr>
<td>NWB network graph file:</td><td><a href='".$downloadpath."'>Link for download.</a> Give it a new name.</td>
</tr>
</table>";
}
}
// all finished
unset($selected);
unset($chunks);
mysqli_free_result($results);
mysqli_close($connection);
fwrite($log, "arrays and results freed, connection closed. memory used: ".memory_get_usage()."\n");
fclose($log);
return;
?>