Method: BomDB::Cli::Application#index

Defined in:
lib/bomdb/cli/application.rb

#index(wordgroup) ⇒ Object



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
# File 'lib/bomdb/cli/application.rb', line 305

def index(wordgroup)
  wordgroup = wordgroup.to_i

  chapters = BomDB::Query.new.chapters.map do |(book, chapter), content|
    Chapter.new(content.split(/\s+/).size, book, chapter)
  end

  total_words = chapters.inject(0){ |sum, ch| sum += ch.wordcount }

  idx = []
  words_so_far = 0
  chapter_index = 0
  j = 0

  (wordgroup..(total_words+wordgroup)).step(wordgroup) do |i|
    while words_so_far < i && chapter_index < chapters.size
      words_so_far += chapters[chapter_index].wordcount
      chapter_index += 1
    end
    c = chapters[chapter_index-1]
    idx << ["#{c.book} #{c.chapter}", j]
    # puts "[#{chapters[chapter_index-1].book} #{chapters[chapter_index-1].chapter},#{j}"
    j += 1
  end

  puts idx.to_json
end