ffi-hunspell 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title 'FFI Hunspell Documentation' --protected --files ChangeLog.md,LICENSE.txt
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2010-10-05
2
+
3
+ * Initial release.
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+
2
+ The MIT License
3
+
4
+ Copyright (c) 2010 Hal Brodigan
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ 'Software'), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # ffi-hunspell
2
+
3
+ * [github.com/postmodern/ffi-hunspell](http://github.com/postmodern/ffi-hunspell)
4
+ * [github.com/postmodern/ffi-hunspell/issues](http://github.com/postmodern/ffi-hunspell/issues)
5
+ * Postmodern (postmodern.mod3 at gmail.com)
6
+
7
+ ## Description
8
+
9
+ Ruby FFI bindings for [Hunspell](http://hunspell.sourceforge.net/).
10
+
11
+ ## Examples
12
+
13
+ Open a dictionary:
14
+
15
+ require 'ffi/hunspell'
16
+
17
+ dict = FFI::Hunspell.dict('/usr/share/myspell/en_US')
18
+ # ...
19
+ dict.close
20
+
21
+ FFI::Hunspell.dict('/usr/share/myspell/en_US') do |dict|
22
+ # ...
23
+ end
24
+
25
+ Check if a word is valid:
26
+
27
+ dict.check?('dog')
28
+ # => true
29
+
30
+ dict.check?('d0g')
31
+ # => false
32
+
33
+ Find the stems of a word:
34
+
35
+ dict.stem('dogs')
36
+ # => ["dog"]
37
+
38
+ Suggest alternate spellings for a word:
39
+
40
+ dict.suggest('arbitrage')
41
+ # => ["arbitrage", "arbitrages", "arbitrager", "arbitraged", "arbitrate"]
42
+
43
+ ## Requirements
44
+
45
+ * [libhunspell](http://hunspell.sourceforge.net/) >= 1.2.0
46
+ * [ffi](http://github.com/ffi/ffi) ~> 0.6.0
47
+
48
+ ## Install
49
+
50
+ $ sudo gem install ffi-hunspell
51
+
52
+ ## License
53
+
54
+ See {file:LICENSE.txt} for license information.
55
+
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ gem 'ore-tasks', '~> 0.1.2'
6
+ require 'ore/tasks'
7
+
8
+ Ore::Tasks.new
9
+ rescue LoadError => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
12
+ end
13
+
14
+ begin
15
+ gem 'rspec', '~> 2.0.0'
16
+ require 'rspec/core/rake_task'
17
+
18
+ RSpec::Core::RakeTask.new
19
+ rescue LoadError => e
20
+ task :spec do
21
+ abort "Please run `gem install rspec` to install RSpec."
22
+ end
23
+ end
24
+ task :default => :spec
25
+
26
+ begin
27
+ gem 'yard', '~> 0.6.0'
28
+ require 'yard'
29
+
30
+ YARD::Rake::YardocTask.new
31
+ rescue LoadError => e
32
+ task :yard do
33
+ abort "Please run `gem install yard` to install YARD."
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ STDERR.puts "The 'ffi-hunspell.gemspec' file requires Ore."
9
+ STDERR.puts "Run `gem install ore` to install Ore."
10
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,19 @@
1
+ name: ffi-hunspell
2
+ version: 0.1.0
3
+ summary: FFI bindings for Hunspell
4
+ description: Ruby FFI bindings for Hunspell spell checker.
5
+ license: MIT
6
+ authors: Postmodern
7
+ email: postmodern.mod3@gmail.com
8
+ homepage: http://github.com/postmodern/ffi-hunspell
9
+ has_yard: true
10
+
11
+ requirements: libhunspell >= 1.2.0
12
+
13
+ dependencies:
14
+ ffi: ~> 0.6.0
15
+
16
+ development_dependencies:
17
+ ore: ~> 0.2.0
18
+ ore-tasks: ~> 0.1.2
19
+ yard: ~> 0.6.0
@@ -0,0 +1,2 @@
1
+ require 'ffi/hunspell/hunspell'
2
+ require 'ffi/hunspell/dictionary'
@@ -0,0 +1,186 @@
1
+ require 'ffi/hunspell/hunspell'
2
+
3
+ module FFI
4
+ module Hunspell
5
+ class Dictionary
6
+
7
+ #
8
+ # Creates a new dictionary.
9
+ #
10
+ # @param [String] affix_path
11
+ # The path to the `.aff` file.
12
+ #
13
+ # @param [String] dict_path
14
+ # The path to the `.dic` file.
15
+ #
16
+ # @param [String] key
17
+ # The optional key for encrypted dictionary files.
18
+ #
19
+ def initialize(affix_path,dic_path,key=nil)
20
+ @ptr = if key
21
+ Hunspell.Hunspell_create_key(affix_path,dic_path,key)
22
+ else
23
+ Hunspell.Hunspell_create(affix_path,dic_path)
24
+ end
25
+ end
26
+
27
+ #
28
+ # Opens a Hunspell dictionary.
29
+ #
30
+ # @param [String] path
31
+ # The path prefix shared by the `.aff` and `.dic` files.
32
+ #
33
+ # @yield [dict]
34
+ # The given block will be passed the Hunspell dictionary.
35
+ #
36
+ # @yieldparam [Dictionary] dict
37
+ # The opened dictionary.
38
+ #
39
+ # @return [Dictionary]
40
+ # If no block is given, the open dictionary will be returned.
41
+ #
42
+ def self.open(path)
43
+ dict = self.new("#{path}.aff","#{path}.dic")
44
+
45
+ if block_given?
46
+ yield dict
47
+
48
+ dict.close
49
+ return nil
50
+ else
51
+ return dict
52
+ end
53
+ end
54
+
55
+ #
56
+ # Determines if the dictionary is closed.
57
+ #
58
+ # @return [Boolean]
59
+ # Specifies whether the dictionary was closed.
60
+ #
61
+ def closed?
62
+ @ptr.nil?
63
+ end
64
+
65
+ #
66
+ # The encoding of the dictionary file.
67
+ #
68
+ # @return [String]
69
+ # The encoding of the dictionary file.
70
+ #
71
+ def encoding
72
+ Hunspell.Hunspell_get_dic_encoding(self)
73
+ end
74
+
75
+ #
76
+ # Adds a word to the dictionary.
77
+ #
78
+ # @param [String] word
79
+ # The word to add to the dictionary.
80
+ #
81
+ def add(word)
82
+ Hunspell.Hunspell_add(self,word.to_s)
83
+ end
84
+
85
+ def add_affix(word,example)
86
+ Hunspell.Hunspell_add_affix(self,word.to_s,example.to_s)
87
+ end
88
+
89
+ alias << add
90
+
91
+ #
92
+ # Removes a word from the dictionary.
93
+ #
94
+ # @param [String] word
95
+ # The word to remove.
96
+ #
97
+ def remove(word)
98
+ Hunspell.Hunspell_remove(self,word.to_s)
99
+ end
100
+
101
+ alias delete remove
102
+
103
+ #
104
+ # Checks if the word is validate.
105
+ #
106
+ # @param [String] word
107
+ # The word in question.
108
+ #
109
+ # @return [Boolean]
110
+ # Specifies whether the word is valid.
111
+ #
112
+ def check?(word)
113
+ Hunspell.Hunspell_spell(self,word.to_s) != 0
114
+ end
115
+
116
+ alias valid? check?
117
+
118
+ #
119
+ # Finds the stems of a word.
120
+ #
121
+ # @param [String] word
122
+ # The word in question.
123
+ #
124
+ # @return [Array<String>]
125
+ # The stems of the word.
126
+ #
127
+ def stem(word)
128
+ stems = []
129
+
130
+ FFI::MemoryPointer.new(:pointer) do |output|
131
+ count = Hunspell.Hunspell_stem(self,output,word.to_s)
132
+ ptr = output.get_pointer(0)
133
+
134
+ stems = ptr.get_array_of_string(0,count)
135
+ end
136
+
137
+ return stems
138
+ end
139
+
140
+ #
141
+ # Suggests alternate spellings of a word.
142
+ #
143
+ # @param [String] word
144
+ # The word in question.
145
+ #
146
+ # @return [Array<String>]
147
+ # The suggestions for the word.
148
+ #
149
+ def suggest(word)
150
+ suggestions = []
151
+
152
+ FFI::MemoryPointer.new(:pointer) do |output|
153
+ count = Hunspell.Hunspell_suggest(self,output,word.to_s)
154
+ ptr = output.get_pointer(0)
155
+
156
+ suggestions = ptr.get_array_of_string(0,count)
157
+ end
158
+
159
+ return suggestions
160
+ end
161
+
162
+ #
163
+ # Closes the dictionary.
164
+ #
165
+ # @return [nil]
166
+ #
167
+ def close
168
+ Hunspell.Hunspell_destroy(self)
169
+
170
+ @ptr = nil
171
+ return nil
172
+ end
173
+
174
+ #
175
+ # Converts the dictionary to a pointer.
176
+ #
177
+ # @return [FFI::Pointer]
178
+ # The pointer for the dictionary.
179
+ #
180
+ def to_ptr
181
+ @ptr
182
+ end
183
+
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,48 @@
1
+ require 'ffi'
2
+
3
+ module FFI
4
+ module Hunspell
5
+ extend FFI::Library
6
+
7
+ ffi_lib 'hunspell-1.2'
8
+
9
+ attach_function :Hunspell_create, [:string, :string], :pointer
10
+ attach_function :Hunspell_create_key, [:string, :string, :string], :pointer
11
+ attach_function :Hunspell_destroy, [:pointer], :void
12
+ attach_function :Hunspell_spell, [:pointer, :string], :int
13
+ attach_function :Hunspell_get_dic_encoding, [:pointer], :string
14
+ attach_function :Hunspell_suggest, [:pointer, :pointer, :string], :int
15
+ attach_function :Hunspell_analyze, [:pointer, :pointer, :string], :int
16
+ attach_function :Hunspell_stem, [:pointer, :pointer, :string], :int
17
+ attach_function :Hunspell_generate, [:pointer, :pointer, :string, :string], :int
18
+ attach_function :Hunspell_add, [:pointer, :string], :int
19
+ attach_function :Hunspell_add_with_affix, [:pointer, :string, :string], :int
20
+ attach_function :Hunspell_remove, [:pointer, :string], :int
21
+ attach_function :Hunspell_free_list, [:pointer, :pointer, :int], :void
22
+
23
+ #
24
+ # missing functions:
25
+ #
26
+ # attach_function :Hunspell_stem2, [:pointer, :pointer, :pointer, :int], :int
27
+ # attach_function :Hunspell_generate2, [:pointer, :pointer, :string, :pointer, :int], :int
28
+ #
29
+
30
+ #
31
+ # Opens a Hunspell dictionary.
32
+ #
33
+ # @param [String] path
34
+ # The path prefix shared by the `.aff` and `.dic` files.
35
+ #
36
+ # @yield [dict]
37
+ # The given block will be passed the Hunspell dictionary.
38
+ #
39
+ # @yieldparam [Dictionary] dict
40
+ # The opened dictionary.
41
+ #
42
+ # @return [nil]
43
+ #
44
+ def Hunspell.dict(path,&block)
45
+ Dictionary.open(path,&block)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'ffi/hunspell/dictionary'
3
+
4
+ describe Hunspell::Dictionary do
5
+ subject { Hunspell::Dictionary }
6
+
7
+ let(:root) { '/usr/share/myspell' || ENV['HUNSPELL_ROOT'] }
8
+ let(:path) { File.join(root,'en_US') }
9
+
10
+ let(:aff_path) { "#{path}.aff" }
11
+ let(:dic_path) { "#{path}.dic" }
12
+
13
+ it "should open a dictionary file from a path" do
14
+ subject.open(path) do |dict|
15
+ dict.should_not be_nil
16
+ end
17
+ end
18
+
19
+ it "should create a dictionary from '.aff' and '.dic' files" do
20
+ dict = subject.new(aff_path,dic_path)
21
+ dict.should_not be_nil
22
+
23
+ dict.close
24
+ end
25
+
26
+ it "should close the dictionary" do
27
+ dict = subject.open(path)
28
+ dict.close
29
+
30
+ dict.should be_closed
31
+ end
32
+
33
+ it "should provide the encoding of the dictionary files" do
34
+ subject.open(path) do |dict|
35
+ dict.encoding.should_not be_empty
36
+ end
37
+ end
38
+
39
+ it "should check if a word is valid" do
40
+ subject.open(path) do |dict|
41
+ dict.should be_valid('dog')
42
+ dict.should_not be_valid('dxg')
43
+ end
44
+ end
45
+
46
+ it "should find the stems of a word" do
47
+ subject.open(path) do |dict|
48
+ dict.stem('fishing').should == %w[fishing fish]
49
+ end
50
+ end
51
+
52
+ it "should suggest alternate spellings for words" do
53
+ subject.open(path) do |dict|
54
+ dict.suggest('arbitrage').should == %w[
55
+ arbitrage
56
+ arbitrages
57
+ arbitrager
58
+ arbitraged
59
+ arbitrate
60
+ ]
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'ffi'
3
+
4
+ include FFI
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-hunspell
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Postmodern
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-27 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ffi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 6
31
+ - 0
32
+ version: 0.6.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: ore
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 2
46
+ - 0
47
+ version: 0.2.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: ore-tasks
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ - 1
61
+ - 2
62
+ version: 0.1.2
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: yard
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ - 6
76
+ - 0
77
+ version: 0.6.0
78
+ type: :development
79
+ version_requirements: *id004
80
+ description: Ruby FFI bindings for Hunspell spell checker.
81
+ email: postmodern.mod3@gmail.com
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - README.md
88
+ files:
89
+ - .rspec
90
+ - .yardopts
91
+ - ChangeLog.md
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - ffi-hunspell.gemspec
96
+ - gemspec.yml
97
+ - lib/ffi/hunspell.rb
98
+ - lib/ffi/hunspell/dictionary.rb
99
+ - lib/ffi/hunspell/hunspell.rb
100
+ - spec/dictionary_spec.rb
101
+ - spec/spec_helper.rb
102
+ has_rdoc: yard
103
+ homepage: http://github.com/postmodern/ffi-hunspell
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements:
128
+ - libhunspell >= 1.2.0
129
+ rubyforge_project: ffi-hunspell
130
+ rubygems_version: 1.3.7
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: FFI bindings for Hunspell
134
+ test_files:
135
+ - spec/dictionary_spec.rb