Class: BibleTools::Analyse

Inherits:
Object
  • Object
show all
Defined in:
lib/bibletools.rb

Instance Method Summary collapse

Constructor Details

#initialize(bible_obj, tts: nil, debug: false) ⇒ Analyse

Returns a new instance of Analyse.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bibletools.rb', line 165

def initialize(bible_obj, tts: nil, debug: false)
  
  @debug = debug
  
  if bible_obj then
    
    @doc = bible_obj.to_doc
    @check = EnglishSpellcheck.new debug: false #verbose: false
    
    if tts then
      @cerevoice = Cerecvoice2019.new accountid: tts[:account], 
          password: tts[:password], voice: tts[:voice] || 'Stuart'
    end
    
  end
        
  
end

Instance Method Details

#asr(chapters: []) ⇒ Object

Automatically select verses; verses are selected based upon word frequency



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
# File 'lib/bibletools.rb', line 187

def asr(chapters: [])
  
  r = if chapters.any? then
    
    doc = Rexle.new('<book/>')
            
    chapters.each do |n|
      e2 = @doc.root.element("chapter[@no='#{n.to_s}']")
      doc.root.add e2 if e2
    end
    
    return doc
    
    keyword = wordtally(doc: doc).keys.first
    #return keyword
    #get the wordstally for the chapters
    assoc_r(keyword, doc)
    
  else
    assoc_r()
  end
  
  e = r[1].root.element('chapter/verse')
  verse_no = e.attributes[:no]
  chptr_no = e.parent.attributes[:no]
  
  # we are backtracking the results to hopefully find a new branch to explore
  trail = r.first

  #return r
  a, trail2 = mine_words(r, trail, level: 2)
  a << [chptr_no, verse_no,  e.text, 1]
  puts 'a: ' + a.inspect
  #return a
  h = a.group_by(&:first)
  a2 = h.sort_by {|x, _| x.to_i}
  @verses = a2.map do |chapter, verses|
    [chapter, verses.sort_by {|_,x| x.to_i}]
  end
    
  [@verses, trail2]
end

#assoc(keyword, doc = @doc) ⇒ Object

find the words associated with a given keyword



232
233
234
235
# File 'lib/bibletools.rb', line 232

def assoc(keyword, doc=@doc)
  doc2 = self.search keyword, doc
  [self.words(doc: doc2), doc2]
end

#assoc_r(keyword = words().keys.first, doc = @doc, list = [], results = []) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/bibletools.rb', line 237

def assoc_r(keyword=words().keys.first, doc=@doc, list=[], results=[])
  
  result = assoc(keyword, doc)
  results << result
  h, doc2 = result
  new_keyword = h.keys.find {|x| not list.include? x}
  return [list, doc2, results] unless new_keyword
  
  list << new_keyword
  assoc_r(new_keyword, doc2, list, results)
  
end

#custom_words(doc: @doc, level: 3) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/bibletools.rb', line 250

def custom_words(doc: @doc, level: 3)
        
  a = wordtally(level: level, doc: doc).keys.map do |word|
    puts 'word: ' + word.inspect if @debug
    [word, @check.spelling(word, verbose: false)]
  end
  
  a2 = a.select do |w, x| 
    if x.is_a? String then
      w != x
    elsif x.is_a? Array
      not (w == x.join or w == x.first)
    else
      true
    end
  end
  a2.map(&:first)
  
end

#read(chapter, verse) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/bibletools.rb', line 279

def read(chapter, verse)
  
  if verse.is_a? Integer then
    [@doc.root.element("chapter[@no='#{chapter}']/verse[@no='#{verse}']")]
  elsif verse.is_a? Array
    verse.map do |x|
      @doc.root.element("chapter[@no='#{chapter}']/verse[@no='#{x}']")
    end
  end
end

#search(keyword, doc = @doc) ⇒ Object



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
# File 'lib/bibletools.rb', line 291

def search(keyword, doc=@doc)
  
  verses = doc.root.xpath('chapter/verse')
  puts 'verses: ' + verses.inspect
  a = verses.select {|x| x.text =~ /#{keyword}/i}
  
  a2 = a.map.with_index do |verse, i|
    
    #puts 'verse: ' + verse.xml.inspect
    txt = verse.text
    
    puts 'txt: ' + txt.inspect if @debug
    
    n = 0
    chapter = verse.parent.attributes[:no]
    
    until txt.rstrip[/[\.\?!]\W*$/] or n > 2 do
      n +=1
      puts 'n: ' + n.inspect
      nverse = read(chapter, verse.attributes[:no].to_i + n)[0]
      puts 'nverse: ' + nverse.inspect
      puts 'nverse: ' + nverse&.text.inspect if @debug
      txt += nverse.text if nverse
    end 
    
    if @debug then
      puts 'n: ' + n.inspect
      puts '_txt: ' + txt.inspect
    end
    
    [verse.parent.attributes[:no], verse.attributes[:no], txt]
  end

  # group by chapter
  #
  h = a2.group_by(&:first)      
  
  doc = Rexle.new('<book/>')

  h.each do |key, value|

    echapter = Rexle::Element.new('chapter', attributes: {no: key})

    value.each do |_, verse_no, verse|
      everse = Rexle::Element.new('verse', attributes: {no: verse_no}).add_text verse
      echapter.add everse
    end

    doc.root.add echapter

  end
  
  return doc
  
end

#tts(chapter, verse) ⇒ Object



347
348
349
350
351
352
# File 'lib/bibletools.rb', line 347

def tts(chapter, verse)
  
  verses = read(chapter, verse).map(&:text).join(' ')
  @cerevoice.tts verses, out: 'tts.ogg'      
  
end

#verses(level: nil, html: false, title: nil) ⇒ Object



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
# File 'lib/bibletools.rb', line 354

def verses(level: nil, html: false, title: nil)                  

  a = @verses.map {|_, body| body}.flatten(1)
  a1 = level ? a.select {|c,v,t,l| l <= level} : a.map {|c,v,t,l| [c,v,t]}
  a2 = a1.map {|c,v,t,l| [c,v,t]}.uniq
  h = a2.group_by(&:first)
  
  puts 'html: ' + html.inspect
  return h unless html == true
  
  doc = Rexle.new('<html/>')


  head = Rexle::Element.new('head')
  head.add(Rexle::Element.new('h1').add_text(title) ) if title

  style = Rexle::Element.new('style')
  style.add_text '     ins {font-size: 0.8em;padding: 0.8em;}'
  head.add style

  doc.root.add head
  body = Rexle::Element.new('body')

  h.each do |chptr, verses|

    h2 = Rexle::Element.new('h2').add_text chptr
    body.add h2

    verses.each do |cno, vno, text|

      ins = Rexle::Element.new('ins').add_text vno
      para = Rexle::Element.new('p')
      para.add ins
      para.add_text text
      body.add para
      
    end
    
  end
  
  doc.root.add body

  doc.root.xml pretty: true
  
end

#wordtally(level: 2, doc: @doc) ⇒ Object Also known as: words



270
271
272
273
274
275
# File 'lib/bibletools.rb', line 270

def wordtally(level: 2, doc: @doc)
  
  return unless doc
  Yawc.new(doc.root.plaintext, level: level).to_h
  
end