Class: Spellr::Wordlist
- Inherits:
-
Object
- Object
- Spellr::Wordlist
- Includes:
- Enumerable
- Defined in:
- lib/spellr.rb,
lib/spellr/wordlist.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #<<(term) ⇒ Object
- #clean(file = @path) ⇒ Object
- #clear_cache ⇒ Object
- #each(&block) ⇒ Object
- #exist? ⇒ Boolean
-
#include?(term) ⇒ Boolean
significantly faster than default Enumerable#include? requires terms to have been sorted.
-
#initialize(file, name: file) ⇒ Wordlist
constructor
A new instance of Wordlist.
- #insert_sorted(term) ⇒ Object
-
#inspect ⇒ Object
:nocov:.
- #length ⇒ Object
- #touch ⇒ Object
- #words ⇒ Object (also: #to_a)
- #write(words, clear_cache: true) ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/spellr/wordlist.rb', line 11 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/spellr/wordlist.rb', line 11 def path @path end |
Instance Method Details
#<<(term) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/spellr/wordlist.rb', line 39 def <<(term) term = term.spellr_normalize touch @include[term] = true insert_sorted(term) write(words, clear_cache: false) end |
#clean(file = @path) ⇒ Object
56 57 58 59 |
# File 'lib/spellr/wordlist.rb', line 56 def clean(file = @path) require_relative 'tokenizer' write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms) end |
#clear_cache ⇒ Object
91 92 93 94 95 |
# File 'lib/spellr/wordlist.rb', line 91 def clear_cache @words = nil @include = {} remove_instance_variable(:@exist) if defined?(@exist) end |
#each(&block) ⇒ Object
20 21 22 |
# File 'lib/spellr/wordlist.rb', line 20 def each(&block) words.each(&block) end |
#exist? ⇒ Boolean
69 70 71 72 73 |
# File 'lib/spellr/wordlist.rb', line 69 def exist? return @exist if defined?(@exist) @exist = @path.exist? end |
#include?(term) ⇒ Boolean
significantly faster than default Enumerable#include? requires terms to have been sorted
32 33 34 35 36 37 |
# 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 |
#insert_sorted(term) ⇒ Object
86 87 88 89 |
# File 'lib/spellr/wordlist.rb', line 86 def insert_sorted(term) insert_at = words.bsearch_index { |value| value >= term } insert_at ? words.insert(insert_at, term) : words.push(term) end |
#inspect ⇒ Object
:nocov:
25 26 27 |
# File 'lib/spellr/wordlist.rb', line 25 def inspect "#<#{self.class.name}:#{@path}>" end |
#length ⇒ Object
82 83 84 |
# File 'lib/spellr/wordlist.rb', line 82 def length to_a.length end |
#touch ⇒ Object
75 76 77 78 79 80 |
# File 'lib/spellr/wordlist.rb', line 75 def touch return if exist? @path.dirname.mkpath write([]) end |
#words ⇒ Object Also known as: to_a
47 48 49 50 51 52 53 |
# File 'lib/spellr/wordlist.rb', line 47 def words @words ||= if exist? @path.readlines(chomp: true) else [] end end |
#write(words, clear_cache: true) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/spellr/wordlist.rb', line 61 def write(words, clear_cache: true) content = words.join("\n") content << "\n" unless words.empty? @path.write(content) self.clear_cache if clear_cache end |