-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpoc.html
552 lines (492 loc) · 18.8 KB
/
poc.html
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<html><head><title>Dictionary interface - POC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body { width: 100%; background-color: #DDD; }
body, div { margin: 0; padding: 0; }
/*
a { color: black; text-decoration: none; }
*/
.button {
width: 100%; background-color: silver;
font-size: xx-large; text-align: center;
padding-top: 10px; padding-bottom: 10px;
}
hr { margin: 5px; }
.c_word {
border: 1px silver solid;
}
.arabic {
direction: rtl;
}
</style>
<meta name='viewport' content='width=device-width'/>
</head>
<body>
<!--
http://www.openjs.com/scripts/events/keyboard_shortcuts/
-->
<script type="text/javascript" src="jslib/jquery.js"></script>
<script type="text/javascript" src="jslib/jquery.ba-hashchange.min.js"></script>
<script type="text/javascript" src="jslib/shortcut.js"></script>
<script type="text/javascript" src="jslib/xregexp-all-min.js"></script>
<script type="text/javascript" src="jsastem.js"></script>
<!--
<script type="text/javascript" src="aa-ll-data.js"></script>
<script type="text/javascript" src="aa-ls-data.js"></script>
-->
<textarea rows=10 cols=100 id=debug style='height: auto; float: bottom; bottom: 0; position: fixed; background-color: #ffd; z-index: 999;'>
</textarea>
Type here to search for an arabic root:
<input type=text id='mainsearch' size=40 value="وكر"></input>
<div id='maindiv'>
<div id=header style=" font-size: larger; float: right; ">
The Word
</div>
<div id='maincontent'>
</div>
</div>
<div id=text style=' display: none;'>
This is some text. Try clicking on a word.
كتب ضرب ملك شرح نصح كور وكر
موضعه
روه ريغ
</div>
<hr>
<div id='main'>
</div>
<hr>
<div id='template' style='display: none;'>
<span class=close_layer style='float: right; border: 2px solid black; padding: 20px; '>X</span>
<span class=choice style='font-size: larger;'>choice here</span>
<span class=text>text here</p>
</div>
<h4>About</h4>
Browser compatibility:<p>
<table>
<tr><th>Browser <th>works online<th>offline<th>Notes</tr>
<tr><td>Opera (desktop) <td>yes <td>yes but:<td>For offline use you need to enable the following:
<a href="opera:config#UserPrefs|AllowFileXMLHttpRequest">opera:config#UserPrefs|AllowFileXMLHttpRequest</a>
<tr><td>Firefox (desktop) <td>yes <td>yes <td>Works by default
<tr><td>Chrome <td>yes <td>yes but:<td>For desktop use, chrome must be run with the following parameter: --allow-file-access-from-files (put this after chrome.exe or google-chrome on the commandline if you're running windows or linux respectively)
</tr></table>
</p>
<script>
var debug_msg = 0;
var current_layer = 0;
var layer_colors = [ 'pink', "blue", "yellow", "green", "red", "purple"];
var layer_bgcolors = [ 'black', "#ddf", "#ffd", "#dfd" , "#fdd", "#fdf"];
/*
layers[0][0] = '0_32'; // <- this the $layer_$wordcount of the word that has been clicked in the particular layer
layers[0][1] = 'blah'; // <- This is the actual word that's being displayed in the layer on top, as it may be (slightly) different
layers[1][0] = '1_32';
layers[1][1] = 'nice';
This then becomes the following hash in the window bar for bookmarking, sharing and navigation use.
http://the.url/page.html#0_32,blah,1_32,nice
*/
var layers = new Array();
var dictionaries = new Array();
// main()
// Load initial test data:
$("#main").text( $("#text").text() );
$('#mainsearch').keypress(function(e)
{
code= (e.keyCode ? e.keyCode : e.which);
if (code == 13) set_main_search();
});
spanify_text( '#main', current_layer);
parse_hash();
//window.location.hash = "#";
// Bind the event.
$(window).hashchange( function(){
// Alerts every time the hash changes!
//alert( location.hash );
parse_hash();
})
// ========= End of main() =========
// Only subroutines below. Beware!
function set_main_search() {
var dict_contents = get_dict_contents( $('#mainsearch').val(), "lisan");
$('#maincontent').html( dict_contents.html);
spanify_text( '#maincontent', 0);
}
function register_dictionary( index, shortname, displayname, dirname, description) {
//debug( name);
//debug( contents);
dictionaries[dictionaries.length] = {
"shortname": shortname, "displayname": displayname,
"dirname": dirname, "index": index, "description": description };
//debug( dictionaries);
}
function set_layer( word, zindex) {
layers[zindex] = []; //empty or create
layers[zindex][0] = $(word).attr('id');
var stemmed_word = jsastem( $(word).text());
debug( 'Stem result: ' + $(word).text() + ' -> ' + stemmed_word);
layers[zindex][1] = stemmed_word
//layers[zindex][2] = stem($(word).text());
for (i = layers.length; i > zindex; i--) {
layers.splice( i, 1);
$("#" + "layer_" + i).remove();
debug("#" + "layer_" + i);
}
build_hash();
}
function build_hash() {
var str = "";
for (i = 0; i < layers.length; i++) {
if (i in layers)
str += layers[i][0] + '=' + layers[i][1] + ',';
}
// slice of last ','
str = str.slice(0,-1);
console.log(str);
debug(str);
// This triggers parse_hash() automatically
window.location.hash = "#" + str;
}
function parse_hash() {
var hashstring = window.location.hash.replace(/^#/,'');
var i, variables = hashstring.split(',');
if (variables.length > 0 && variables[0].indexOf('=') > 2) {
// Variables present in hash, they represent the visible layers
for (i = 0; i < variables.length; i++) {
keyValuePair = variables[i].split('=');
//alert( "Var found: " + keyValuePair[0] + ' = ' + keyValuePair[1] );
var clicked_word = keyValuePair[0];
var word_on_top = keyValuePair[1];
if ( clicked_word !== undefined && word_on_top !== undefined) {
debug( "Var found: " + keyValuePair[0] + ' = ' + keyValuePair[1] );
//debug( 'create_layer(' + (i + 1) + clicked_word +', '+ word_on_top );
layers[i] = [];
layers[i][0] = clicked_word;
layers[i][1] = word_on_top;
create_layer( i + 1, clicked_word, word_on_top);
} else {
debug( "Malformed content in hash found: " + variables[i]);
}
}
}
else {
debug ('No variables in the hash found');
}
debug(layers);
}
function create_layer( zindex, clicked_word, word_on_top ) {
// If any info is found for word_on_top then continue, else show some sort of warning
var dict_contents = get_dict_contents( word_on_top, "lisan");
if ( dict_contents.hits == 0) {
debug('No content whatsoever found for: ' + word_on_top);
$('#' + clicked_word).css('background-color', 'white');
$('#' + clicked_word).css('border', '1px solid black');
} else {
layer_contents = dict_contents.html;
//debug( zindex + ' ' + clicked_word + ' ' + word_on_top + ' ' + layer_colors[zindex]);
$('#' + clicked_word).siblings().css('background-color', 'inherit');
$('#' + clicked_word).siblings().css('border', 'none');
$('#' + clicked_word).css('background-color', layer_bgcolors[zindex]);
$('#' + clicked_word).css('border', '1px solid ' + layer_colors[zindex]);
//debug( stem( $('#'+clicked_word).text()) );
var new_layer = "layer_" + zindex;
$('body').append("<div id=" + new_layer + "></div>");
$("#" + new_layer).html($("#template").html());
$("#" + new_layer + " span.text" ).html( layer_contents);
$("#" + new_layer + " span.text" ).addClass( 'arabic');
$("#" + new_layer + " span.choice" ).text( word_on_top);
$("#" + new_layer + " span.choice" ).css( 'border', '3px solid ' + layer_colors[zindex]);
spanify_text( "#" + new_layer + " span.text", zindex);
$("#" + new_layer + " span.close_layer" ).click(function() {
//alert("Handler for .click() called.");
$('#' + clicked_word).css('background-color', 'inherit');
$('#' + clicked_word).css('border', 'none');
//console.log( layers);
//console.log( layers);
debug(layers);
for (i = layers.length; i >= zindex - 1; i--) {
layers.splice( i, 1);
$("#" + "layer_" + i).remove();
debug("#" + "layer_" + i);
}
debug(layers);
build_hash();
//console.log( layers);
});
//onClick="javascript:close_layer(this,1)"
var cssObj = {
'position' : 'absolute',
'background-color' : layer_bgcolors[zindex],
'border' : '1px solid ' + layer_colors[zindex],
'border-top' : '3px solid ' + layer_colors[zindex],
'z-index' : zindex,
'width' : '99%',
'top' : $('#' + clicked_word).offset().top + $('#' + clicked_word).height() * 3
}
$("#" + new_layer).css(cssObj);
}
/*
'color' : 'rgb(0,40,244)',
*/
}
function get_dict_contents( word, which_dictionaries) {
// Choose which dictionaries you want/don't want by setting which_dictionaries to something
// Returned html should be a concatenation of filled out templates
var html_to_return = "";
var hits = 0;
for ( i = 0; i < dictionaries.length; i++) {
html_to_return += '<b>' + dictionaries[i]["displayname"] + '</b><p>';
if ( $.inArray( word, dictionaries[i]["index"]) > -1 ) {
hits++;
var path = dictionaries[i]["dirname"] + '/' + word.substr(0,1) + '/' + word + '.txt';
debug( path);
html_to_return +=
$.ajax({
url: path,
type: 'get',
dataType: 'text',
async: false,
success: null
}).responseText;
} else {
debug( "'" + word + "' not found in dictionary: " + dictionaries[i]["shortname"]);
html_to_return += '(root not found in this dictionary)';
}
/*
debug(i + ': ' + dictionaries[i]["shortname"]);
html_to_return += '<b>' + dictionaries[i]["displayname"] + '</b><br>'
+ $.ajax({
url: path,
type: 'get',
dataType: 'text',
async: false,
success: null
}).responseText;
$.get( path, function(data){ html_to_return = data; }) ;
debug( "method 1: " + html_to_return);
html_to_return = "";
$.ajax({
url: path,
type: 'get',
dataType: 'text',
async: false,
success: function(data) {
html_to_return = data;
}
});
debug( "method 2: " + html_to_return);
html_to_return = "";
html_to_return = $.ajax({
url: path,
type: 'post',
dataType: 'text',
async: false,
success: function(data) {
//html_to_return = data;
}
}).responseText;
debug( "method 4: " + html_to_return);
*/
}
debug( 'hits: ' + hits);
html_to_return = html_to_return.replace( /\n/g, '<br>');
return {
'hits': hits,
'html': html_to_return
};
}
function spanify_text( id, source_layer) {
//debug( 'id: ' + id + ' sl: ' + source_layer);
var word_count = 0;
target = source_layer + 1;
// Works for westerns \w+ only:
//$(id).html().replace( /([^<>\s:;,\.\(\)\{\}\[\]]+)/g, function( word) {
$(id).html(
$(id).html().replace( XRegExp('[\\p{Arabic}\\p{M}]+', 'g'), function( word) {
return '<span class=span_' + source_layer + ' id="' + source_layer + '_' + ++word_count + '" '
+ 'onClick="javascript:set_layer(this,'+source_layer+')">' + word + '</span>';
//+ 'onClick="javascript:set_layer(this,'+target+')">' + word + '</span>';
})
);
//if ( target==2) $(id).html("BLAH:<i>italic</i>");
/*
$(id).html(
XRegExp.forEach( $("#input").text(), XRegExp('[\\p{Arabic}\\p{M}]+'), function(match, i) {
//if (i % 2) this.push(+match[0]);
//debug(match);
stem(match[0]);
}, []);
);
$(id + ' span').click(function() {
//alert("Handler for .click() called.");
//debug(target);
setup_layer( this, target);
//$("#" + new_layer).remove();
});
$(id + ' span').css( 'border', '1px solid silver');
var text = $(id).text();
var pattern = /(\w+)/g;
var match;
while (match = pattern.exec(text)) {
match = 'T';
}
//$(id).text().replace(/(\w+)/g, '<span class=span_' + source_layer + ' onClick="javascript:setup_layer(this,'+target+')">$1</span>' )
//$(id).text().replace(/(\w+)/g, '<span class=span_' + source_layer + ' onClick="javascript:setup_layer(this,'+target+')">$1</span>' )
$(id).html( text);
//$(id + ' span').prepend(source_layer+':');
//alert("test");
var words = $("#text").text().split(/[\s,]+/);
//$("p").empty();
$.each(words, function(i, v) {
$("#main").append(
"<span class=c_word onClick=javascript:do_something(this)>" + v + "</span> "
);
});
*/
}
function debug( obj) {
debug_msg++;
$('#debug').prepend( '#' + debug_msg + ' ' + dump(obj) + "<br>");
//console.log( str);
}
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
* The level - OPTIONAL
* Returns : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
* Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Strings/Chars/Numbers etc.
dumped_text = "("+typeof(arr)+")("+arr.length+") => "+arr+" <=\n";
}
return dumped_text;
}
/*
//See jsastem.js for this now
function stem( input) {
var showdebug = 1;
var stem = input;
var candidate_roots = [];
// Stemming step 1. Strip diacritics
var stem = XRegExp.replace( stem, XRegExp('\\p{M}', 'g'), '');
debug( 'step 1. : ' + input + ' -> ' + stem);
// Stemming step 2. remove length three and length two prefixes/suffixes in this order
if (stem.length >= 6) { stem = stem.replace( /^(كال|بال|ولل|وال)(.*)$/i, "$2"); }
if (stem.length >= 5) { stem = stem.replace( /^(ال|لل)(.*)$/i, "$2"); }
debug( 'step 2. : ' + stem);
// Stemming step 3. remove length three and length two suffixes in this order
if (stem.length >= 6) { stem = stem.replace( /^(.*)(تما|هما|تان|تين|كما)$/i, "$1"); }
if (stem.length >= 5) { stem = stem.replace( /^(.*)(ون|ات|ان|ين|تن|كم|هن|نا|يا|ها|تم|كن|ني|وا|ما|هم)$/i, "$1"); }
debug( 'step 3. : ' + stem);
// Stemming step 4. remove initial waw if found
if (stem.length >= 4) { stem = stem.replace( /^وو/i, 'و'); }
debug( 'step 4. : ' + stem);
// Stemming step 5. normalize initial hamza to bare alif
if (stem.length >= 4) { stem = stem.replace( /^[آأإ]/i, 'ا'); }
debug( 'step 5. : ' + stem);
if (stem.length <= 3) {
return stem;
}
// Stemming step 6. process length four patterns and extract length three roots
if (stem.length == 6) {
stem = stem.replace( /^[ام]ست(...)$/i, "$1"); // مستفعل - استفعل
stem = stem.replace( /^[ام]ست(...)$/i, "$1"); // مستفعل - استفعل
stem = stem.replace( /^[تم](.)ا(.)ي(.)$/i, "$1$2$3"); // تفاعيل - مفاعيل
stem = stem.replace( /^م(..)ا(.)ة$/i, "$1$2"); // مفعالة
stem = stem.replace( /^ا(.)[تط](.)ا(.)$/i, "$1$2$3"); // افتعال
stem = stem.replace( /^ا(.)(.)و\2(.)$/i, "$1$2$3"); // افعوعل
if (stem.length == 3 ) { return stem; }
else {
stem = stem.replace( /[ةهيكتان]$/i, ""); // single letter suffixes
//if (stem.length == 4 ) { TODO: initiate 4 letter word routine? }
//if (stem.length == 5 ) { TODO: initiate 5 letter word routine? }
stem = stem.replace( /^(..)ا(..)$/i, "$1$2"); // فعالل
stem = stem.replace( /^ا(...)ا(.)$/i, "$1$2"); // افعلال
stem = stem.replace( /^مت(.۔..)$/i, "$1"); // متفعلل
stem = stem.replace( /^[لبفسويتنامك]/i, ""); // single letter prefixes، added م for مفعلل
if (stem.length == 6 ) {
stem = stem.replace( /^(..)ا(.)ي(.)$/i, "$1$2$3"); // فعاليل
}
//return stem;
}
}
debug( 'after length 6 : ' + stem);
if (stem.length == 5) {
stem = stem.replace( /^ا(.)[اتط](.)(.)$/i, "$1$2$3"); // افتعل - افاعل
stem = stem.replace( /^م(.)(.)[يوا](.)$/i, "$1$2$3"); // مفعول - مفعال - مفعيل
stem = stem.replace( /^[اتم](.)(.)(.)ة$/i, "$1$2$3"); // مفعلة - تفعلة - افعلة
stem = stem.replace( /^[يتم](.)[تط](.)(.)$/i, "$1$2$3"); // مفتعل - يفتعل - تفتعل
stem = stem.replace( /^[تم](.)ا(.)(.)$/i, "$1$2$3"); // مفاعل - تفاعل
stem = stem.replace( /^(.)(.)[وا](.)ة$/i, "$1$2$3"); // فعولة - فعالة
stem = stem.replace( /^[ما]ن(.)(.)(.)$/i, "$1$2$3"); // انفعل - منفعل
stem = stem.replace( /^ا(.)(.)ا(.)$/i, "$1$2$3"); // افعال
stem = stem.replace( /^(.)(.)(.)ان$/i, "$1$2$3"); // فعلان
stem = stem.replace( /^ت(.)(.)ي(.)$/i, "$1$2$3"); // تفعيل
stem = stem.replace( /^(.)ا(.)و(.)$/i, "$1$2$3"); // فاعول
stem = stem.replace( /^(.)وا(.)(.)$/i, "$1$2$3"); // فواعل
stem = stem.replace( /^(.)(.)ائ(.)$/i, "$1$2$3"); // فعائل
stem = stem.replace( /^(.)ا(.)(.)ة$/i, "$1$2$3"); // فاعلة
stem = stem.replace( /^(.)(.)ا(.)ي$/i, "$1$2$3"); // فعالي
if (stem.length == 3 ) { return stem; }
else {
stem = stem.replace( /^[اتم]/i, ""); // تفعلل - افعلل - مفعلل
stem = stem.replace( /[ةهيكتان]$/i, ""); // single letter suffixes
//if (stem.length == 4 ) { TODO: initiate 4 letter word routine? }
stem = stem.replace( /^(..)ا(..)$/i, "$1$2"); // فعالل
stem = stem.replace( /^(...)ا(.)$/i, "$1$2"); // فعلال
stem = stem.replace( /^[لبفسويتنامك]/i, ""); // single letter prefixes، added م for مفعلل
//return stem;
}
}
debug( 'after length 5 : ' + stem);
if (stem.length == 4) {
stem = stem.replace( /^م(.)(.)(.)$/i, "$1$2$3"); // مفعل
stem = stem.replace( /^(.)ا(.)(.)$/i, "$1$2$3"); // فاعل
stem = stem.replace( /^(.)(.)[يوا](.)$/i, "$1$2$3"); // فعال - فعول - فعيل
stem = stem.replace( /^(.)(.)(.)ة$/i, "$1$2$3"); // فعلة
if (stem.length == 3 ) { return stem; }
else {
stem = stem.replace( /^(.)(.)(.)[ةهيكتان]$/i, "$1$2$3"); // single letter suffixes
if (stem.length == 3 ) { return stem; }
stem = stem.replace( /^[لبفسويتناك](.)(.)(.)$/i, "$1$2$3"); // single letter prefixes
//return stem;
}
}
debug( 'after length 4 : ' + stem);
/*
TODO
var matches = new array();
var possible_roots = new array();
if ( matches = stem.match( /(.)ئ(.)/)) {
possible_roots[] = $matches[0] + 'و' + $matches[1];
possible_roots[] = $matches[0] + 'ي' + $matches[1];
}
*/
/*
Filter candidate_roots through known possible roots
*/
return stem
}
*/
</script>
<!-- now try to load all the dictionaries -->
<script type="text/javascript" src="lisan.js"></script>
</body>
</html>