-
Notifications
You must be signed in to change notification settings - Fork 26
/
demo.tt
99 lines (94 loc) · 2.97 KB
/
demo.tt
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
[% header %]
<noscript>
<div class="alert alert-warning">
Sorry, this demo requires Javascript to function.
</div>
</noscript>
<p>
Try the
[% stemmer_select %]
stemming algorithm:
</p>
<table border='0'><tr><td width="50%">
<textarea id="words" rows="1" cols="50" placeholder="Enter some English text" oninput="stem_demo(this);">
</textarea>
</td>
<td id="results" class="license" style='border:1px;border-style:solid;'></td>
</tr></table>
<p>
This demo performs stemming entirely within your browser, using Javascript code generated by the Snowball compiler.
</p>
[% FOREACH js IN stemmer_js_files %]
<script language="javascript" src="[% js %]"></script>
[% END %]
<script>
var old_words;
function stem_demo(elt) {
var words = elt.value;
if (words === old_words) return;
old_words = words;
// Auto-expand height of the textarea
var lines = 1;
words.replace(/\n/g, function(){++lines;});
elt.rows = lines;
var lang = document.getElementById('lang').value;
if (lang.search(/[^-A-Za-z0-9_]/) >= 0) return;
lang += "Stemmer";
var s = eval("new "+lang+"()");
var result = '';
var i = 0;
for (const m of words.matchAll(/([[% word_regexp %]']+)/ug)) {
result += words.substring(i, m.index).replace(/[ &<>\n]/g, function(c){
if (c === ' ') return ' ';
if (c === '<') return '<';
if (c === '>') return '>';
if (c === '&') return '&';
// Zero width space means a trailing newline will get rendered.
return '<br>​';
});
const w = m[0];
result += '<span style="background-color:#dfd" title="' + lang + '(' + w + ')">' + s.stemWord(w.toLowerCase()) + '</span>';
i = m.index + w.length;
}
document.getElementById('results').innerHTML = result;
}
function lang_changed(elt) {
old_words = undefined;
var lang = elt.value;
var words_elt = document.getElementById('words');
var results_elt = document.getElementById('results');
if (lang === 'Arabic' || lang === 'Yiddish') {
words_elt.dir = results_elt.dir = 'rtl';
} else {
if (lang === 'Porter') lang = 'English';
words_elt.dir = results_elt.dir = 'ltr';
}
words_elt.placeholder = "Enter some " + lang + " text";
stem_demo(document.getElementById('words'));
}
window.onload = function() {
let d=location.hash;
if (d.length) {
let c=null;
let s=document.getElementById('lang');
let a=(s.selectedIndex>=0?s.options[s.selectedIndex]:null);
d = d.substring(1);
for (let i=0; i<s.options.length; i++) {
let e = s.options[i];
if (e.getAttribute("value") === d) c = e;
}
if (c !== null && c !== a) {
if (a !== null) a.removeAttribute("selected");
c.setAttribute("selected", "");
s.onchange();
}
}
let t=(new URLSearchParams(location.search.substring(1))).get("text");
if (t !== null) {
let s=document.getElementById('words');
s.value=t;
stem_demo(s);
}
}
</script>
[% footer %]