cjk_index 0.2.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +72 -0
- data/LICENSE-Unicode +39 -0
- data/NOTICE +17 -0
- data/README.md +193 -41
- data/data/ja-kyujitai.tsv +423 -292
- data/data/ko-hanyang-pua.tsv +5666 -0
- data/data/zh-hant-hans-safe.tsv +2774 -0
- data/exe/cjk_index +6 -0
- data/lib/cjk_index/bridgetown.rb +100 -0
- data/lib/cjk_index/builder.rb +250 -90
- data/lib/cjk_index/cli.rb +240 -0
- data/lib/cjk_index/index_file.rb +4 -0
- data/lib/cjk_index/jekyll.rb +23 -7
- data/lib/cjk_index/normalizer.rb +103 -18
- data/lib/cjk_index/postings.rb +218 -0
- data/lib/cjk_index/rake_task.rb +71 -0
- data/lib/cjk_index/runtime.js +243 -29
- data/lib/cjk_index/runtime.rb +1 -0
- data/lib/cjk_index/searcher.rb +102 -38
- data/lib/cjk_index/tokenizer.rb +30 -0
- data/lib/cjk_index/version.rb +1 -1
- data/lib/cjk_index.rb +1 -0
- metadata +16 -4
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CJKIndex
|
|
4
|
+
# The compact encoding of a posting list in format 4, shared by Builder
|
|
5
|
+
# (writing) and Searcher (reading); runtime.js decodes it the same way.
|
|
6
|
+
#
|
|
7
|
+
# A list is a sequence of entries, ordered by document and field. Each
|
|
8
|
+
# entry is written as numbers:
|
|
9
|
+
#
|
|
10
|
+
# key gap, n, first position, gap to the next position, ...
|
|
11
|
+
#
|
|
12
|
+
# where key = document * number of fields + field, and the key gap is the
|
|
13
|
+
# difference to the key of the previous entry minus 1 (the first entry
|
|
14
|
+
# counts from -1). n is the number of positions. Each number is written in
|
|
15
|
+
# printable ASCII: the last digit in base 60 from the first 60 characters
|
|
16
|
+
# of ALPHABET, the digits before it in base 30 from the other 30, most
|
|
17
|
+
# significant first. 0..59 take one character, up to 1,799 two, up to
|
|
18
|
+
# 53,999 three. ALPHABET has neither " nor \, so a list is a JSON string
|
|
19
|
+
# without escapes.
|
|
20
|
+
module Postings
|
|
21
|
+
ALPHABET = ((0x23..0x7d).map(&:chr) - ["\\"]).join.freeze
|
|
22
|
+
LAST = 60
|
|
23
|
+
LEAD = 30
|
|
24
|
+
# The characters that end a number are the bytes below LEAD_BYTE
|
|
25
|
+
# (FINAL_BYTES in the notation of String#count).
|
|
26
|
+
LEAD_BYTE = ALPHABET.getbyte(LAST)
|
|
27
|
+
FINAL_BYTES = "#-[\\]-_"
|
|
28
|
+
# byte => digit; 0...60 ends a number, 60...90 leads.
|
|
29
|
+
DIGIT = Array.new(256, -1).tap { |t| ALPHABET.each_byte.with_index { |b, i| t[b] = i } }.freeze
|
|
30
|
+
|
|
31
|
+
def self.number_slow(value)
|
|
32
|
+
s = ALPHABET[value % LAST]
|
|
33
|
+
value /= LAST
|
|
34
|
+
while value.positive?
|
|
35
|
+
s = ALPHABET[LAST + (value % LEAD)] + s
|
|
36
|
+
value /= LEAD
|
|
37
|
+
end
|
|
38
|
+
s
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Every number of up to three characters, written once.
|
|
42
|
+
NUMBERS = Array.new(LAST * LEAD * LEAD) { |v| number_slow(v).freeze }.freeze
|
|
43
|
+
|
|
44
|
+
module_function
|
|
45
|
+
|
|
46
|
+
def number(value) = NUMBERS[value] || number_slow(value)
|
|
47
|
+
|
|
48
|
+
# Appends one entry to out. offsets are ascending positions.
|
|
49
|
+
def append(out, key_gap, offsets)
|
|
50
|
+
out << number(key_gap) << number(offsets.length)
|
|
51
|
+
last = 0
|
|
52
|
+
offsets.each do |pos|
|
|
53
|
+
out << number(pos - last)
|
|
54
|
+
last = pos
|
|
55
|
+
end
|
|
56
|
+
out
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# All numbers of a list.
|
|
60
|
+
def numbers(str)
|
|
61
|
+
out = []
|
|
62
|
+
acc = 0
|
|
63
|
+
str.each_byte do |b|
|
|
64
|
+
d = DIGIT[b]
|
|
65
|
+
if d >= LAST then acc = (acc * LEAD) + d - LAST
|
|
66
|
+
else
|
|
67
|
+
out << ((acc * LAST) + d)
|
|
68
|
+
acc = 0
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
out
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Yields key, start, body and stop for each entry: its key, and the byte
|
|
75
|
+
# offsets where the entry starts, where its key gap ends and where it
|
|
76
|
+
# ends. Loops over bytes rather than numbers, so that the changes below
|
|
77
|
+
# can copy the rest of a list as it is.
|
|
78
|
+
def each_entry(str)
|
|
79
|
+
return enum_for(:each_entry, str) unless block_given?
|
|
80
|
+
|
|
81
|
+
key = -1
|
|
82
|
+
i = 0
|
|
83
|
+
len = str.bytesize
|
|
84
|
+
while i < len
|
|
85
|
+
start = i
|
|
86
|
+
acc = 0
|
|
87
|
+
while (d = DIGIT[str.getbyte(i)]) >= LAST
|
|
88
|
+
acc = (acc * LEAD) + d - LAST
|
|
89
|
+
i += 1
|
|
90
|
+
end
|
|
91
|
+
key += (acc * LAST) + d + 1
|
|
92
|
+
i += 1
|
|
93
|
+
body = i
|
|
94
|
+
acc = 0
|
|
95
|
+
while (d = DIGIT[str.getbyte(i)]) >= LAST
|
|
96
|
+
acc = (acc * LEAD) + d - LAST
|
|
97
|
+
i += 1
|
|
98
|
+
end
|
|
99
|
+
n = (acc * LAST) + d
|
|
100
|
+
i += 1
|
|
101
|
+
# Skip n numbers: each ends with a byte below LEAD_BYTE. Many at a
|
|
102
|
+
# time are counted by String#count, which runs in C.
|
|
103
|
+
while n > 8
|
|
104
|
+
t = str.byteslice(i, n).count(FINAL_BYTES)
|
|
105
|
+
i += n
|
|
106
|
+
n -= t
|
|
107
|
+
end
|
|
108
|
+
while n.positive?
|
|
109
|
+
n -= 1 if str.getbyte(i) < LEAD_BYTE
|
|
110
|
+
i += 1
|
|
111
|
+
end
|
|
112
|
+
yield key, start, body, i
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Key of the last entry, -1 for an empty list.
|
|
117
|
+
def last_key(str)
|
|
118
|
+
key = -1
|
|
119
|
+
each_entry(str) { |k| key = k }
|
|
120
|
+
key
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Each key of a list, in order.
|
|
124
|
+
def each_key(str)
|
|
125
|
+
return enum_for(:each_key, str) unless block_given?
|
|
126
|
+
|
|
127
|
+
each_entry(str) { |key| yield key }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# The list without the entries whose keys are in range (consecutive).
|
|
131
|
+
def delete(str, range)
|
|
132
|
+
before = -1
|
|
133
|
+
from = nil
|
|
134
|
+
each_entry(str) do |key, start, body|
|
|
135
|
+
if range.cover?(key)
|
|
136
|
+
from ||= start
|
|
137
|
+
elsif from
|
|
138
|
+
return str.byteslice(0, from) + number(key - before - 1) + str.byteslice(body..)
|
|
139
|
+
elsif key > range.end
|
|
140
|
+
return str
|
|
141
|
+
else
|
|
142
|
+
before = key
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
from ? str.byteslice(0, from) : str
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The list with an entry for key (not yet in it) and offsets inserted in order.
|
|
149
|
+
def insert(str, key, offsets)
|
|
150
|
+
before = -1
|
|
151
|
+
each_entry(str) do |k, start, body|
|
|
152
|
+
if k > key
|
|
153
|
+
entry = append(+"", key - before - 1, offsets)
|
|
154
|
+
return str.byteslice(0, start) + entry + number(k - key - 1) + str.byteslice(body..)
|
|
155
|
+
end
|
|
156
|
+
before = k
|
|
157
|
+
end
|
|
158
|
+
append(+str, key - before - 1, offsets)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# The list with each key's document replaced by numbering[document], which
|
|
162
|
+
# keeps the order (removed documents have no entries).
|
|
163
|
+
def renumber(str, fields, numbering)
|
|
164
|
+
out = +""
|
|
165
|
+
old_before = -1
|
|
166
|
+
new_before = -1
|
|
167
|
+
copied = 0 # str up to here is in out or to be copied as it is
|
|
168
|
+
each_entry(str) do |key, start, body|
|
|
169
|
+
new_key = (numbering[key / fields] * fields) + (key % fields)
|
|
170
|
+
if new_key - new_before != key - old_before
|
|
171
|
+
out << str.byteslice(copied, start - copied) << number(new_key - new_before - 1)
|
|
172
|
+
copied = body
|
|
173
|
+
end
|
|
174
|
+
old_before = key
|
|
175
|
+
new_before = new_key
|
|
176
|
+
end
|
|
177
|
+
copied.zero? ? str : out << str.byteslice(copied..)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# The flat list of format 3, [doc, field, n, pos, ..., doc, ...].
|
|
181
|
+
def decode(str, fields)
|
|
182
|
+
nums = numbers(str)
|
|
183
|
+
out = []
|
|
184
|
+
key = -1
|
|
185
|
+
i = 0
|
|
186
|
+
while i < nums.length
|
|
187
|
+
key += nums[i] + 1
|
|
188
|
+
n = nums[i + 1]
|
|
189
|
+
out << (key / fields) << (key % fields) << n
|
|
190
|
+
pos = 0
|
|
191
|
+
j = i + 2
|
|
192
|
+
stop = j + n
|
|
193
|
+
while j < stop
|
|
194
|
+
pos += nums[j]
|
|
195
|
+
out << pos
|
|
196
|
+
j += 1
|
|
197
|
+
end
|
|
198
|
+
i = stop
|
|
199
|
+
end
|
|
200
|
+
out
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# A flat list of format 2 or 3 in this encoding.
|
|
204
|
+
def encode(flat, fields)
|
|
205
|
+
out = +""
|
|
206
|
+
last = -1
|
|
207
|
+
i = 0
|
|
208
|
+
while i < flat.length
|
|
209
|
+
key = (flat[i] * fields) + flat[i + 1]
|
|
210
|
+
n = flat[i + 2]
|
|
211
|
+
append(out, key - last - 1, flat[i + 3, n])
|
|
212
|
+
last = key
|
|
213
|
+
i += 3 + n
|
|
214
|
+
end
|
|
215
|
+
out
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rake"
|
|
4
|
+
require "rake/tasklib"
|
|
5
|
+
require "fileutils"
|
|
6
|
+
require_relative "cli"
|
|
7
|
+
|
|
8
|
+
module CJKIndex
|
|
9
|
+
# A Rake task that builds an index (and the browser script) from a CSV,
|
|
10
|
+
# JSON or JSON Lines file, usable from any Ruby build (Rails, Sinatra, a
|
|
11
|
+
# plain Rakefile). It does what `cjk_index build` does.
|
|
12
|
+
#
|
|
13
|
+
# require "cjk_index/rake_task"
|
|
14
|
+
#
|
|
15
|
+
# CJKIndex::RakeTask.new(:search_index) do |t|
|
|
16
|
+
# t.source = "data/items.csv"
|
|
17
|
+
# t.output = "public/search/items.json"
|
|
18
|
+
# t.runtime = "public/search/cjk-index.js" # optional
|
|
19
|
+
# t.ref = "id"
|
|
20
|
+
# t.fields = %w[title creator]
|
|
21
|
+
# t.boosts = { "title" => 3 }
|
|
22
|
+
# t.filters = { "type" => "value", "date" => "year" }
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# rake search_index
|
|
26
|
+
#
|
|
27
|
+
# records can be set instead of source: an Array of Hashes, or an object
|
|
28
|
+
# that responds to call and returns one (e.g. a lambda reading the database).
|
|
29
|
+
class RakeTask < Rake::TaskLib
|
|
30
|
+
attr_accessor :name, :source, :records, :output, :runtime, :ref, :fields, :boosts, :filters, :variants, :format,
|
|
31
|
+
:description
|
|
32
|
+
|
|
33
|
+
def initialize(name = :cjk_index)
|
|
34
|
+
super()
|
|
35
|
+
@name = name
|
|
36
|
+
@boosts = {}
|
|
37
|
+
@filters = {}
|
|
38
|
+
@variants = Normalizer::DEFAULT_TABLES
|
|
39
|
+
@description = "Build the CJK search index"
|
|
40
|
+
yield self if block_given?
|
|
41
|
+
define
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Builds the index now; the task calls this.
|
|
45
|
+
def build
|
|
46
|
+
raise CLI::Error, "#{name}: set output" unless output
|
|
47
|
+
|
|
48
|
+
data = records.respond_to?(:call) ? records.call : records
|
|
49
|
+
data ||= source ? CLI.read_records(source.to_s, format || CLI.format_of(source)) : raise(CLI::Error, "#{name}: set source or records")
|
|
50
|
+
builder = CLI.build_index(records: data, ref: ref, fields: fields, boosts: boosts, filters: filters, variants: variants)
|
|
51
|
+
write(output, builder.to_json)
|
|
52
|
+
write(runtime, Runtime.source(normalizer: builder.normalizer)) if runtime
|
|
53
|
+
builder
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def define
|
|
59
|
+
desc description
|
|
60
|
+
task(name) do
|
|
61
|
+
builder = build
|
|
62
|
+
puts "#{output}: #{builder.size} records"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def write(path, content)
|
|
67
|
+
FileUtils.mkdir_p(File.dirname(path.to_s))
|
|
68
|
+
File.write(path.to_s, content)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/cjk_index/runtime.js
CHANGED
|
@@ -9,9 +9,49 @@
|
|
|
9
9
|
|
|
10
10
|
var VARIANTS = /*@VARIANTS@*/{};
|
|
11
11
|
var TABLES = /*@TABLES@*/[];
|
|
12
|
+
var DIGEST = /*@DIGEST@*/'';
|
|
12
13
|
var CJK = new RegExp(/*@CJK@*/'', 'u');
|
|
13
14
|
var SEPARATOR = new RegExp(/*@SEPARATOR@*/'', 'u');
|
|
14
|
-
var FORMATS = ['cjk_index/2', 'cjk_index/3'];
|
|
15
|
+
var FORMATS = ['cjk_index/2', 'cjk_index/3', 'cjk_index/4'];
|
|
16
|
+
var COMPACT = 'cjk_index/4';
|
|
17
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
18
|
+
|
|
19
|
+
// Posting lists of format 4 (CJKIndex::Postings in Ruby): numbers written
|
|
20
|
+
// with 90 printable characters, '#' to '}' without '\'. The first 60 are the
|
|
21
|
+
// last digit of a number (base 60), the other 30 lead it (base 30).
|
|
22
|
+
var DIGIT = [];
|
|
23
|
+
for (var code = 0x23, digit = 0; code <= 0x7d; code++) if (code !== 0x5c) DIGIT[code] = digit++;
|
|
24
|
+
|
|
25
|
+
// A posting list of format 4 as the flat list of format 3:
|
|
26
|
+
// [doc, field, n, pos, ..., doc, field, n, ...].
|
|
27
|
+
function decodePostings(s, nf) {
|
|
28
|
+
var out = [];
|
|
29
|
+
var key = -1, state = 0, left = 0, pos = 0, acc = 0;
|
|
30
|
+
for (var i = 0; i < s.length; i++) {
|
|
31
|
+
var d = DIGIT[s.charCodeAt(i)];
|
|
32
|
+
if (d >= 60) { acc = acc * 30 + d - 60; continue; }
|
|
33
|
+
var v = acc * 60 + d;
|
|
34
|
+
acc = 0;
|
|
35
|
+
if (state === 0) { key += v + 1; out.push(Math.floor(key / nf), key % nf); state = 1; }
|
|
36
|
+
else if (state === 1) { out.push(v); left = v; pos = 0; state = 2; }
|
|
37
|
+
else { pos += v; out.push(pos); if (--left === 0) state = 0; }
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Compares strings by code point, as Ruby compares UTF-8 strings (the
|
|
43
|
+
// order of the shards). JavaScript's < compares UTF-16 code units, which
|
|
44
|
+
// puts characters beyond U+FFFF before U+E000-U+FFFF.
|
|
45
|
+
function compareCodePoints(a, b) {
|
|
46
|
+
var i = 0, j = 0;
|
|
47
|
+
while (i < a.length && j < b.length) {
|
|
48
|
+
var x = a.codePointAt(i), y = b.codePointAt(j);
|
|
49
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
50
|
+
i += x > 0xffff ? 2 : 1;
|
|
51
|
+
j += y > 0xffff ? 2 : 1;
|
|
52
|
+
}
|
|
53
|
+
return (i < a.length ? 1 : 0) - (j < b.length ? 1 : 0);
|
|
54
|
+
}
|
|
15
55
|
|
|
16
56
|
// BM25 parameters (the usual defaults, as in Lucene and Pagefind).
|
|
17
57
|
var K1 = 1.2;
|
|
@@ -119,6 +159,10 @@
|
|
|
119
159
|
throw new Error('cjk_index: index built with variant tables ' + JSON.stringify(data.variants) +
|
|
120
160
|
' but this script folds with ' + JSON.stringify(TABLES) + '; rebuild both together');
|
|
121
161
|
}
|
|
162
|
+
if (data.variant_digest !== undefined && data.variant_digest !== DIGEST) {
|
|
163
|
+
throw new Error('cjk_index: index built with other contents of the variant tables ' +
|
|
164
|
+
JSON.stringify(TABLES) + ' (an older cjk_index?); rebuild it');
|
|
165
|
+
}
|
|
122
166
|
options = options || {};
|
|
123
167
|
this.prefix = options.prefix !== false; // last Latin word matches as a prefix
|
|
124
168
|
this.typos = options.typos !== false; // Latin words of 5+ letters allow one edit
|
|
@@ -126,15 +170,30 @@
|
|
|
126
170
|
this.boosts = data.boosts;
|
|
127
171
|
this.refs = data.refs;
|
|
128
172
|
this.lengths = data.lengths;
|
|
129
|
-
this.tokens = data.tokens;
|
|
173
|
+
this.tokens = data.tokens || null;
|
|
174
|
+
this.encoded = data.format === COMPACT;
|
|
175
|
+
this.decoded = new Map();
|
|
176
|
+
if (data.shards) {
|
|
177
|
+
// Split index: the manifest lists the first token and file of each shard.
|
|
178
|
+
this.loadShard = options.loadShard;
|
|
179
|
+
if (!this.loadShard) throw new Error('cjk_index: a split index needs options.loadShard (CJKIndex.load sets it)');
|
|
180
|
+
this.shardFirst = data.shards.map(function (s) { return s[0]; });
|
|
181
|
+
this.shardFile = data.shards.map(function (s) { return s[1]; });
|
|
182
|
+
this.shards = new Map(); // file -> tokens, once loaded
|
|
183
|
+
this.pending = new Map(); // file -> Promise while loading
|
|
184
|
+
}
|
|
130
185
|
var nf = this.fields.length;
|
|
131
186
|
var avg = new Array(nf).fill(0);
|
|
132
187
|
for (var i = 0; i < this.lengths.length; i++) avg[i % nf] += this.lengths[i];
|
|
133
188
|
this.avgLength = avg.map(function (s) { return s / Math.max(1, data.refs.length) || 1; });
|
|
134
|
-
|
|
189
|
+
// A split index lists its Latin tokens in a file, fetched by prepare
|
|
190
|
+
// only for typo expansion.
|
|
191
|
+
this.latinFile = this.tokens ? null : data.latin;
|
|
192
|
+
if (this.tokens) this._setLatin(Object.keys(this.tokens).filter(isLatin));
|
|
135
193
|
this.slotOf = new Map();
|
|
136
194
|
for (var d = 0; d < this.refs.length; d++) if (!this.slotOf.has(this.refs[d])) this.slotOf.set(this.refs[d], d);
|
|
137
|
-
// Filter values by document:
|
|
195
|
+
// Filter values by document: a year, or the ids of values (value
|
|
196
|
+
// filters), with the folded key of each id, the ids of each key, the
|
|
138
197
|
// form to show for each key and its rank of first appearance.
|
|
139
198
|
this.filters = {};
|
|
140
199
|
var filters = data.filters || {};
|
|
@@ -142,19 +201,22 @@
|
|
|
142
201
|
var f = filters[name];
|
|
143
202
|
var docs = f.docs;
|
|
144
203
|
var display = new Map();
|
|
204
|
+
var keys = null;
|
|
205
|
+
var ids = new Map();
|
|
145
206
|
if (f.kind !== 'year') {
|
|
146
|
-
|
|
207
|
+
keys = f.values.map(filterKey);
|
|
147
208
|
f.values.forEach(function (v, i) { if (!display.has(keys[i])) display.set(keys[i], v); });
|
|
148
|
-
|
|
209
|
+
keys.forEach(function (key, id) { if (!ids.has(key)) ids.set(key, []); ids.get(key).push(id); });
|
|
149
210
|
}
|
|
150
211
|
var rank = new Map();
|
|
151
212
|
docs.forEach(function (value) {
|
|
152
|
-
(f.kind === 'year' ? (value === null ? [] : [value]) : value).forEach(function (
|
|
213
|
+
(f.kind === 'year' ? (value === null ? [] : [value]) : value).forEach(function (v) {
|
|
214
|
+
var key = f.kind === 'year' ? v : keys[v];
|
|
153
215
|
if (!rank.has(key)) rank.set(key, rank.size);
|
|
154
216
|
});
|
|
155
217
|
});
|
|
156
218
|
if (f.kind === 'year') rank.forEach(function (r, year) { display.set(year, year); });
|
|
157
|
-
this.filters[name] = { kind: f.kind, docs: docs, display: display, rank: rank };
|
|
219
|
+
this.filters[name] = { kind: f.kind, docs: docs, keys: keys, ids: ids, display: display, rank: rank };
|
|
158
220
|
}, this);
|
|
159
221
|
}
|
|
160
222
|
|
|
@@ -175,8 +237,12 @@
|
|
|
175
237
|
(max === null || max === undefined || year <= max);
|
|
176
238
|
}]);
|
|
177
239
|
} else {
|
|
178
|
-
|
|
179
|
-
|
|
240
|
+
// Compares value ids, not folded strings.
|
|
241
|
+
var ids = new Set();
|
|
242
|
+
(Array.isArray(c) ? c : [c]).forEach(function (v) {
|
|
243
|
+
(f.ids.get(filterKey(v)) || []).forEach(function (id) { ids.add(id); });
|
|
244
|
+
});
|
|
245
|
+
out.push([f, function (list) { return list.some(function (id) { return ids.has(id); }); }]);
|
|
180
246
|
}
|
|
181
247
|
});
|
|
182
248
|
return out;
|
|
@@ -206,7 +272,8 @@
|
|
|
206
272
|
var counts = new Map();
|
|
207
273
|
docs.forEach(function (doc) {
|
|
208
274
|
var v = f.docs[doc];
|
|
209
|
-
(f.kind === 'year' ? (v === null ? [] : [v]) : v).forEach(function (
|
|
275
|
+
(f.kind === 'year' ? (v === null ? [] : [v]) : v).forEach(function (x) {
|
|
276
|
+
var key = f.kind === 'year' ? x : f.keys[x];
|
|
210
277
|
counts.set(key, (counts.get(key) || 0) + 1);
|
|
211
278
|
});
|
|
212
279
|
});
|
|
@@ -215,12 +282,109 @@
|
|
|
215
282
|
return out.map(function (e) { return { value: f.display.get(e[0]), count: e[1] }; });
|
|
216
283
|
};
|
|
217
284
|
|
|
285
|
+
Index.prototype._setLatin = function (tokens) {
|
|
286
|
+
this.latinTokens = tokens.slice().sort();
|
|
287
|
+
this.latinSet = new Set(this.latinTokens);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// Number of the shard that holds token (split index), or -1.
|
|
291
|
+
Index.prototype._shardIndex = function (token) {
|
|
292
|
+
var lo = 0, hi = this.shardFirst.length;
|
|
293
|
+
while (lo < hi) {
|
|
294
|
+
var mid = (lo + hi) >> 1;
|
|
295
|
+
if (compareCodePoints(this.shardFirst[mid], token) <= 0) lo = mid + 1; else hi = mid;
|
|
296
|
+
}
|
|
297
|
+
return lo - 1;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
function notLoaded(what) {
|
|
301
|
+
return new Error('cjk_index: ' + what + ' not loaded; use searchAsync(query) or await prepare(query)');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Tokens of shard i, or undefined when it is not loaded: then its file is
|
|
305
|
+
// added to missing (prepare), or an error is thrown (search).
|
|
306
|
+
Index.prototype._shard = function (i, missing) {
|
|
307
|
+
var tokens = this.shards.get(this.shardFile[i]);
|
|
308
|
+
if (tokens) return tokens;
|
|
309
|
+
if (!missing) throw notLoaded('the shard ' + this.shardFile[i] + ' is');
|
|
310
|
+
missing.files.add(this.shardFile[i]);
|
|
311
|
+
return undefined;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// The object that holds token's posting list if it is in the index; null
|
|
315
|
+
// when it cannot be; undefined when its shard is not loaded (see _shard).
|
|
316
|
+
Index.prototype._table = function (token, missing) {
|
|
317
|
+
if (this.tokens) return this.tokens;
|
|
318
|
+
var i = this._shardIndex(token);
|
|
319
|
+
return i < 0 ? null : this._shard(i, missing);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// true or false, or undefined when not known yet (prepare).
|
|
323
|
+
Index.prototype._has = function (token, missing) {
|
|
324
|
+
var table = this._table(token, missing);
|
|
325
|
+
return table === undefined ? undefined : !!table && hasOwn.call(table, token);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// The posting list of a token as [doc, field, n, pos, ...], or null.
|
|
329
|
+
Index.prototype._postings = function (token) {
|
|
330
|
+
var table = this._table(token);
|
|
331
|
+
if (!table || !hasOwn.call(table, token)) return null;
|
|
332
|
+
if (!this.encoded) return table[token];
|
|
333
|
+
var p = this.decoded.get(token);
|
|
334
|
+
if (!p) { p = decodePostings(table[token], this.fields.length); this.decoded.set(token, p); }
|
|
335
|
+
return p;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
Index.prototype._load = function (file, done) {
|
|
339
|
+
var self = this;
|
|
340
|
+
var p = this.pending.get(file);
|
|
341
|
+
if (!p) {
|
|
342
|
+
p = Promise.resolve(this.loadShard(file)).then(done);
|
|
343
|
+
p.catch(function () { self.pending.delete(file); });
|
|
344
|
+
this.pending.set(file, p);
|
|
345
|
+
}
|
|
346
|
+
return p;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// Fetches what a search for query needs from a split index, so that
|
|
350
|
+
// search(query) can then run: the shards of its tokens, of the words a
|
|
351
|
+
// last Latin word is a prefix of, and, for a Latin word of 5+ letters not
|
|
352
|
+
// in the index, the list of Latin tokens (once) and the shards of the
|
|
353
|
+
// words one edit away. Resolves to the index; at once for one file.
|
|
354
|
+
Index.prototype.prepare = function (query) {
|
|
355
|
+
var self = this;
|
|
356
|
+
if (!this.shardFirst) return Promise.resolve(this);
|
|
357
|
+
var missing = { files: new Set(), latin: false };
|
|
358
|
+
var qp = phrases(query, false);
|
|
359
|
+
qp.forEach(function (phrase, q) {
|
|
360
|
+
phrase.forEach(function (tok, i) {
|
|
361
|
+
self._alternatives(tok[0], q === qp.length - 1 && i === phrase.length - 1, missing).tokens.forEach(function (t) {
|
|
362
|
+
self._table(t, missing);
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
var loads = Array.from(missing.files).map(function (file) {
|
|
367
|
+
return self._load(file, function (tokens) { self.shards.set(file, tokens); });
|
|
368
|
+
});
|
|
369
|
+
if (missing.latin) loads.push(this._load(this.latinFile, function (tokens) { self._setLatin(tokens); }));
|
|
370
|
+
if (!loads.length) return Promise.resolve(this);
|
|
371
|
+
// What was loaded may show more to load (typo candidates).
|
|
372
|
+
return Promise.all(loads).then(function () { return self.prepare(query); });
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// search, after fetching the shards it needs. Works for every index, so
|
|
376
|
+
// a page can use it whether or not the index is split.
|
|
377
|
+
Index.prototype.searchAsync = function (query, options) {
|
|
378
|
+
var self = this;
|
|
379
|
+
return this.prepare(query).then(function () { return self.search(query, options); });
|
|
380
|
+
};
|
|
381
|
+
|
|
218
382
|
// Map of "doc,field" key -> Set of positions, merged over the alternatives.
|
|
219
383
|
Index.prototype._positions = function (tokens) {
|
|
220
384
|
var nf = this.fields.length;
|
|
221
385
|
var map = new Map();
|
|
222
386
|
for (var t = 0; t < tokens.length; t++) {
|
|
223
|
-
var p = this.tokens[
|
|
387
|
+
var p = this._postings(tokens[t]);
|
|
224
388
|
if (!p) continue;
|
|
225
389
|
for (var i = 0; i < p.length;) {
|
|
226
390
|
var key = p[i] * nf + p[i + 1];
|
|
@@ -236,17 +400,36 @@
|
|
|
236
400
|
|
|
237
401
|
// Alternatives for one query token: itself, and for Latin words the index
|
|
238
402
|
// words it is a prefix of (last word only) or one edit away from.
|
|
239
|
-
|
|
403
|
+
// With missing (prepare), what is not loaded yet is noted there and left out.
|
|
404
|
+
Index.prototype._alternatives = function (token, isLast, missing) {
|
|
240
405
|
if (!isLatin(token)) return { tokens: [token], expanded: false };
|
|
241
|
-
var
|
|
242
|
-
var
|
|
406
|
+
var has = this.tokens ? this.latinSet.has(token) : this._has(token, missing);
|
|
407
|
+
var out = has ? [token] : [];
|
|
243
408
|
if (this.prefix && isLast) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
409
|
+
if (this.tokens) {
|
|
410
|
+
var keys = this.latinTokens;
|
|
411
|
+
var lo = 0, hi = keys.length;
|
|
412
|
+
while (lo < hi) { var mid = (lo + hi) >> 1; if (keys[mid] < token) lo = mid + 1; else hi = mid; }
|
|
413
|
+
for (var i = lo; i < keys.length && keys[i].indexOf(token) === 0; i++) if (keys[i] !== token) out.push(keys[i]);
|
|
414
|
+
} else {
|
|
415
|
+
// The words starting with token are in its shard and the next ones
|
|
416
|
+
// that start with it (the tokens are sorted).
|
|
417
|
+
var first = Math.max(0, this._shardIndex(token));
|
|
418
|
+
for (var s = first; s < this.shardFirst.length && (s === first || this.shardFirst[s].indexOf(token) === 0); s++) {
|
|
419
|
+
var shard = this._shard(s, missing);
|
|
420
|
+
if (!shard) continue;
|
|
421
|
+
for (var k in shard) if (hasOwn.call(shard, k) && k !== token && k.indexOf(token) === 0) out.push(k);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
247
424
|
}
|
|
248
|
-
if (this.typos &&
|
|
249
|
-
|
|
425
|
+
if (this.typos && has === false && Array.from(token).length >= 5) {
|
|
426
|
+
if (this.latinTokens) {
|
|
427
|
+
this.latinTokens.forEach(function (k) { if (oneEdit(token, k)) out.push(k); });
|
|
428
|
+
} else if (missing) {
|
|
429
|
+
missing.latin = true;
|
|
430
|
+
} else {
|
|
431
|
+
throw notLoaded('the Latin tokens are');
|
|
432
|
+
}
|
|
250
433
|
}
|
|
251
434
|
return { tokens: out, expanded: out.length > 0 && out[0] !== token };
|
|
252
435
|
};
|
|
@@ -335,20 +518,38 @@
|
|
|
335
518
|
});
|
|
336
519
|
}
|
|
337
520
|
|
|
521
|
+
// Characters that NFKC may compose with the one before (combining marks,
|
|
522
|
+
// Hangul vowel and final jamo, half-width sound marks): kept in one cluster
|
|
523
|
+
// so that the cluster normalizes as it does inside the whole text.
|
|
524
|
+
var JOINS_PREVIOUS = /^[\p{M}\u1160-\u11ff\ud7b0-\ud7ff\u314f-\u3163\u3187-\u318e\uff9e\uff9f]$/u;
|
|
525
|
+
|
|
338
526
|
// [start, end) ranges, in code points of the original text, where a phrase
|
|
339
527
|
// of the query occurs after normalization.
|
|
340
528
|
function matchRanges(text, query) {
|
|
341
529
|
var chars = Array.from(String(text === null || text === undefined ? '' : text));
|
|
342
530
|
var norm = [];
|
|
343
|
-
var
|
|
531
|
+
var start = [];
|
|
532
|
+
var end = [];
|
|
344
533
|
var prev = null;
|
|
345
|
-
|
|
346
|
-
|
|
534
|
+
// Each folded code point remembers which original characters it came
|
|
535
|
+
// from. A character may fold to several (ko: one Private Use Area
|
|
536
|
+
// character -> a jamo sequence), and a cluster may compose into fewer.
|
|
537
|
+
function emit(str, from, to) {
|
|
538
|
+
for (var c of str.normalize('NFKC').toLowerCase()) {
|
|
347
539
|
prev = foldChar(c, prev);
|
|
348
|
-
norm.push(
|
|
349
|
-
origin.push(i);
|
|
540
|
+
for (var p of prev) { norm.push(p); start.push(from); end.push(to); }
|
|
350
541
|
}
|
|
351
|
-
}
|
|
542
|
+
}
|
|
543
|
+
for (var i = 0, j; i < chars.length; i = j) {
|
|
544
|
+
for (j = i + 1; j < chars.length && JOINS_PREVIOUS.test(chars[j]); j++);
|
|
545
|
+
var cluster = chars.slice(i, j);
|
|
546
|
+
var whole = cluster.join('').normalize('NFKC');
|
|
547
|
+
if (cluster.map(function (c) { return c.normalize('NFKC'); }).join('') === whole) {
|
|
548
|
+
cluster.forEach(function (c, k) { emit(c, i + k, i + k + 1); });
|
|
549
|
+
} else {
|
|
550
|
+
emit(cluster.join(''), i, j);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
352
553
|
var normText = norm.join('');
|
|
353
554
|
var ranges = [];
|
|
354
555
|
var needles = [];
|
|
@@ -363,7 +564,7 @@
|
|
|
363
564
|
var at;
|
|
364
565
|
while ((at = normText.indexOf(needle, from)) !== -1) {
|
|
365
566
|
var cp = Array.from(normText.slice(0, at)).length;
|
|
366
|
-
ranges.push([
|
|
567
|
+
ranges.push([start[cp], end[cp + len - 1]]);
|
|
367
568
|
from = at + needle.length;
|
|
368
569
|
}
|
|
369
570
|
});
|
|
@@ -400,11 +601,24 @@
|
|
|
400
601
|
return (start > 0 ? '…' : '') + highlight(part, query) + (end < m.chars.length ? '…' : '');
|
|
401
602
|
}
|
|
402
603
|
|
|
403
|
-
function
|
|
604
|
+
function fetchJson(url) {
|
|
404
605
|
return fetch(url).then(function (res) {
|
|
405
606
|
if (!res.ok) throw new Error('cjk_index: ' + url + ' returned ' + res.status);
|
|
406
607
|
return res.json();
|
|
407
|
-
})
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// Loads an index file, or the manifest of a split index: its shards are
|
|
612
|
+
// then fetched from the same directory as queries need them (searchAsync).
|
|
613
|
+
function load(url, options) {
|
|
614
|
+
options = Object.assign({}, options);
|
|
615
|
+
return fetchJson(url).then(function (data) {
|
|
616
|
+
if (data && data.shards && !options.loadShard) {
|
|
617
|
+
var base = new URL(url, typeof location !== 'undefined' ? location.href : undefined);
|
|
618
|
+
options.loadShard = function (file) { return fetchJson(new URL(file, base).href); };
|
|
619
|
+
}
|
|
620
|
+
return new Index(data, options);
|
|
621
|
+
});
|
|
408
622
|
}
|
|
409
623
|
|
|
410
624
|
var api = {
|
data/lib/cjk_index/runtime.rb
CHANGED
|
@@ -17,6 +17,7 @@ module CJKIndex
|
|
|
17
17
|
# back-references and drop half of the regexp escapes.
|
|
18
18
|
.sub("/*@VARIANTS@*/{}") { JSON.generate(normalizer.variants) }
|
|
19
19
|
.sub("/*@TABLES@*/[]") { JSON.generate(normalizer.tables) }
|
|
20
|
+
.sub("/*@DIGEST@*/''") { JSON.generate(normalizer.digest) }
|
|
20
21
|
.sub("/*@CJK@*/''") { JSON.generate(Tokenizer.char_class(Tokenizer::CJK_RANGES)) }
|
|
21
22
|
.sub("/*@SEPARATOR@*/''") do
|
|
22
23
|
JSON.generate(Tokenizer.char_class(Tokenizer::SEPARATOR_RANGES, Tokenizer::SEPARATOR_CHARS))
|