hunspell-ffi 0.1.2 → 0.1.3.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hunspell-ffi.rb CHANGED
@@ -7,23 +7,45 @@ class Hunspell
7
7
  attach_function :Hunspell_create, [:string, :string], :pointer
8
8
  attach_function :Hunspell_spell, [:pointer, :string], :bool
9
9
  attach_function :Hunspell_suggest, [:pointer, :pointer, :string], :int
10
+ attach_function :Hunspell_add, [:pointer, :string], :int
11
+ attach_function :Hunspell_add_with_affix, [:pointer, :string, :string], :int
12
+ attach_function :Hunspell_remove, [:pointer, :string], :int
10
13
  end
11
14
 
12
- # TODO RDoc
13
-
14
15
  def initialize(affpath, dicpath)
16
+ warn("Hunspell could not find aff-file #{affpath}") unless File.exist?(affpath)
17
+ warn("Hunspell could not find dic-file #{affpath}") unless File.exist?(dicpath)
15
18
  @handler = C.Hunspell_create(affpath, dicpath)
16
19
  end
17
20
 
21
+ # Returns true for a known word or false.
18
22
  def spell(word)
19
23
  C.Hunspell_spell(@handler, word)
20
24
  end
21
- alias_method :check, :spell
25
+ alias_method :check, :spell
22
26
 
27
+ # Returns an array with suggested words or returns and empty array.
23
28
  def suggest(word)
24
29
  ptr = FFI::MemoryPointer.new(:pointer, 1)
25
30
  len = Hunspell::C.Hunspell_suggest(@handler, ptr, word)
26
31
  str_ptr = ptr.read_pointer
27
32
  str_ptr.null? ? [] : str_ptr.get_array_of_string(0, len).compact
28
- end
33
+ end
34
+
35
+ # Add word to the run-time dictionary
36
+ def add(word)
37
+ C.Hunspell_add(@handler, word)
38
+ end
39
+
40
+ # Add word to the run-time dictionary with affix flags of
41
+ # the example (a dictionary word): Hunspell will recognize
42
+ # affixed forms of the new word, too.
43
+ def add_with_affix(word, example)
44
+ C.Hunspell_add_with_affix(@handler, word, example)
45
+ end
46
+
47
+ # Remove word from the run-time dictionary
48
+ def remove(word)
49
+ C.Hunspell_remove(@handler, word)
50
+ end
29
51
  end
@@ -14,4 +14,13 @@ class TestHunspell < Test::Unit::TestCase
14
14
  assert_equal ["Baumkuchen"], @dict.suggest("Baumgurken")
15
15
  assert_equal [], @dict.suggest("qwss43easd")
16
16
  end
17
+
18
+ def test_dict_modifications
19
+ assert @dict.spell("Neuer Kuchen") == false
20
+ @dict.add("Neuer Kuchen")
21
+ assert @dict.spell("Neuer Kuchen") == true
22
+ @dict.remove("Neuer Kuchen")
23
+ assert @dict.spell("Neuer Kuchen") == false
24
+ # TODO test add_with_affix
25
+ end
17
26
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hunspell-ffi
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ prerelease: true
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ - alpha
10
+ version: 0.1.3.alpha
10
11
  platform: ruby
11
12
  authors:
12
13
  - Andreas Haller