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
- #<<(word, flags: []) ⇒ Object
-
#add_word(word, flags: []) ⇒ Boolean
abstract
Add a word to the dictionary.
- #all_words ⇒ Object
- #contains?(word) ⇒ Boolean
-
#each_word {|word| ... } ⇒ Enumerator
Iterate over all words.
-
#empty? ⇒ Boolean
Check if the dictionary is empty.
- #has_word?(word) ⇒ Boolean
- #include?(word) ⇒ Boolean
-
#initialize(language_code, locale: nil, metadata: {}) ⇒ Base
constructor
Create a new dictionary.
-
#lookup(word) ⇒ Boolean
abstract
Check if a word exists in the dictionary.
-
#lookup?(word) ⇒ Boolean
Check if a word exists in the dictionary (alias for lookup).
-
#path ⇒ String?
On-disk path for file-backed dictionaries.
-
#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>
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.
48 49 50 51 52 53 54 |
# File 'lib/kotoshu/dictionary/base.rb', line 48 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.
41 42 43 |
# File 'lib/kotoshu/dictionary/base.rb', line 41 def @metadata end |
Class Method Details
.load(type, *args) ⇒ Base
Load a dictionary by type.
220 221 222 223 224 225 |
# File 'lib/kotoshu/dictionary/base.rb', line 220 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.
203 204 205 |
# File 'lib/kotoshu/dictionary/base.rb', line 203 def self.register_type(type_key) Kotoshu::Dictionary.register_type(type_key, self) end |
.registry ⇒ Hash
Class-level registry for dictionary types.
210 211 212 |
# File 'lib/kotoshu/dictionary/base.rb', line 210 def self.registry @registry ||= {} end |
Instance Method Details
#<<(word, flags: []) ⇒ Object
113 114 115 |
# File 'lib/kotoshu/dictionary/base.rb', line 113 def <<(word, flags: []) add_word(word, flags: flags) end |
#add_word(word, flags: []) ⇒ Boolean
Subclasses must implement this method.
Add a word to the dictionary.
109 110 111 |
# File 'lib/kotoshu/dictionary/base.rb', line 109 def add_word(word, flags: []) raise NotImplementedError, "#{self.class} must implement #add_word" end |
#all_words ⇒ Object
136 137 138 |
# File 'lib/kotoshu/dictionary/base.rb', line 136 def all_words words end |
#contains?(word) ⇒ Boolean
87 88 89 |
# File 'lib/kotoshu/dictionary/base.rb', line 87 def contains?(word) lookup(word) end |
#each_word {|word| ... } ⇒ Enumerator
Iterate over all words.
160 161 162 163 164 |
# File 'lib/kotoshu/dictionary/base.rb', line 160 def each_word(&) return enum_for(:each_word) unless block_given? words.each(&) end |
#empty? ⇒ Boolean
Check if the dictionary is empty.
152 153 154 |
# File 'lib/kotoshu/dictionary/base.rb', line 152 def empty? size.zero? end |
#has_word?(word) ⇒ Boolean
79 80 81 |
# File 'lib/kotoshu/dictionary/base.rb', line 79 def has_word?(word) lookup(word) end |
#include?(word) ⇒ Boolean
83 84 85 |
# File 'lib/kotoshu/dictionary/base.rb', line 83 def include?(word) lookup(word) end |
#lookup(word) ⇒ Boolean
Subclasses must implement this method.
Check if a word exists in the dictionary.
62 63 64 |
# File 'lib/kotoshu/dictionary/base.rb', line 62 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).
Dispatches dynamically to whatever lookup the concrete subclass
defines. (Ruby's alias keyword would bind to Base#lookup at
definition time and bypass subclass overrides — these one-line
method definitions are what makes the alias actually work.)
75 76 77 |
# File 'lib/kotoshu/dictionary/base.rb', line 75 def lookup?(word) lookup(word) end |
#path ⇒ String?
Returns On-disk path for file-backed dictionaries.
Nil for in-memory backends (e.g. Custom). Subclasses that load
from a file override this with their own attr_reader :path.
36 37 38 |
# File 'lib/kotoshu/dictionary/base.rb', line 36 def path nil end |
#remove_word(word) ⇒ Boolean
Subclasses must implement this method.
Remove a word from the dictionary.
123 124 125 |
# File 'lib/kotoshu/dictionary/base.rb', line 123 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.
143 144 145 |
# File 'lib/kotoshu/dictionary/base.rb', line 143 def size words.length end |
#suggest(word, max_suggestions: 10) ⇒ Array<String>
Subclasses must implement this method.
Generate spelling suggestions for a word.
98 99 100 |
# File 'lib/kotoshu/dictionary/base.rb', line 98 def suggest(word, max_suggestions: 10) raise NotImplementedError, "#{self.class} must implement #suggest" end |
#to_s ⇒ String Also known as: inspect
Convert to string.
185 186 187 |
# File 'lib/kotoshu/dictionary/base.rb', line 185 def to_s "#{self.class.name}(language: #{@language_code}, size: #{size})" end |
#type ⇒ Symbol
Dictionary type identifier.
193 194 195 |
# File 'lib/kotoshu/dictionary/base.rb', line 193 def type self.class.name.split("::").last.gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym end |
#words ⇒ Array<String>
Subclasses must implement this method.
Get all words in the dictionary.
132 133 134 |
# File 'lib/kotoshu/dictionary/base.rb', line 132 def words raise NotImplementedError, "#{self.class} must implement #words" end |
#words_matching(pattern) ⇒ Array<String>
Get words matching a pattern.
178 179 180 |
# File 'lib/kotoshu/dictionary/base.rb', line 178 def words_matching(pattern) words.select { |w| w.match?(pattern) } end |
#words_with_prefix(prefix) ⇒ Array<String>
Get words starting with a prefix.
170 171 172 |
# File 'lib/kotoshu/dictionary/base.rb', line 170 def words_with_prefix(prefix) words.select { |w| w.start_with?(prefix) } end |