Class: Hunspell

Inherits:
Object
  • Object
show all
Defined in:
lib/hunspell-ffi.rb

Defined Under Namespace

Modules: C

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, language = nil) ⇒ Hunspell

Creates a spell-checking instance. If only path is given, Hunspell will look for a dictionary using the language of your current locale, checking LC_ALL, LC_MESSAGES and LANG. If you would like to spell check words of a specific language provide it as the second parameter, language.

You may also directly provide the affix file as the path argument and the dictionary file as the language argument, provided they both exist. This is for legacy use of Hunspell.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hunspell-ffi.rb', line 45

def initialize(path, language = nil)
  if File.exist?(path) and language and File.exist?(language) then
    @affix      = path
    @dictionary = language
  else
    language ||= find_language

    @affix      = File.join path, "#{language}.aff"
    @dictionary = File.join path, "#{language}.dic"
  end

  raise ArgumentError,
        "Hunspell could not find affix file #{@affix}" unless
    File.exist?(@affix)
  raise ArgumentError,
        "Hunspell could not find dictionary file #{@dictionary}" unless
    File.exist?(@dictionary)

  @handler = C.Hunspell_create @affix, @dictionary
  @dic_encoding = nil

  if Object.const_defined? :Encoding then
    begin
      encoding_name = C.Hunspell_get_dic_encoding @handler
      @dic_encoding = Encoding.find encoding_name
    rescue ArgumentError
      # unknown encoding name, results will be ASCII-8BIT
    end
  end
end

Instance Attribute Details

#affixObject (readonly)

The affix file used to check words



28
29
30
# File 'lib/hunspell-ffi.rb', line 28

def affix
  @affix
end

#dictionaryObject (readonly)

The dictionary file used to check words



33
34
35
# File 'lib/hunspell-ffi.rb', line 33

def dictionary
  @dictionary
end

Instance Method Details

#add(word) ⇒ Object

Add word to the run-time dictionary



105
106
107
# File 'lib/hunspell-ffi.rb', line 105

def add(word)
  C.Hunspell_add(@handler, word)
end

#add_with_affix(word, example) ⇒ Object

Add word to the run-time dictionary with affix flags of the example (a dictionary word): Hunspell will recognize affixed forms of the new word, too.



112
113
114
# File 'lib/hunspell-ffi.rb', line 112

def add_with_affix(word, example)
  C.Hunspell_add_with_affix(@handler, word, example)
end

#analyze(word) ⇒ Object

Performs morphological analysis of word. See hunspell(4) for details on the output format.



118
119
120
121
122
123
124
# File 'lib/hunspell-ffi.rb', line 118

def analyze(word)
  list_pointer = FFI::MemoryPointer.new(:pointer, 1)

  len = C.Hunspell_analyze(@handler, list_pointer, word)

  read_list(list_pointer, len)
end

#find_languageObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hunspell-ffi.rb', line 76

def find_language
  %w[LC_ALL LC_MESSAGES LANG].each do |var|
    next unless value = ENV[var]

    lang, charset = value.split('.', 2)

    return lang if charset
  end

  nil
end

#read_list(list_pointer, len) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hunspell-ffi.rb', line 126

def read_list(list_pointer, len)
  return [] if len.zero?

  list = list_pointer.read_pointer

  strings = list.get_array_of_string(0, len)

  C.Hunspell_free_list(@handler, list_pointer, len)

  if @dic_encoding then
    strings.map do |string|
      string.force_encoding @dic_encoding
    end
  end

  strings
end

#remove(word) ⇒ Object

Remove word from the run-time dictionary



145
146
147
# File 'lib/hunspell-ffi.rb', line 145

def remove(word)
  C.Hunspell_remove(@handler, word)
end

#spell(word) ⇒ Object Also known as: check

Returns true for a known word or false.



89
90
91
# File 'lib/hunspell-ffi.rb', line 89

def spell(word)
  C.Hunspell_spell(@handler, word)
end

#stem(word) ⇒ Object

Returns the stems of word



150
151
152
153
154
155
156
# File 'lib/hunspell-ffi.rb', line 150

def stem(word)
  list_pointer = FFI::MemoryPointer.new(:pointer, 1)

  len = C.Hunspell_stem(@handler, list_pointer, word)

  read_list(list_pointer, len)
end

#suggest(word) ⇒ Object

Returns an array with suggested words or returns and empty array.



96
97
98
99
100
101
102
# File 'lib/hunspell-ffi.rb', line 96

def suggest(word)
  list_pointer = FFI::MemoryPointer.new(:pointer, 1)

  len = C.Hunspell_suggest(@handler, list_pointer, word)

  read_list(list_pointer, len)
end