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



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

alias_method :_rust_correct, :correct

#_rust_correct?Object



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

alias_method :_rust_correct?, :correct?

#_rust_correct_tokensObject



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

alias_method :_rust_correct_tokens, :correct_tokens

#_rust_healthcheckObject



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

alias_method :_rust_healthcheck, :healthcheck

#_rust_load!Object

Save original Rust methods



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

alias_method :_rust_load!, :load!

#_rust_statsObject



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

alias_method :_rust_stats, :stats

#_rust_suggestionsObject



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

alias_method :_rust_suggestions, :suggestions

#correct(word) ⇒ Object



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?(word) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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

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



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

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

  _rust_correct_tokens(tokens)
end

#healthcheckObject



231
232
233
# File 'lib/spellkit.rb', line 231

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



108
109
110
111
112
113
114
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
# File 'lib/spellkit.rb', line 108

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



227
228
229
# File 'lib/spellkit.rb', line 227

def stats
  _rust_stats
end

#suggestions(word, max = 5) ⇒ Object



200
201
202
203
204
205
# File 'lib/spellkit.rb', line 200

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