class Shookach::Indexer

Public Class Methods

new(library_path, output_path) click to toggle source
# File lib/shookach/indexer.rb, line 3
def initialize(library_path, output_path)
  @library_path = library_path
  create_index_file(output_path)
end

Public Instance Methods

call() click to toggle source
# File lib/shookach/indexer.rb, line 8
def call
  write_dir_indexes

  @output_file.close
end

Private Instance Methods

create_index_file(path) click to toggle source
# File lib/shookach/indexer.rb, line 16
def create_index_file(path)
  Dir.mkdir(path) unless File.exists?(path)

  @output_file = File.open("#{path}indexes#{Time.now.strftime('%Y%m%d%H%M%S')}.yml", 'w')
end
sentences_arr() click to toggle source
# File lib/shookach/indexer.rb, line 50
def sentences_arr
  @file.read().gsub(',', ' ').gsub(/\n|\r/, ' ').split(/\.\s*/)
end
write_dir_indexes() click to toggle source
# File lib/shookach/indexer.rb, line 22
def write_dir_indexes
  puts "Indexing directory: #{@library_path}"

  Dir.foreach(@library_path) do |filename|
    next if filename == '.' || filename == '..'

    write_file_indexes(filename)
  end

  puts "Output file: #{@output_file.path}"
end
write_file_indexes(filename) click to toggle source
# File lib/shookach/indexer.rb, line 34
def write_file_indexes(filename)
  puts "Indexing file: #{filename}"

  @file = File.open("#{@library_path}/#{filename}", 'r')

  sentences_arr.each_with_index do |sentence, s_index|
    words = sentence.split(' ')

    words.each_with_index do |word, w_index|
      @output_file.write("#{word}:\r file: #{filename}\r sentence: #{s_index}\r word: #{w_index}\n")
    end
  end

  @file.close
end