class Spellr::Wordlist
Attributes
name[R]
path[R]
Public Class Methods
new(file, name: file)
click to toggle source
# File lib/spellr/wordlist.rb, line 13 def initialize(file, name: file) path = @file = file @path = Spellr.pwd.join('.spellr_wordlists').join(path).expand_path @name = name @include = {} end
Public Instance Methods
<<(term)
click to toggle source
# File lib/spellr/wordlist.rb, line 39 def <<(term) term = term.spellr_normalize touch @include[term] = true insert_sorted(term) @path.write(words.join) # we don't need to clear the cache end
clean(file = @path)
click to toggle source
# File lib/spellr/wordlist.rb, line 52 def clean(file = @path) require_relative 'tokenizer' write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms.join) end
each(&block)
click to toggle source
# File lib/spellr/wordlist.rb, line 20 def each(&block) words.each(&block) end
exist?()
click to toggle source
# File lib/spellr/wordlist.rb, line 63 def exist? return @exist if defined?(@exist) @exist = @path.exist? end
include?(term)
click to toggle source
significantly faster than default Enumerable#include? requires terms to have been sorted
# File lib/spellr/wordlist.rb, line 32 def include?(term) term = term.spellr_normalize @include.fetch(term) do @include[term] = words.bsearch { |value| term <=> value } end end
inspect()
click to toggle source
:nocov:
# File lib/spellr/wordlist.rb, line 25 def inspect "#<#{self.class.name}:#{@path}>" end
length()
click to toggle source
# File lib/spellr/wordlist.rb, line 77 def length to_a.length end
touch()
click to toggle source
# File lib/spellr/wordlist.rb, line 69 def touch return if exist? @path.dirname.mkpath @path.write('') clear_cache end
words()
click to toggle source
# File lib/spellr/wordlist.rb, line 47 def words @words ||= (exist? ? @path.readlines : []) end
Also aliased as: to_a
write(content)
click to toggle source
# File lib/spellr/wordlist.rb, line 57 def write(content) @path.write(content) clear_cache end
Private Instance Methods
clear_cache()
click to toggle source
# File lib/spellr/wordlist.rb, line 88 def clear_cache @words = nil @include = {} remove_instance_variable(:@exist) if defined?(@exist) end
insert_sorted(term)
click to toggle source
# File lib/spellr/wordlist.rb, line 83 def insert_sorted(term) insert_at = words.bsearch_index { |value| value >= term } insert_at ? words.insert(insert_at, term) : words.push(term) end