-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrch.rb
447 lines (368 loc) · 10.1 KB
/
srch.rb
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
require 'duckdb'
require 'nokogiri'
require 'pp'
module Models
class Document
attr_reader :title, :body
def initialize(title:, body:)
@title = title
@body = body
end
end
class Entry
attr_reader :document, :term_freq, :term_count
def initialize(document:, term_freq:, term_count:)
@document = document
@term_freq = term_freq
@term_count = term_count
end
end
end
class Commands
class Tokenize
TOKEN_BEGIN = :token_begin
TOKEN_END = :token_end
def call(content)
Enumerator.new do |yielder|
start = 0
token_boundaries(content).each do |type, index|
case type
when TOKEN_BEGIN
start = index
when TOKEN_END
token = content[start..index]
yielder.yield(token.downcase)
else
raise NotImplementedError, type
end
end
end
end
private
def token_boundaries(content)
Enumerator.new do |yielder|
inside_token = false
content.each_char.with_index do |char, index|
if token_char?(char)
if !inside_token
yielder.yield(TOKEN_BEGIN, index)
inside_token = true
end
else
if inside_token
yielder.yield(TOKEN_END, index - 1)
inside_token = false
end
end
end
if inside_token
yielder.yield(TOKEN_END, content.size - 1)
end
end
end
def token_char?(char)
!whitespace?(char) && !punctuation?(char)
end
def whitespace?(char)
char == ' ' ||
char == "\r" ||
char == "\n" ||
char == "\t" ||
char == "\v" ||
char == "\f"
end
def punctuation?(char)
(33..47).include?(char.ord) ||
(58..64).include?(char.ord) ||
(91..96).include?(char.ord) ||
(123..126).include?(char.ord)
end
end
# NOTE: indexing directory containing plain text files
class IndexDirectory
def call(index, pattern)
Dir.glob(pattern).each do |file_path|
file_contents = File.read(file_path)
document = Models::Document.new(
title: file_path,
body: file_contents,
)
$stdout.puts("INFO: indexing #{file_path.dump}")
index.add(document)
end
$stdout.puts('INFO: done')
end
end
# NOTE: indexing wikipedia xml dump files
# ref: https://dumps.wikimedia.org/enwiki/
class IndexArticles
def call(index, path)
File.open(path) do |file|
title = nil
body = nil
doc_count = 1
Nokogiri::XML::Reader(file).each do |node|
case node.name
when 'title'
if title.nil? && !node.inner_xml.empty?
title = node.inner_xml
end
when 'text'
if body.nil? && !node.inner_xml.empty?
body = node.inner_xml
end
end
if title && body
document = Models::Document.new(
title: title,
body: body,
)
$stdout.puts("INFO: (#{doc_count}) indexing #{title.dump}")
index.add(document)
title = nil
body = nil
break if doc_count > 10000
doc_count += 1
end
end
$stdout.puts('INFO: done')
end
end
end
end
module Repositories
class InMemoryIndex
attr_reader :entries, :entry_freq
def initialize(tokenize:)
@tokenize = tokenize
@entries = []
@entry_freq = {}
end
def add(document)
term_freq = @tokenize.call(document.body).tally
@entries.push(
Models::Entry.new(
document: document,
term_freq: term_freq,
term_count: term_freq.sum { |_, freq| freq },
)
)
term_freq.each do |term, _|
@entry_freq[term] ||= 0
@entry_freq[term] += 1
end
end
def search(query)
query_terms = @tokenize.call(query).to_a
results = entries.map do |entry|
rank = query_terms.sum do |term|
tf_idf(entry, term)
end
[rank, entry.document.title]
end
results.select! { |result| result[0].positive? }
results.sort!
results.reverse!
results.take(10)
end
private
def tf(entry, term)
entry.term_freq.fetch(term, 0).to_f / entry.term_count
end
def idf(term)
count = entry_freq.fetch(term, 0)
if count.zero?
0.0
else
Math.log2(entries.count.to_f / count)
end
end
def tf_idf(entry, term)
tf(entry, term) * idf(term)
end
end
class DuckdbIndex
def initialize(db:, tokenize:)
@db = db
@tokenize = tokenize
end
def setup
transaction do
@db.execute(<<~SQL)
CREATE SEQUENCE seq_documents_id
START 1;
SQL
@db.execute(<<~SQL)
CREATE TABLE documents (
id INTEGER NOT NULL DEFAULT nextval('seq_documents_id'),
title VARCHAR NOT NULL,
body VARCHAR NOT NULL,
term_count INTEGER NOT NULL,
PRIMARY KEY (id),
);
SQL
@db.execute(<<~SQL)
CREATE SEQUENCE seq_terms_id
START 1;
SQL
@db.execute(<<~SQL)
CREATE TABLE terms (
id INTEGER NOT NULL DEFAULT nextval('seq_terms_id'),
term VARCHAR NOT NULL,
document_freq INTEGER NOT NULL,
PRIMARY KEY (id),
UNIQUE (term),
);
SQL
@db.execute(<<~SQL)
CREATE TABLE term_freq (
document_id INTEGER NOT NULL,
term_id INTEGER NOT NULL,
freq INTEGER NOT NULL,
PRIMARY KEY (document_id, term_id),
FOREIGN KEY (document_id) REFERENCES documents(id),
FOREIGN KEY (term_id) REFERENCES terms(id),
);
SQL
end
end
def add(document)
transaction do
term_freq = @tokenize.call(document.body).tally
document_id = insert_document(document, term_freq)
term_ids = insert_terms(term_freq)
insert_term_freq(term_freq, document_id, term_ids)
end
end
def search(query)
query_terms = @tokenize.call(query).to_a
query_terms_values = query_terms.count.times.
map { |index| "($#{index + 1})" }.
join(', ')
stmt = @db.prepared_statement(<<~SQL)
SELECT
sum(
coalesce(term_freq.freq, 0)::double / coalesce(documents.term_count, 0)
* log2((SELECT count(*) FROM documents)::double / coalesce(terms.document_freq, 0))
) AS rank,
documents.title
FROM (VALUES #{query_terms_values}) AS query_terms(term)
INNER JOIN terms
ON query_terms.term = terms.term
INNER JOIN term_freq
ON term_freq.term_id = terms.id
INNER JOIN documents
ON term_freq.document_id = documents.id
GROUP BY
documents.id,
documents.title
HAVING rank > 0.0
ORDER BY rank DESC
LIMIT 10
SQL
query_terms.each.with_index do |term, index|
stmt.bind(index + 1, term)
end
stmt.execute.to_a
end
private
def transaction
@db.execute('BEGIN;')
yield
@db.execute('COMMIT;')
rescue
@db.execute('ROLLBACK;')
raise
end
def insert_document(document, term_freq)
term_count = term_freq.sum { |_, freq| freq }
# NOTE: for some reason binding 3 values doesn't return id, but count of
# inserted rows
stmt = @db.prepared_statement(<<~SQL)
INSERT INTO documents(title, body, term_count)
VALUES ($1, $2, #{term_count})
RETURNING id;
SQL
stmt.bind(1, document.title)
stmt.bind(2, document.body)
stmt.execute.to_value(0, 0)
end
def insert_terms(term_freq)
terms = find_terms(term_freq)
if terms.any?
@db.execute(<<~SQL)
UPDATE terms
SET document_freq = document_freq + 1
WHERE id IN (#{terms.values.join(', ')})
SQL
end
(term_freq.keys - terms.keys).each do |term|
stmt = @db.prepared_statement(<<~SQL)
INSERT INTO terms(term, document_freq)
VALUES ($1, 1)
RETURNING id;
SQL
stmt.bind(1, term)
term_id = stmt.execute.to_value(0, 0)
terms[term] = term_id
end
terms
end
def find_terms(term_freq)
in_binding = term_freq.count.times.map { |index| "$#{index + 1}" }.join(', ')
stmt = @db.prepared_statement(<<~SQL)
SELECT term, id
FROM terms
WHERE term IN (#{in_binding})
SQL
term_freq.each.with_index do |(term, _), index|
stmt.bind(index + 1, term)
end
stmt.execute.to_h
end
def insert_term_freq(term_freq, document_id, term_ids)
term_freq.each do |term, freq|
stmt = @db.prepared_statement(<<~SQL)
INSERT INTO term_freq(document_id, term_id, freq)
VALUES ($1, $2, $3);
SQL
stmt.bind(1, document_id)
stmt.bind(2, term_ids[term])
stmt.bind(3, freq)
stmt.execute
end
end
end
end
# ------------------------------------------------------------------------------
INDEX_FILE_PATH = './index.duckdb'
INDEX_WAL_FILE_PATH = './index.duckdb.wal'
tokenize = Commands::Tokenize.new
db = DuckDB::Database.open(INDEX_FILE_PATH).connect
duckdb_index = Repositories::DuckdbIndex.new(
db: db,
tokenize: tokenize,
)
case ARGV[0]
when 'index'
File.delete(INDEX_FILE_PATH) if File.exist?(INDEX_FILE_PATH)
File.delete(INDEX_WAL_FILE_PATH) if File.exist?(INDEX_WAL_FILE_PATH)
index_articles = Commands::IndexArticles.new
duckdb_index.setup
index_articles.call(duck_dbindex, ARGV[1])
when 'search'
loop do
$stdout.puts('-' * 40)
$stdout << 'query> '
query = $stdin.gets
break unless query
search_start = Time.now
hits = duckdb_index.search(query)
search_end = Time.now
puts
puts "RESULTS (#{search_end - search_start}s):"
pp hits
end
else
puts "ERROR: invalid command #{ARGV[0].dump}"
end