class Sakuru::Database

Attributes

files[R]

Public Class Methods

new() click to toggle source
# File lib/sakuru/database.rb, line 7
def initialize
  @inverted_index = {}
  @files = []
end

Public Instance Methods

add(path) click to toggle source
# File lib/sakuru/database.rb, line 12
def add(path)
  if @files.include?(path)
    id = @files.index(path)
    @inverted_index.each do |key, posting_list|
      @inverted_index[key].delete(id)
    end
  else
    @files << path
    id = @files.index(path)
  end

  open(path) do |file|
    file.each_line do |line|
      # TODO: normalize and tokenize.
      line.split(/\s+/).each do |word|
        next if word.empty?
        @inverted_index[word] ||= []
        # TODO: add position
        @inverted_index[word] << id
      end
    end
  end
end
load(saved_file_path) click to toggle source
# File lib/sakuru/database.rb, line 59
def load(saved_file_path)
  data = JSON.load(File.read(saved_file_path))
  @files = data["files"]
  @inverted_index = data["inverted_index"]
end
save(output_path) click to toggle source
# File lib/sakuru/database.rb, line 49
def save(output_path)
  data = {
    "files" => @files,
    "inverted_index" => @inverted_index,
  }
  File.open(output_path, "w") do |file|
    JSON.dump(data, file)
  end
end