Class: RobotLab::Durable::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/durable/store.rb

Constant Summary collapse

DEFAULT_PATH =
File.join(Dir.home, ".robot_lab", "durable")
MIN_WORD_LENGTH =
3

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_PATH) ⇒ Store

Returns a new instance of Store.



13
14
15
16
# File 'lib/robot_lab/durable/store.rb', line 13

def initialize(path: DEFAULT_PATH)
  @path = path
  FileUtils.mkdir_p(@path)
end

Instance Method Details

#confirm(entry) ⇒ Entry

Increment confidence and use_count on a stored entry.

Parameters:

Returns:

  • (Entry)

    the updated entry



59
60
61
62
63
# File 'lib/robot_lab/durable/store.rb', line 59

def confirm(entry)
  updated = entry.confirm
  record_exact(updated)
  updated
end

#recall(query:, domain: nil, min_confidence: 0.0) ⇒ Array<Entry>

Return entries matching query keywords, sorted by descending confidence.

Parameters:

  • query (String)

    natural-language search string

  • domain (String, nil) (defaults to: nil)

    restrict to one domain file; nil searches all

  • min_confidence (Float) (defaults to: 0.0)

    exclude entries below this threshold

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/robot_lab/durable/store.rb', line 24

def recall(query:, domain: nil, min_confidence: 0.0)
  entries = domain ? load_domain(domain) : load_all
  words   = tokenize(query)

  entries
    .select { |e| e.confidence >= min_confidence }
    .select { |e| matches?(e, words) }
    .sort_by { |e| -e.confidence }
end

#record(entry) ⇒ Entry

Persist a new entry. If an entry with the same content already exists in the domain file, increment its confidence and use_count instead.

Parameters:

Returns:

  • (Entry)

    the stored entry (may differ if an existing one was updated)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/robot_lab/durable/store.rb', line 39

def record(entry)
  with_domain_lock(entry.domain) do
    entries = load_domain(entry.domain)
    idx     = entries.find_index { |e| e.content.downcase == entry.content.downcase }

    if idx
      entries[idx] = entries[idx].confirm
    else
      entries << entry
    end

    save_domain(entry.domain, entries)
    entries[idx || -1]
  end
end