Class: Sakuru::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



7
8
9
10
# File 'lib/sakuru/database.rb', line 7

def initialize
  @inverted_index = {}
  @files = []
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/sakuru/database.rb', line 6

def files
  @files
end

Instance Method Details

#add(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



59
60
61
62
63
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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

#search(query) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sakuru/database.rb', line 36

def search(query)
  results = {}
  # TODO: normalize and tokenize.
  ids = @inverted_index[query]
  return results unless ids
  ids.each do |id|
    file = @files[id]
    results[file] ||= 0
    results[file] += 1
  end
  results
end