Class: Kotoshu::Dictionary::Base
- Inherits:
-
Object
- Object
- Kotoshu::Dictionary::Base
- Defined in:
- lib/kotoshu/dictionary/base.rb
Overview
Subclasses must implement the abstract methods: #lookup, #suggest, #add_word, and #remove_word.
Base class for all dictionary backends.
This abstract class defines the interface that all dictionary implementations must follow.
Instance Attribute Summary collapse
-
#language_code ⇒ String
readonly
The language code (e.g., “en-US”, “en-GB”).
-
#locale ⇒ String?
readonly
The locale (e.g., “en”, “en_US”).
-
#metadata ⇒ Hash
readonly
Additional metadata.
Class Method Summary collapse
-
.load(type, *args) ⇒ Base
Load a dictionary by type.
-
.register_type(type_key) ⇒ Object
Register this dictionary type.
-
.registry ⇒ Hash
Class-level registry for dictionary types.
Instance Method Summary collapse
-
#add_word(word, flags: []) ⇒ Boolean
(also: #<<)
abstract
Add a word to the dictionary.
-
#each_word {|word| ... } ⇒ Enumerator
Iterate over all words.
-
#empty? ⇒ Boolean
Check if the dictionary is empty.
-
#initialize(language_code, locale: nil, metadata: {}) ⇒ Base
constructor
Create a new dictionary.
-
#lookup(word) ⇒ Boolean
(also: #has_word?, #include?, #contains?)
abstract
Check if a word exists in the dictionary.
-
#lookup?(word) ⇒ Boolean
Check if a word exists in the dictionary (alias for lookup).
-
#remove_word(word) ⇒ Boolean
abstract
Remove a word from the dictionary.
-
#size ⇒ Integer
(also: #count, #length)
Get the number of words in the dictionary.
-
#suggest(word, max_suggestions: 10) ⇒ Array<String>
abstract
Generate spelling suggestions for a word.
-
#to_s ⇒ String
(also: #inspect)
Convert to string.
-
#type ⇒ Symbol
Dictionary type identifier.
-
#words ⇒ Array<String>
(also: #all_words)
abstract
Get all words in the dictionary.
-
#words_matching(pattern) ⇒ Array<String>
Get words matching a pattern.
-
#words_with_prefix(prefix) ⇒ Array<String>
Get words starting with a prefix.
Constructor Details
#initialize(language_code, locale: nil, metadata: {}) ⇒ Base
Create a new dictionary.
41 42 43 44 45 46 47 |
# File 'lib/kotoshu/dictionary/base.rb', line 41 def initialize(language_code, locale: nil, metadata: {}) raise ArgumentError, "Language code cannot be empty" if language_code.nil? || language_code.empty? @language_code = language_code.dup.freeze @locale = locale&.dup&.freeze @metadata = .dup.freeze end |
Instance Attribute Details
#language_code ⇒ String (readonly)
Returns The language code (e.g., “en-US”, “en-GB”).
28 29 30 |
# File 'lib/kotoshu/dictionary/base.rb', line 28 def language_code @language_code end |
#locale ⇒ String? (readonly)
Returns The locale (e.g., “en”, “en_US”).
31 32 33 |
# File 'lib/kotoshu/dictionary/base.rb', line 31 def locale @locale end |
#metadata ⇒ Hash (readonly)
Returns Additional metadata.
34 35 36 |
# File 'lib/kotoshu/dictionary/base.rb', line 34 def @metadata end |
Class Method Details
.load(type, *args) ⇒ Base
Load a dictionary by type.
194 195 196 197 198 199 |
# File 'lib/kotoshu/dictionary/base.rb', line 194 def self.load(type, *args) klass = registry[type] raise ConfigurationError, "Unknown dictionary type: #{type}" unless klass klass.new(*args) end |
.register_type(type_key) ⇒ Object
Register this dictionary type.
177 178 179 |
# File 'lib/kotoshu/dictionary/base.rb', line 177 def self.register_type(type_key) Kotoshu::Dictionary.register_type(type_key, self) end |
.registry ⇒ Hash
Class-level registry for dictionary types.
184 185 186 |
# File 'lib/kotoshu/dictionary/base.rb', line 184 def self.registry @registry ||= {} end |
Instance Method Details
#add_word(word, flags: []) ⇒ Boolean Also known as: <<
Subclasses must implement this method.
Add a word to the dictionary.
89 90 91 |
# File 'lib/kotoshu/dictionary/base.rb', line 89 def add_word(word, flags: []) raise NotImplementedError, "#{self.class} must implement #add_word" end |
#each_word {|word| ... } ⇒ Enumerator
Iterate over all words.
134 135 136 137 138 |
# File 'lib/kotoshu/dictionary/base.rb', line 134 def each_word(&block) return enum_for(:each_word) unless block_given? words.each(&block) end |
#empty? ⇒ Boolean
Check if the dictionary is empty.
126 127 128 |
# File 'lib/kotoshu/dictionary/base.rb', line 126 def empty? size.zero? end |
#lookup(word) ⇒ Boolean Also known as: has_word?, include?, contains?
Subclasses must implement this method.
Check if a word exists in the dictionary.
55 56 57 |
# File 'lib/kotoshu/dictionary/base.rb', line 55 def lookup(word) raise NotImplementedError, "#{self.class} must implement #lookup" end |
#lookup?(word) ⇒ Boolean
Check if a word exists in the dictionary (alias for lookup).
63 64 65 |
# File 'lib/kotoshu/dictionary/base.rb', line 63 def lookup?(word) lookup(word) end |
#remove_word(word) ⇒ Boolean
Subclasses must implement this method.
Remove a word from the dictionary.
100 101 102 |
# File 'lib/kotoshu/dictionary/base.rb', line 100 def remove_word(word) raise NotImplementedError, "#{self.class} must implement #remove_word" end |
#size ⇒ Integer Also known as: count, length
Get the number of words in the dictionary.
117 118 119 |
# File 'lib/kotoshu/dictionary/base.rb', line 117 def size words.length end |
#suggest(word, max_suggestions: 10) ⇒ Array<String>
Subclasses must implement this method.
Generate spelling suggestions for a word.
78 79 80 |
# File 'lib/kotoshu/dictionary/base.rb', line 78 def suggest(word, max_suggestions: 10) raise NotImplementedError, "#{self.class} must implement #suggest" end |
#to_s ⇒ String Also known as: inspect
Convert to string.
159 160 161 |
# File 'lib/kotoshu/dictionary/base.rb', line 159 def to_s "#{self.class.name}(language: #{@language_code}, size: #{size})" end |
#type ⇒ Symbol
Dictionary type identifier.
167 168 169 |
# File 'lib/kotoshu/dictionary/base.rb', line 167 def type self.class.name.split("::").last.gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym end |
#words ⇒ Array<String> Also known as: all_words
Subclasses must implement this method.
Get all words in the dictionary.
109 110 111 |
# File 'lib/kotoshu/dictionary/base.rb', line 109 def words raise NotImplementedError, "#{self.class} must implement #words" end |
#words_matching(pattern) ⇒ Array<String>
Get words matching a pattern.
152 153 154 |
# File 'lib/kotoshu/dictionary/base.rb', line 152 def words_matching(pattern) words.select { |w| w.match?(pattern) } end |
#words_with_prefix(prefix) ⇒ Array<String>
Get words starting with a prefix.
144 145 146 |
# File 'lib/kotoshu/dictionary/base.rb', line 144 def words_with_prefix(prefix) words.select { |w| w.start_with?(prefix) } end |