Class: Kotoshu::Core::Trie::Builder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



9
10
11
# File 'lib/kotoshu/core/trie/builder.rb', line 9

def initialize
  @trie = Trie.new
end

Class Method Details

.from_array(words) ⇒ Trie

Build a trie from an array of words (class method).

Parameters:

  • words (Array<String>)

    Array of words

Returns:

  • (Trie)

    The constructed trie



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).

Parameters:

  • path (String)

    Path to the file

Returns:

  • (Trie)

    The constructed trie



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).

Parameters:

  • hash (Hash)

    Hash of words to payloads

Returns:

  • (Trie)

    The constructed trie



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).

Parameters:

  • text (String)

    String containing words

Returns:

  • (Trie)

    The constructed trie



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.

Parameters:

  • word (String)

    The word to add

  • payload (Object) (defaults to: nil)

    Optional payload

Returns:



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.

Parameters:

  • words (Array<String>)

    Array of words to add

Returns:



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

#buildTrie

Get the built trie.

Returns:

  • (Trie)

    The constructed 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.

Parameters:

  • array (Array<String>)

    Array of words

Returns:



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).

Parameters:

  • path (String)

    Path to the file

Returns:



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).

Parameters:

  • hash (Hash)

    Hash of words to payloads

Returns:



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).

Parameters:

  • text (String)

    String containing words

Returns:



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