Class: Kotoshu::Dictionary::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/dictionary/base.rb

Overview

Note:

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.

Examples:

Implementing a custom dictionary

class MyDictionary < Base
  def initialize(path, language_code:, locale: nil)
    super(language_code, locale: locale)
    @words = load_words(path)
  end

  def lookup(word)
    @words.include?(word.downcase)
  end

  # ... implement other abstract methods
end

Direct Known Subclasses

CSpell, Custom, Hunspell, PlainText, UnixWords

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language_code, locale: nil, metadata: {}) ⇒ Base

Create a new dictionary.

Parameters:

  • language_code (String)

    The language code (e.g., "en-US")

  • locale (String, nil) (defaults to: nil)

    The locale (optional)

  • metadata (Hash) (defaults to: {})

    Additional metadata (optional)

Raises:

  • (ArgumentError)


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_codeString (readonly)

Returns The language code (e.g., "en-US", "en-GB").

Returns:

  • (String)

    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

#localeString? (readonly)

Returns The locale (e.g., "en", "en_US").

Returns:

  • (String, nil)

    The locale (e.g., "en", "en_US")



31
32
33
# File 'lib/kotoshu/dictionary/base.rb', line 31

def locale
  @locale
end

#metadataHash (readonly)

Returns Additional metadata.

Returns:

  • (Hash)

    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.

Parameters:

  • type (Symbol)

    The dictionary type

  • args (Array)

    Arguments to pass to constructor

Returns:

  • (Base)

    The loaded dictionary

Raises:



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.

Examples:

Registering a custom dictionary type

MyDictionary.register_type(:my_custom)

Parameters:

  • type_key (Symbol)

    The type key to register as



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

.registryHash

Class-level registry for dictionary types.

Returns:

  • (Hash)

    Registry of type keys to classes



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

This method is abstract.

Subclasses must implement this method.

Add a word to the dictionary.

Parameters:

  • word (String)

    The word to add

  • flags (Array<String>) (defaults to: [])

    Morphological flags (optional)

Returns:

  • (Boolean)

    True if the word was added

Raises:

  • (NotImplementedError)

    Subclass must implement



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_wordsObject



136
137
138
# File 'lib/kotoshu/dictionary/base.rb', line 136

def all_words
  words
end

#contains?(word) ⇒ Boolean

Returns:

  • (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.

Yields:

  • (word)

    Each word

Returns:

  • (Enumerator)

    Enumerator if no block given



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.

Returns:

  • (Boolean)

    True if empty



152
153
154
# File 'lib/kotoshu/dictionary/base.rb', line 152

def empty?
  size.zero?
end

#has_word?(word) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/kotoshu/dictionary/base.rb', line 79

def has_word?(word)
  lookup(word)
end

#include?(word) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/kotoshu/dictionary/base.rb', line 83

def include?(word)
  lookup(word)
end

#lookup(word) ⇒ Boolean

This method is abstract.

Subclasses must implement this method.

Check if a word exists in the dictionary.

Parameters:

  • word (String)

    The word to look up

Returns:

  • (Boolean)

    True if the word exists

Raises:

  • (NotImplementedError)

    Subclass must implement



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

Parameters:

  • word (String)

    The word to look up

Returns:

  • (Boolean)

    True if the word exists



75
76
77
# File 'lib/kotoshu/dictionary/base.rb', line 75

def lookup?(word)
  lookup(word)
end

#pathString?

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.

Returns:

  • (String, nil)

    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

This method is abstract.

Subclasses must implement this method.

Remove a word from the dictionary.

Parameters:

  • word (String)

    The word to remove

Returns:

  • (Boolean)

    True if the word was removed

Raises:

  • (NotImplementedError)

    Subclass must implement



123
124
125
# File 'lib/kotoshu/dictionary/base.rb', line 123

def remove_word(word)
  raise NotImplementedError, "#{self.class} must implement #remove_word"
end

#sizeInteger Also known as: count, length

Get the number of words in the dictionary.

Returns:

  • (Integer)

    Word count



143
144
145
# File 'lib/kotoshu/dictionary/base.rb', line 143

def size
  words.length
end

#suggest(word, max_suggestions: 10) ⇒ Array<String>

This method is abstract.

Subclasses must implement this method.

Generate spelling suggestions for a word.

Parameters:

  • word (String)

    The misspelled word

  • max_suggestions (Integer) (defaults to: 10)

    Maximum number of suggestions

Returns:

  • (Array<String>)

    List of suggested words

Raises:

  • (NotImplementedError)

    Subclass must implement



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_sString Also known as: inspect

Convert to string.

Returns:

  • (String)

    String representation



185
186
187
# File 'lib/kotoshu/dictionary/base.rb', line 185

def to_s
  "#{self.class.name}(language: #{@language_code}, size: #{size})"
end

#typeSymbol

Dictionary type identifier.

Returns:

  • (Symbol)

    The dictionary type



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

#wordsArray<String>

This method is abstract.

Subclasses must implement this method.

Get all words in the dictionary.

Returns:

  • (Array<String>)

    All words

Raises:

  • (NotImplementedError)

    Subclass must implement



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.

Parameters:

  • pattern (Regexp)

    The pattern

Returns:

  • (Array<String>)

    Matching words



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.

Parameters:

  • prefix (String)

    The prefix

Returns:

  • (Array<String>)

    Words with the 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