Class: Kotoshu::Languages::German::SpellChecker

Inherits:
Components::SpellChecker show all
Defined in:
lib/kotoshu/languages/de/language.rb

Overview

German spell checker with Hunspell integration.

Uses the Lookup algorithm with Hunspell-format dictionaries and handles German-specific features (umlauts, ß, compound words).

Constant Summary collapse

GERMAN_SUBSTITUTIONS =

German-specific character substitutions for suggestions

{
  # Umlauts
  'ä' => %w[a ae],
  'ö' => %w[o oe],
  'ü' => %w[u ue],
  'ß' => %w[ss sz],
  # Common German errors
  'a' => %w[ä],
  'o' => %w[ö],
  'u' => %w[ü],
  's' => %w[ß],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aff_path:, dic_path:, script: :latin, encoding: 'UTF-8') ⇒ SpellChecker

Returns a new instance of SpellChecker.



33
34
35
36
37
38
39
# File 'lib/kotoshu/languages/de/language.rb', line 33

def initialize(aff_path:, dic_path:, script: :latin, encoding: 'UTF-8')
  @aff_path = aff_path
  @dic_path = dic_path
  @script = script
  @encoding = encoding
  @lookuper = Readers::LookupBuilder.new(aff_path, dic_path, encoding: encoding, script: script).build
end

Instance Attribute Details

#aff_pathObject (readonly)

Returns the value of attribute aff_path.



17
18
19
# File 'lib/kotoshu/languages/de/language.rb', line 17

def aff_path
  @aff_path
end

#dic_pathObject (readonly)

Returns the value of attribute dic_path.



17
18
19
# File 'lib/kotoshu/languages/de/language.rb', line 17

def dic_path
  @dic_path
end

#scriptObject (readonly)

Returns the value of attribute script.



17
18
19
# File 'lib/kotoshu/languages/de/language.rb', line 17

def script
  @script
end

Instance Method Details

#check(word) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kotoshu/languages/de/language.rb', line 41

def check(word)
  return { found: false, stem: nil, flags: [] } if word.nil? || word.empty?

  # Try exact match first
  first_form = @lookuper.good_forms(word).first
  return { found: true, stem: first_form.stem || word, flags: first_form.flags&.to_a || [] } if first_form

  # Try lowercase version (German nouns are capitalized)
  unless word == word.downcase
    lowercase_form = @lookuper.good_forms(word.downcase).first
    if lowercase_form
      return { found: true, stem: lowercase_form.stem || word.downcase,
               flags: lowercase_form.flags&.to_a || [] }
    end
  end

  { found: false, stem: nil, flags: [] }
end

#correct?(word) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/kotoshu/languages/de/language.rb', line 69

def correct?(word)
  check(word)[:found]
end

#lookuperObject



73
74
75
# File 'lib/kotoshu/languages/de/language.rb', line 73

def lookuper
  @lookuper
end

#suggest(word, max_suggestions: 10) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/kotoshu/languages/de/language.rb', line 60

def suggest(word, max_suggestions: 10)
  return [] if word.nil? || word.empty?

  first_form = @lookuper.good_forms(word).first
  return [] if first_form

  generate_suggestions(word, max_suggestions).take(max_suggestions)
end