-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.php
executable file
·343 lines (289 loc) · 10.5 KB
/
search.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
<html>
<head>
<title>The <a>constituent</a> Project | Search</title>
<style type="text/css">
.dist {
border: 1px solid black;
display: inline;
padding: 0px;
}
.dist a {
text-decoration: none;
}
html {
-moz-tab-size: 2;
}
a {
color: #000;
}
.match {
color: red;
font-weight: bold;
}
.content a {
color: #00e; /* default blue */
}
.searchbox {
float: left;
border: 1px solid #ccc;
border-radius: 5px;
margin-left: 10px;
padding: 10px;
color: #999;
}
.searchbox h4 {
margin: 0px;
}
#searchboxes {
overflow: auto;
}
.searchbox.active {
color: #000;
border-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script>
jQuery(function($){
$('.searchbox').mouseover(function() {
$(this).addClass('active');
});
$('.searchbox').mouseout(function() {
if (!$(this).is(':focus'))
$(this).removeClass('active');
});
$('.searchbox input').bind('focus change', function() {
$(this).closest('.searchbox').addClass('active');
$(this).closest('.searchbox').siblings().removeClass('active');
});
$('.searchbox input').blur(function() {
$(this).closest('.searchbox').removeClass('active');
});
});
</script>
<?php
if ( stristr($_SERVER['HTTP_HOST'], 'mit.edu') !== false ):?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19567124-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php endif;?>
</head>
<body>
<?php
$time_start = microtime_float();
error_reporting( E_ALL );
ini_set('display_errors', true);
include_once('functions.php');
include_once('search_functions.php');
if (isset($_REQUEST['tables']) && !empty($_REQUEST['tables'])) {
$tables = $_REQUEST['tables'];
define("ENTRIES_TABLE", "hyperlinks_entries_" . $_REQUEST['tables']);
define("LINKS_TABLE", "hyperlinks_links_" . $_REQUEST['tables']);
} else {
$tables = '';
define("ENTRIES_TABLE", "hyperlinks_entries");
define("LINKS_TABLE", "hyperlinks_links");
}
$full = isset($_REQUEST['full']) ? $_REQUEST['full'] : 'ajax';
$constituency = isset($_REQUEST['constituency']) ? $_REQUEST['constituency'] : false;
$failure = isset($_REQUEST['failure']) ? $_REQUEST['failure'] : false;
$missing_node = isset($_REQUEST['missing_node']) ? $_REQUEST['missing_node'] : false;
$immediate_node = isset($_REQUEST['immediate_node']) ? $_REQUEST['immediate_node'] : false;
?>
<h3>Query syntax:</h3>
<ol>
<li>parentheses in queries are understood to match.</li>
<li><code>NODE</code> is a special wildcard for a syntactic node, and <code>WORD</code> is a special wildcard for a terminal. We understand + and *.</li>
<li>Node labels may be unspecified, e.g. <code>(NP ( few ) NODE+ )</code>.</li>
<li>Any space can be understood as whitespace, including line breaks.</li>
<li>< and > are understood to mean beginning and end of a link.</li>
<li>Enclosing an element in {} will 'capture' it and produce a table showing how often each word/phrase appears in the captured area.</li>
<li>TODO: think about domination relations?</li>
</ol>
<h3>Node label abbreviations</h3>
<p>The search recognizes node labels according to <a href='http://repository.upenn.edu/cgi/viewcontent.cgi?article=1603&context=cis_reports'>the Penn Treebank (PTB) standard</a>. However, to make my life easier, the following aliases have been implemented:</p>
<ul>
<li>N = NN, NNS, NNP, NNPS</li>
<li>A = JJ, JJR, JJS</li>
<li>D = DT, CD, PDT, but note that many quantifiers are unfortunately called JJ (adj) by PTB</li>
<li>P = IN</li>
<li>V = MD, VB, VBD, VBG, VBN, VBP, VBZ</li>
<li>ADV = RB, RBR, RBS</li>
</ul>
<div id="searchboxes">
<form method="get" id="syntaxsearch" class="searchbox <?php if (empty($constituency)) echo ' active';?>">
<h4>Search by syntactic query</h4>
<input type="hidden" name="tables" value="<?php echo $tables ?>"/>
<p>
<input type="search" name="query" id="query" placeholder="(NP (NP (DT the ) <(JJS WORD ) NODE+ )> NODE+ )" size="40"<?php
if (isset($_REQUEST['query']))
echo " value='" . htmlspecialchars($_REQUEST['query']) . "'";
?>>
</p>
<p>
Full (ajax) search?
<input type="radio" name="full" value="ajax"<?php
if ($full == 'ajax')
echo " checked";
?>>
Just (static) stats?
<input type="radio" name="full" value="stats"<?php
if ($full == 'stats')
echo " checked";
?>>
</p>
<p>
Output tree?
<input type="checkbox" name="tree"<?php
if (isset($_REQUEST['tree']))
echo " checked";
?>>
</p>
<input type="submit" value="Search!"/>
</form>
<form method="get" id="typesearch" class="searchbox <?php if (!empty($constituency)) echo ' active';?>">
<h4>Search by link status</h4>
<input type="hidden" name="tables" value="<?php echo $tables ?>"/>
<p>Status: <input type="radio" name="constituency" value="constituent"<?php if ($constituency == 'constituent') echo " checked";?>/><label for="constituent">constituent</label> <input type="radio" name="constituency" value="multiple_constituents"<?php if ($constituency == 'multiple_constituents') echo " checked";?>/> <label for="multiple_constituents">multiple constituents</label> <input type="radio" name="constituency" value="not_constituent"<?php if ($constituency == 'not_constituent') echo " checked";?>/> <label for="not_constituent">not constituent</label></p>
<p>Failure type: <select name="failure">
<option value=""></option>
<option value="missing_before"<?php if ($failure == 'missing_before') echo " selected";?>>before</option>
<option value="missing_after"<?php if ($failure == 'missing_after') echo " selected";?>>after</option>
<option value="missing_before_after"<?php if ($failure == 'missing_before_after') echo " selected";?>>before_after</option>
<option value="x_clausal"<?php if ($failure == 'x_clausal') echo " selected";?>>cross-clausal</option>
</select></p>
<p>Missing: <input type="search" name="missing_node"<?php if (!empty($missing_node)) echo " value='{$missing_node}'";?>/> Dominating: <input type="search" name="immediate_node"<?php if (!empty($immediate_node)) echo " value='{$immediate_node}'";?>/></p>
<p>
Output tree?
<input type="checkbox" name="tree"<?php
if (isset($_REQUEST['tree']))
echo " checked";
?>>
</p>
<input type="submit" value="Search!"/>
</form>
</div>
<h3>Results</h3>
<?php
$query = isset($_REQUEST['query']) && !empty($_REQUEST['query']) ?
$_REQUEST['query'] :
"(NP (NP (DT the ) <(JJS WORD ) NODE+ )> NODE+ )";
if ($full == 'stats') {
$pattern = new Pattern($query);
$results = $pattern->queryDb(-1);
echo "<h4>SQL:</h4>\n<code>" . $pattern->prep->queryString . "<br>";
echo "like is bound to " . $pattern->getLike() . "<br>";
echo "</code>\n\n";
$n = count($results);
echo "<p>$n/98527 total results.";
$pct = round($n*100/98527, 2);
echo "($pct%)</p>";
if (strpos($query, "{") !== false) {
echo "<table>";
foreach ($pattern->backrefs as $id => $instance) {
foreach ($instance as $match) {
echo "<tr><th>$id</th><td>" . implode('</td><td>', $match) . "</td></tr>\n";
}
}
echo "</table>";
}
$time_end = microtime_float();
$time = round($time_end - $time_start,2);
} else {
// ajax, including link status queries
?>
<script>
$(function() {
var tree = $('[name="tree"]').prop('checked');
var query = $('#query').val();
var tables = '<?php echo $tables; ?>';
// parameters for link status search:
var constituency = $('[name=constituency]:checked').val();
var failure = $('[name=failure]').val();
var missing = $('[name=missing_node]').val();
var immediate = $('[name=immediate_node]').val();
var results = $('#results');
var startTime = new Date();
var ajax = [];
var returned = 0;
var atATime = 500;
var expected = 100000/atATime;
var start = 0;
var resultsCount = 0;
function runAjax() {
if (ajax.length - returned < 5) {
start += atATime;
<?php if (empty($constituency)): ?>
ajax.push($.getJSON('search_ajax.php', {query: query, tree: true, end: start + atATime, start: start, tables: tables }, showResults));
<?php else: ?>
ajax.push($.getJSON('search_ajax.php', {constituency: constituency, failure: failure, missing: missing, immediate: immediate, tree: true, end: start + atATime, start: start, tables: tables }, showResults));
<?php endif; ?>
}
if (!(start < 100000))
return;
setTimeout(runAjax, 300); // at most one query per 300ms
}
runAjax();
window.runAjax = runAjax;
function showResults(json) {
returned++;
$('#status').text(Math.round((returned/expected) * 100) + '%');
if (!'results' in json)
return results.append('<p>error.</p>');
resultsCount += json.results.length;
$('#found').text(resultsCount + ' found');
$.each(json.results, function() {
var result = '<div id="' + this.id + '" class="result" data-treedata="' + escape(JSON.stringify(this.treedata)) + '"><h3>Entry <a href="display.php?entry=' + this.id + '">' + this.id + '</a>';
var id = this.id;
if (this.lid) {
result += ' (';
var links = [];
$.each(this.lid.split(','), function(i, val) {
links.push('<a href="display.php?entry=' + id + '&id=' + val + '">' + val + '</a>');
});
result += links.join(', ') + ')';
}
result += ' [<a href="http://metafilter.com/' + this.id + '/" target="_new">MeFi</a>]:</h3>';
if (tree) {
$.each(this.treedata, function() {
result += '<span class="tree"><img src="lib/phpsyntaxtree/stgraph.svg?data=' + escape(this) + '"/></span>';
});
} else {
result += '<span class="content">' + this.content + '</span>';
}
result += '</div>';
results.append(result);
});
$('#time').text(((new Date()) - startTime)/1000);
}
});
</script>
<?php
}
$time_end = microtime_float();
$time = round($time_end - $time_start,2);
echo "<p><span id='found'></span> (<span id='time'>$time</span> seconds, <span id='status'></span>)</p>";
?>
<div id="results">
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19567124-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html><?php
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}