Class: Kotoshu::Languages::Japanese::SpellChecker

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

Overview

Japanese spell checker using dictionary lookup.

Japanese uses morphological analysis rather than traditional Hunspell dictionaries. Spell checking is done through dictionary lookup of segmented words from the morphological analyzer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dic_path:, script: :cjk) ⇒ SpellChecker

Returns a new instance of SpellChecker.



20
21
22
23
24
25
26
# File 'lib/kotoshu/languages/ja/language.rb', line 20

def initialize(dic_path:, script: :cjk)
  @dic_path = dic_path
  @script = script
  # Japanese dictionaries are typically in custom formats
  # Load dictionary into memory for fast lookup
  @dictionary = load_dictionary(dic_path)
end

Instance Attribute Details

#dic_pathObject (readonly)

Returns the value of attribute dic_path.



18
19
20
# File 'lib/kotoshu/languages/ja/language.rb', line 18

def dic_path
  @dic_path
end

#scriptObject (readonly)

Returns the value of attribute script.



18
19
20
# File 'lib/kotoshu/languages/ja/language.rb', line 18

def script
  @script
end

Instance Method Details

#check(word) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kotoshu/languages/ja/language.rb', line 28

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

  # Check if word exists in dictionary
  found = @dictionary.include?(word)

  if found
    { found: true, stem: word, flags: [] }
  else
    # For CJK text, we might want to check if it contains valid characters
    # but not actual word validation
    { found: false, stem: nil, flags: [] }
  end
end

#correct?(word) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/kotoshu/languages/ja/language.rb', line 51

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

#suggest(word, max_suggestions: 10) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/kotoshu/languages/ja/language.rb', line 43

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

  # Generate suggestions based on common Japanese errors
  generate_suggestions(word, max_suggestions).take(max_suggestions)
end