Class: SpellKit::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/spellkit.rb

Overview

Reopen Rust-defined Checker class to add Ruby wrappers

Instance Method Summary collapse

Instance Method Details

#_rust_correctObject



110
# File 'lib/spellkit.rb', line 110

alias_method :_rust_correct, :correct

#_rust_correct?Object



109
# File 'lib/spellkit.rb', line 109

alias_method :_rust_correct?, :correct?

#_rust_correct_tokensObject



111
# File 'lib/spellkit.rb', line 111

alias_method :_rust_correct_tokens, :correct_tokens

#_rust_healthcheckObject



113
# File 'lib/spellkit.rb', line 113

alias_method :_rust_healthcheck, :healthcheck

#_rust_load!Object

Save original Rust methods



107
# File 'lib/spellkit.rb', line 107

alias_method :_rust_load!, :load!

#_rust_statsObject



112
# File 'lib/spellkit.rb', line 112

alias_method :_rust_stats, :stats

#_rust_suggestionsObject



108
# File 'lib/spellkit.rb', line 108

alias_method :_rust_suggestions, :suggestions

#correct(word) ⇒ Object



221
222
223
224
225
226
# File 'lib/spellkit.rb', line 221

def correct(word)
  raise SpellKit::InvalidArgumentError, "word cannot be nil" if word.nil?
  raise SpellKit::InvalidArgumentError, "word cannot be empty" if word.to_s.empty?

  _rust_correct(word)
end

#correct?(word) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



214
215
216
217
218
219
# File 'lib/spellkit.rb', line 214

def correct?(word)
  raise SpellKit::InvalidArgumentError, "word cannot be nil" if word.nil?
  raise SpellKit::InvalidArgumentError, "word cannot be empty" if word.to_s.empty?

  _rust_correct?(word)
end

#correct_tokens(tokens) ⇒ Object



228
229
230
231
232
# File 'lib/spellkit.rb', line 228

def correct_tokens(tokens)
  raise SpellKit::InvalidArgumentError, "tokens must be an Array" unless tokens.is_a?(Array)

  _rust_correct_tokens(tokens)
end

#healthcheckObject



238
239
240
# File 'lib/spellkit.rb', line 238

def healthcheck
  _rust_healthcheck
end

#load!(dictionary: nil, protected_path: nil, protected_patterns: [], edit_distance: 1, frequency_threshold: 10.0, skip_urls: false, skip_emails: false, skip_hostnames: false, skip_code_patterns: false, skip_numbers: false, **_options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/spellkit.rb', line 115

def load!(dictionary: nil, protected_path: nil, protected_patterns: [],
          edit_distance: 1, frequency_threshold: 10.0,
          skip_urls: false, skip_emails: false, skip_hostnames: false,
          skip_code_patterns: false, skip_numbers: false, **_options)

  # Validate dictionary parameter
  raise SpellKit::InvalidArgumentError, "dictionary parameter is required" if dictionary.nil?

  # Auto-detect URL vs path
  dictionary_path = if dictionary.to_s.start_with?("http://", "https://")
    download_dictionary(dictionary)
  else
    dictionary.to_s
  end

  # Validate file exists
  raise SpellKit::FileNotFoundError, "Dictionary file not found: #{dictionary_path}" unless File.exist?(dictionary_path)

  # Validate edit distance
  unless [1, 2].include?(edit_distance)
    raise SpellKit::InvalidArgumentError, "edit_distance must be 1 or 2, got: #{edit_distance}"
  end

  # Validate protected_patterns is an array
  unless protected_patterns.is_a?(Array)
    raise SpellKit::InvalidArgumentError, "protected_patterns must be an Array"
  end

  # Validate frequency_threshold
  unless frequency_threshold.is_a?(Numeric)
    raise SpellKit::InvalidArgumentError, "frequency_threshold must be a number, got: #{frequency_threshold.class}"
  end

  unless frequency_threshold.finite?
    raise SpellKit::InvalidArgumentError, "frequency_threshold must be finite (got NaN or Infinity)"
  end

  if frequency_threshold < 0
    raise SpellKit::InvalidArgumentError, "frequency_threshold must be non-negative, got: #{frequency_threshold}"
  end

  # Build skip patterns from convenience flags
  skip_patterns = build_skip_patterns(
    skip_urls: skip_urls,
    skip_emails: skip_emails,
    skip_hostnames: skip_hostnames,
    skip_code_patterns: skip_code_patterns,
    skip_numbers: skip_numbers
  )

  # Merge skip patterns with user-provided patterns
  all_patterns = skip_patterns + protected_patterns

  config = {
    "dictionary_path" => dictionary_path,
    "edit_distance" => edit_distance,
    "frequency_threshold" => frequency_threshold
  }

  config["protected_path"] = protected_path.to_s if protected_path

  # Convert Ruby Regex objects to hashes with flags for Rust
  if all_patterns.any?
    pattern_objects = all_patterns.map do |pattern|
      if pattern.is_a?(Regexp)
        # Extract flags from Regexp.options bitmask
        options = pattern.options
        {
          "source" => pattern.source,
          "case_insensitive" => (options & Regexp::IGNORECASE) != 0,
          "multiline" => (options & Regexp::MULTILINE) != 0,
          "extended" => (options & Regexp::EXTENDED) != 0
        }
      elsif pattern.is_a?(String)
        # Plain strings default to case-sensitive
        {
          "source" => pattern,
          "case_insensitive" => false,
          "multiline" => false,
          "extended" => false
        }
      else
        raise SpellKit::InvalidArgumentError, "protected_patterns must contain Regexp or String objects"
      end
    end
    config["protected_patterns"] = pattern_objects
  end

  _rust_load!(config)
  self
end

#statsObject



234
235
236
# File 'lib/spellkit.rb', line 234

def stats
  _rust_stats
end

#suggestions(word, max = 5) ⇒ Object



207
208
209
210
211
212
# File 'lib/spellkit.rb', line 207

def suggestions(word, max = 5)
  raise SpellKit::InvalidArgumentError, "word cannot be nil" if word.nil?
  raise SpellKit::InvalidArgumentError, "word cannot be empty" if word.to_s.empty?

  _rust_suggestions(word, max)
end