Class: RobotLab::Durable::Store
- Inherits:
-
Object
- Object
- RobotLab::Durable::Store
- 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
-
#confirm(entry) ⇒ Entry
Increment confidence and use_count on a stored entry.
-
#initialize(path: DEFAULT_PATH) ⇒ Store
constructor
A new instance of Store.
-
#recall(query:, domain: nil, min_confidence: 0.0) ⇒ Array<Entry>
Return entries matching query keywords, sorted by descending confidence.
-
#record(entry) ⇒ Entry
Persist a new entry.
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.
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.
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.
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 |