Class: Uniword::Spellcheck::HunspellAdapter
- Inherits:
-
Object
- Object
- Uniword::Spellcheck::HunspellAdapter
- Defined in:
- lib/uniword/spellcheck/hunspell_adapter.rb
Overview
Thin wrapper around the hunspell command-line spell checker.
Responsibility: Interface with the hunspell binary for spell checking and suggestion retrieval.
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
Instance Method Summary collapse
-
#check(word) ⇒ Boolean
Check whether a word is spelled correctly.
-
#initialize(language: "en_US") ⇒ HunspellAdapter
constructor
Initialize the hunspell adapter.
-
#suggest(word) ⇒ Array<String>
Get spelling suggestions for a word.
Constructor Details
#initialize(language: "en_US") ⇒ HunspellAdapter
Initialize the hunspell adapter.
26 27 28 29 |
# File 'lib/uniword/spellcheck/hunspell_adapter.rb', line 26 def initialize(language: "en_US") @language = language verify_hunspell! end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
20 21 22 |
# File 'lib/uniword/spellcheck/hunspell_adapter.rb', line 20 def language @language end |
Instance Method Details
#check(word) ⇒ Boolean
Check whether a word is spelled correctly.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/uniword/spellcheck/hunspell_adapter.rb', line 35 def check(word) return true if word.strip.empty? return true unless word.match?(/\p{L}/) stdout, _stderr, _status = Open3.capture3( "hunspell", "-d", language, "-l", stdin_data: "#{word}\n" ) stdout.strip.empty? end |
#suggest(word) ⇒ Array<String>
Get spelling suggestions for a word.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/uniword/spellcheck/hunspell_adapter.rb', line 50 def suggest(word) return [] if word.strip.empty? return [] unless word.match?(/\p{L}/) stdout, _stderr, _status = Open3.capture3( "hunspell", "-d", language, "-a", stdin_data: "#{word}\n" ) parse_suggestions(stdout) end |