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)


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_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



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.

Parameters:

  • type (Symbol)

    The dictionary type

  • args (Array)

    Arguments to pass to constructor

Returns:

  • (Base)

    The loaded dictionary

Raises:



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.

Examples:

Registering a custom dictionary type

MyDictionary.register_type(:my_custom)

Parameters:

  • type_key (Symbol)

    The type key to register as



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

.registryHash

Class-level registry for dictionary types.

Returns:

  • (Hash)

    Registry of type keys to classes



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: <<

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



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.

Yields:

  • (word)

    Each word

Returns:

  • (Enumerator)

    Enumerator if no block given



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.

Returns:

  • (Boolean)

    True if 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?

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



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

Parameters:

  • word (String)

    The word to look up

Returns:

  • (Boolean)

    True if the word exists



63
64
65
# File 'lib/kotoshu/dictionary/base.rb', line 63

def lookup?(word)
  lookup(word)
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



100
101
102
# File 'lib/kotoshu/dictionary/base.rb', line 100

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



117
118
119
# File 'lib/kotoshu/dictionary/base.rb', line 117

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



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

Convert to string.

Returns:

  • (String)

    String representation



159
160
161
# File 'lib/kotoshu/dictionary/base.rb', line 159

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

#typeSymbol

Dictionary type identifier.

Returns:

  • (Symbol)

    The dictionary type



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

#wordsArray<String> Also known as: all_words

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



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.

Parameters:

  • pattern (Regexp)

    The pattern

Returns:

  • (Array<String>)

    Matching words



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.

Parameters:

  • prefix (String)

    The prefix

Returns:

  • (Array<String>)

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