-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlufz.php
283 lines (259 loc) · 7.61 KB
/
xlufz.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
<?php
/**
* Xlufz is simple PHP-based web service that can highlight selected words
* on a web page. Only certain web pages are supported (but you can modify
* that for your own use). Xlufz is used by the free and open source Exet
* (http://exet.app) web app for crossword construction, to allow setters
* to see cryptic indicators etc. relevant to the clue surface that they
* may be toying with.
*
* Word selection is done via the excellent and versatile Datamuse words
* API (https://www.datamuse.com/api/).
*
* You specify the web page via the "srcurl" parameter. All other parameters
* are passed over to the Datamuse words API for word selection.
*/
/**
* PHP 7 does not have str_(starts,ends}_with(). We implement my_* versions.
*/
function my_str_starts_with($haystack, $needle) {
$l = strlen($needle);
$sub = substr($haystack, 0, $l);
return ($sub == $needle);
}
function my_str_ends_with($haystack, $needle) {
$l = strlen($needle);
$sub = substr($haystack, strlen($haystack) - $l, $l);
return ($sub == $needle);
}
/**
* Input: text free from tags.
* Output: tokens, array of alphabetic tokens and all non-alphabetic character
* tokens.
*/
function tokenize($text, &$tokens) {
$l = strlen($text);
$tokenStart = 0;
for ($i = 0; $i < $l; $i++) {
$c = strtolower($text[$i]);
if (($c < 'a') || ($c > 'z')) {
if ($i > $tokenStart) {
array_push($tokens, substr($text, $tokenStart, $i - $tokenStart));
}
array_push($tokens, $text[$i]);
$tokenStart = $i + 1;
}
}
if ($l > $tokenStart) {
array_push($tokens, substr($text, $tokenStart, $l - $tokenStart));
}
}
$xlufzStopWords = array_fill_keys(
array(
'am', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'for', 'from', 'how',
'if', 'in', 'is', 'it', 'its', 'of', 'or', 'than', 'that', 'the', 'their',
'theirs', 'them', 'then', 'these', 'they', 'this', 'those', 'to', 'was',
'were', 'what', 'when', 'where', 'which', 'who', 'whom', 'why', 'with',
'is', 'that', 'had', 'on', 'for', 'were', 'was'), 1);
/**
* Add key to keys array, unless it's a stop word.
*/
function add_if_not_stop_word($key, &$keys) {
if (strlen($key) <= 1) {
return;
}
global $xlufzStopWords;
if (array_key_exists($key, $xlufzStopWords)) {
return;
}
array_push($keys, $key);
}
/**
* Get an array of keys for a word/phrase.
*/
function get_keys($phrase) {
$keys = array();
$lcPhrase = strtolower($phrase);
global $xlufzStopWords;
if (array_key_exists($lcPhrase, $xlufzStopWords)) {
return $keys;
}
add_if_not_stop_word($lcPhrase, $keys);
$l = strlen($lcPhrase);
$suffixes = array('ed', 's', 'ing');
foreach ($suffixes as $suffix) {
$suffixLen = strlen($suffix);
if (my_str_ends_with($lcPhrase, $suffix)) {
$key = substr($lcPhrase, 0, $l - $suffixLen);
add_if_not_stop_word($key, $keys);
$key = substr($lcPhrase, 0, $l - $suffixLen - 1);
add_if_not_stop_word($key, $keys);
}
}
return $keys;
}
/**
* Populate an associative array (phraseMap) with phrases from the Datamuse
* API output.
*/
function makeMapOfPhrases($phrases, &$phraseMap) {
$num = count($phrases);
for ($i = 0; $i < $num; $i++) {
$phrase = $phrases[$i]['word'];
$keys = get_keys($phrase);
foreach ($keys as $key) {
$phraseMap[$key] = 1;
}
}
}
/**
* Match a token in phraseMap, allow matching some variants too.
*/
function tokenMatch($token, $phraseMap) {
$keys = get_keys($token);
foreach ($keys as $key) {
if (array_key_exists($key, $phraseMap)) {
return true;
}
}
return false;
}
/**
* In text, any tokens that match an entry in phraseMap will get surrounded
* by a highlighting span in the output of this function.
*/
function markPhrasesInText($text, $phraseMap) {
$output = '';
$tokens = array();
tokenize($text, $tokens);
foreach ($tokens as $token) {
if (tokenMatch($token, $phraseMap)) {
$output = $output . '<span class="xlufz">' . $token . '</span>';
} else {
$output = $output . $token;
}
}
return $output;
}
/**
* Apply markPhrasesInTxt() to the text portions of the input html.
* Do not make any modifications inside script and style tags.
*/
function markPhrasesInHtml($html, $phrases) {
$markedHtml = '';
$phraseMap = array();
makeMapOfPhrases($phrases, $phraseMap);
$l = strlen($html);
$textStart = 0;
$i = 0;
while ($i < $l) {
if ($html[$i] != '<') {
$i++;
continue;
}
if ($i > $textStart) {
$markedHtml = $markedHtml . markPhrasesInText(substr(
$html, $textStart, $i - $textStart), $phraseMap);
}
$closer = '>';
$piece = substr($html, $i, 7);
if (my_str_starts_with($piece, '<script')) {
$closer = '</script>';
} else if (my_str_starts_with($piece, '<style')) {
$closer = '</style>';
}
$closerPos = strpos($html, $closer, $i + 1);
$closerLen = strlen($closer);
if ($closerPos === false) {
$closerPos = $l - $closerLen;
}
$closerPos += $closerLen;
$markedHtml = $markedHtml . substr($html, $i, $closerPos - $i);
$textStart = $closerPos;
$i = $closerPos;
if (my_str_starts_with($piece, '<body')) {
$xlufzHeader =
"\n" .
"<style>\n" .
".xlufz {\n" .
" color: blue;\n" .
" font-weight: bold !important;\n" .
"}\n" .
"</style>\n";
$markedHtml = $markedHtml . $xlufzHeader;
}
}
if ($l > $textStart) {
$markedHtml = $markedHtml . markPhrasesInText(substr(
$html, $textStart, $l - $textStart), $phraseMap);
}
return $markedHtml;
}
/**
* Return the contents of url.
*/
function fetch_url($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
/* For local testing, if curl is not available: */
/*
return file_get_contents($url);
*/
}
/**
* Fetch the contents of the web page at the URL parameter "srcurl" and
* return it after highlighting words found in the output of the Datamuse
* API invoked using all other URL parameters. Only allow the listed
* URL roots.
*/
function xlufz() {
$allowedUrlRoots = array(
"https://cryptics.georgeho.org",
"https://www.crosswordunclued.com",
"https://www.highlightpress.com.au",
"https://en.wikipedia.org",
"https://longair.net"
);
$datamuseQuery = "";
$srcurl = "";
foreach($_GET as $key => $value) {
if ($key == "srcurl") {
$srcurl = $value;
continue;
}
$datamuseQuery = $datamuseQuery . "&" . $key . "=" . urlencode($value);
}
if ($srcurl == "") {
return "No srcurl parameter passed";
}
$rootSlashPos = strpos($srcurl, "/", 8);
if (!$rootSlashPos) {
return "srcurl parameter does not have a slash";
}
$srcurlRoot = substr($srcurl, 0, $rootSlashPos);
if (!in_array($srcurlRoot, $allowedUrlRoots)) {
return "srcurl root [" . $srcurlRoot . "] is not one that's allowed";
}
$lastSlashPos = strrpos($srcurl, "/");
$srcurlBase = substr($srcurl, 0, $lastSlashPos);
if ($lastSlashPos <= 8) {
$srcurlBase = $srcurl;
}
$origHtml = fetch_url($srcurl);
/* replace relative urls in img, script, and link tags */
$repl = "$1=$2" . $srcurlRoot . "/";
$origHtml = preg_replace("/( href| src)=(.)\//i", $repl, $origHtml);
$repl = "$1=$2" . $srcurlBase . "/$3";
$origHtml = preg_replace('/( href| src)=(.)([a-zA-Z][^:\.\/]*\.)/i',
$repl, $origHtml);
$datamuseUrl = "https://api.datamuse.com/words?" . $datamuseQuery;
$phrasesJSON = fetch_url($datamuseUrl);
$phrases = json_decode($phrasesJSON, true);
return markPhrasesInHtml($origHtml, $phrases);
}
echo xlufz();
?>