summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/oktavia/tool/web/oktavia-search.jsx
blob: 22dc3f779bc70550a7632c8fcbe49f178bceb937 (plain)
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
import "oktavia.jsx";
import "binary-util.jsx";
import "query.jsx";
import "query-string-parser.jsx";
import "search-result.jsx";
import "style.jsx";
import "stemmer/stemmer.jsx";
import "console.jsx";


class _Result
{
    var title : string;
    var url : string;
    var content : string;
    var score : int;
    function constructor (title : string, url : string, content : string, score : int)
    {
        this.title = title;
        this.url = url;
        this.content = content;
        this.score = score;
    }
}

class _Proposal
{
    var options : string;
    var label : string;
    var count : int;
    function constructor (options : string, label : string, count : int)
    {
        this.options = options;
        this.label = label;
        this.count = count;
    }
}

class OktaviaSearch
{
    var _oktavia : Oktavia;
    static var _stemmer : Nullable.<Stemmer> = null;
    static var _instance : Nullable.<OktaviaSearch> = null;
    var _queryString : Nullable.<string>;
    var _queries : Query[];
    var _highlight : string;
    var _callback : Nullable.<function(:int, :int):void>;
    var _entriesPerPage : int;
    var _currentPage : int;
    var _result : SearchUnit[];
    var _proposals : Proposal[];
    var _currentFolderDepth : int;

    function constructor (entriesPerPage : int)
    {
        this._oktavia = new Oktavia();
        this._entriesPerPage = entriesPerPage;
        this._currentPage = 1;
        this._queryString = null;
        this._callback = null;
        OktaviaSearch._instance = this;
    }

    static function setStemmer(stemmer : Stemmer) : void
    {
        if (OktaviaSearch._instance)
        {
            OktaviaSearch._instance._oktavia.setStemmer(stemmer);
        }
        else
        {
            OktaviaSearch._stemmer = stemmer;
        }
    }

    function loadIndex (index : string) : void
    {
        if (OktaviaSearch._stemmer)
        {
            this._oktavia.setStemmer(OktaviaSearch._stemmer);
        }
        this._oktavia.load(Binary.base64decode(index));
        if (this._queryString)
        {
            this.search(this._queryString, this._callback);
            this._queryString = null;
            this._callback = null;
        }
    }

    function search (queryString : string, callback : function(:int, :int):void) : void
    {
        if (this._oktavia)
        {
            var queryParser = new QueryStringParser();
            this._queries = queryParser.parse(queryString);
            this._highlight = queryParser.highlight();
            var summary = this._oktavia.search(this._queries);
            if (summary.size() > 0)
            {
                this._result = this._sortResult(summary);
                this._proposals = [] : Proposal[];
                this._currentPage = 1;
            }
            else
            {
                this._result = [] : SearchUnit[];
                if (this._queries.length > 1)
                {
                    this._proposals = summary.getProposal();
                }
                else
                {
                    this._proposals = [] : Proposal[];
                }
                this._currentPage = 1;
            }
            callback(this.resultSize(), this.totalPages());
        }
        else
        {
            this._queryString = queryString;
            this._callback = callback;
        }
    }

    function resultSize () : int
    {
        return this._result.length;
    }

    function totalPages () : int
    {
        return Math.ceil(this._result.length / this._entriesPerPage);
    }

    function currentPage () : int
    {
        return this._currentPage;
    }

    function setCurrentPage (page : int) : void
    {
        this._currentPage = page;
    }

    function hasPrevPage () : boolean
    {
        return (this._currentPage != 1);
    }

    function hasNextPage () : boolean
    {
        return (this._currentPage != this.totalPages());
    }

    function pageIndexes () : string[]
    {
        var result = [] : string[];
        var total = this.totalPages();
        if (total < 10)
        {
            for (var i = 1; i <= total; i++)
            {
                result.push(i as string);
            }
        }
        else if (this._currentPage <= 5)
        {
            for (var i = 1; i <= 7; i++)
            {
                result.push(i as string);
            }
            result.push('...', total as string);
        }
        else if (total - 5 <= this._currentPage)
        {
            result.push('1', '...');
            for (var i = total - 8; i <= total; i++)
            {
                result.push(i as string);
            }
        }
        else
        {
            result.push('1', '...');
            for (var i = this._currentPage - 3; i <= this._currentPage + 3; i++)
            {
                result.push(i as string);
            }
            result.push('...', total as string);
        }
        return result;
    }

    function getResult () : _Result[]
    {
        var style = new Style('html');
        var start = (this._currentPage - 1) * this._entriesPerPage;
        var last = Math.min(this._currentPage * this._entriesPerPage, this._result.length);
        var metadata = this._oktavia.getPrimaryMetadata();
        var num = 250;

        var results = [] : _Result[];

        for (var i = start; i < last; i++)
        {
            var unit = this._result[i];
            var info = metadata.getInformation(unit.id).split(Oktavia.eob);

            var offset = info[0].length + 1;
            var content = metadata.getContent(unit.id);
            var start = 0;
            var positions = unit.getPositions();
            if (content.indexOf(info[0]) == 1)
            {
                content = content.slice(info[0].length + 2, content.length);
                start += (info[0].length + 2);
            }
            var end = start + num;
            var split = false;
            if (positions[0].position > end - positions[0].word.length)
            {
                end = positions[0].position + Math.floor(num / 2);
                split = true;
            }
            for (var j = positions.length - 1; j > -1; j--)
            {
                var pos = positions[j];
                if (pos.position + pos.word.length < end)
                {
                    content = [
                        content.slice(0, pos.position - start),
                        style.convert('<hit>*</hit>').replace('*', content.slice(pos.position - start, pos.position + pos.word.length - start)),
                        content.slice(pos.position + pos.word.length - start, content.length)
                    ].join('');
                }
            }
            var text : string;
            if (split)
            {
                text = [
                    content.slice(0, Math.floor(num / 2)) + ' ...',
                    content.slice(-Math.floor(num / 2), end - start)].join('<br/>');
            }
            else
            {
                text = content.slice(0, end - start) + ' ...<br/>';
            }
            text = text.replace(Oktavia.eob, ' ').replace(/(<br\/>)(<br\/>)+/, '<br/><br/>');
            results.push(new _Result(info[0], info[1], text, unit.score));
        }
        return results;
    }

    function getHighlight () : string
    {
        return this._highlight;
    }

    function getProposals () : _Proposal[]
    {
        var style = new Style('html');
        var results = [] : _Proposal[];

        if (this._queries.length > 1)
        {
            for (var i = 0; i < this._proposals.length; i++)
            {
                var proposal = this._proposals[i];
                if (proposal.expect > 0)
                {
                    var label = [] : string[];
                    var option = [] : string[];
                    for (var j = 0; j < this._queries.length; j++)
                    {
                        if (j != proposal.omit)
                        {
                            label.push(style.convert('<hit>' + this._queries[j].toString() + '</hit>'));
                            option.push(this._queries[j].toString());
                        }
                        else
                        {
                            label.push(style.convert('<del>' + this._queries[j].toString() + '</del>'));
                        }
                    }
                    results.push(new _Proposal(option.join(' '), label.join('&nbsp;'), proposal.expect));
                }
            }
        }
        return results;
    }

    function _sortResult (summary : SearchSummary) : SearchUnit[]
    {
        for (var i = 0; i < summary.result.units.length; i++)
        {
            var score = 0;
            var unit = summary.result.units[i];
            for (var pos in unit.positions)
            {
                var position = unit.positions[pos];
                if (this._oktavia.wordPositionType(position.position))
                {
                    score += 10;
                }
                else
                {
                    score += 1;
                }
                if (!position.stemmed)
                {
                    score += 2;
                }
            }
            unit.score = score;
        }
        return summary.getSortedResult();
    }
}

class _Main
{
    static function main(args : string[]) : void
    {
    }
}