Class: Kotoshu::Core::Trie::Builder
- Inherits:
-
Object
- Object
- Kotoshu::Core::Trie::Builder
- Defined in:
- lib/kotoshu/core/trie/builder.rb
Overview
Builder class for constructing Trie objects. Provides a fluent interface for building tries from various sources.
Class Method Summary collapse
-
.from_array(words) ⇒ Trie
Build a trie from an array of words (class method).
-
.from_file(path) ⇒ Trie
Build a trie from a file path (class method).
-
.from_hash(hash) ⇒ Trie
Build a trie from a hash (class method).
-
.from_string(text) ⇒ Trie
Build a trie from a string (class method).
Instance Method Summary collapse
-
#add_word(word, payload = nil) ⇒ Builder
(also: #<<)
Add a single word to the trie.
-
#add_words(words) ⇒ Builder
Add multiple words to the trie.
-
#build ⇒ Trie
Get the built trie.
-
#from_array(array) ⇒ Builder
Build a trie from an array of words.
-
#from_file(path) ⇒ Builder
Build a trie from a file (one word per line).
-
#from_hash(hash) ⇒ Builder
Build a trie from a hash (word => payload mapping).
-
#from_string(text) ⇒ Builder
Build a trie from a string (newline-separated words).
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
Class Method Details
.from_array(words) ⇒ Trie
Build a trie from an array of words (class method).
97 98 99 |
# File 'lib/kotoshu/core/trie/builder.rb', line 97 def self.from_array(words) new.from_array(words).build end |
.from_file(path) ⇒ Trie
Build a trie from a file path (class method).
89 90 91 |
# File 'lib/kotoshu/core/trie/builder.rb', line 89 def self.from_file(path) new.from_file(path).build end |
.from_hash(hash) ⇒ Trie
Build a trie from a hash (class method).
105 106 107 |
# File 'lib/kotoshu/core/trie/builder.rb', line 105 def self.from_hash(hash) new.from_hash(hash).build end |
.from_string(text) ⇒ Trie
Build a trie from a string (class method).
113 114 115 |
# File 'lib/kotoshu/core/trie/builder.rb', line 113 def self.from_string(text) new.from_string(text).build end |
Instance Method Details
#add_word(word, payload = nil) ⇒ Builder Also known as: <<
Add a single word to the trie.
18 19 20 21 |
# File 'lib/kotoshu/core/trie/builder.rb', line 18 def add_word(word, payload = nil) @trie.insert(word, payload) self end |
#add_words(words) ⇒ Builder
Add multiple words to the trie.
28 29 30 31 |
# File 'lib/kotoshu/core/trie/builder.rb', line 28 def add_words(words) words.each { |word| add_word(word) } self end |
#build ⇒ Trie
Get the built trie.
81 82 83 |
# File 'lib/kotoshu/core/trie/builder.rb', line 81 def build @trie.freeze end |
#from_array(array) ⇒ Builder
Build a trie from an array of words.
46 47 48 49 |
# File 'lib/kotoshu/core/trie/builder.rb', line 46 def from_array(array) add_words(array) self end |
#from_file(path) ⇒ Builder
Build a trie from a file (one word per line).
55 56 57 58 59 60 61 62 |
# File 'lib/kotoshu/core/trie/builder.rb', line 55 def from_file(path) File.foreach(path, chomp: true) do |line| next if line.empty? || line.start_with?("#") add_word(line) end self end |
#from_hash(hash) ⇒ Builder
Build a trie from a hash (word => payload mapping).
37 38 39 40 |
# File 'lib/kotoshu/core/trie/builder.rb', line 37 def from_hash(hash) hash.each { |word, payload| add_word(word, payload) } self end |
#from_string(text) ⇒ Builder
Build a trie from a string (newline-separated words).
68 69 70 71 72 73 74 75 76 |
# File 'lib/kotoshu/core/trie/builder.rb', line 68 def from_string(text) text.each_line do |line| word = line.strip next if word.empty? || word.start_with?("#") add_word(word) end self end |