ffi-hunspell 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/ChangeLog.md CHANGED
@@ -1,3 +1,11 @@
1
+ ### 0.2.0 / 2011-01-22
2
+
3
+ * Added {FFI::Hunspell.lang}.
4
+ * Added {FFI::Hunspell.lang=}.
5
+ * Added {FFI::Hunspell.directories}.
6
+ * Have {FFI::Hunspell::Dictionary.open} accept a language name, and search
7
+ {FFI::Hunspell.directories} for the dictionary files.
8
+
1
9
  ### 0.1.0 / 2010-10-05
2
10
 
3
11
  * Initial release.
data/LICENSE.txt CHANGED
@@ -1,6 +1,3 @@
1
-
2
- The MIT License
3
-
4
1
  Copyright (c) 2010 Hal Brodigan
5
2
 
6
3
  Permission is hereby granted, free of charge, to any person obtaining
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # ffi-hunspell
2
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)
3
+ * [Source](http://github.com/postmodern/ffi-hunspell)
4
+ * [Issues](http://github.com/postmodern/ffi-hunspell/issues)
5
+ * [Documentation](http://rubydoc.info/gems/ffi-hunspell)
5
6
  * Postmodern (postmodern.mod3 at gmail.com)
6
7
 
7
8
  ## Description
@@ -14,14 +15,18 @@ Open a dictionary:
14
15
 
15
16
  require 'ffi/hunspell'
16
17
 
17
- dict = FFI::Hunspell.dict('/usr/share/myspell/en_US')
18
- # ...
19
- dict.close
18
+ FFI::Hunspell.dict do |dict|
19
+ # ...
20
+ end
20
21
 
21
- FFI::Hunspell.dict('/usr/share/myspell/en_US') do |dict|
22
+ FFI::Hunspell.dict('en_GB') do |dict|
22
23
  # ...
23
24
  end
24
25
 
26
+ dict = FFI::Hunspell.dict('en_GB')
27
+ # ...
28
+ dict.close
29
+
25
30
  Check if a word is valid:
26
31
 
27
32
  dict.check?('dog')
@@ -49,6 +54,15 @@ Suggest alternate spellings for a word:
49
54
 
50
55
  $ sudo gem install ffi-hunspell
51
56
 
57
+ ## Known Issues
58
+
59
+ Some Linux distributions do not install the `libhunspell-1.2.so`
60
+ shared library file, but instead installs `libhunspell-1.2.so.0`.
61
+ Simply create a symbolic link to the hunspell shared library,
62
+ so that {FFI::Hunspell} can find the library:
63
+
64
+ # ln -s /usr/lib/libhunspell-1.2.so.0 /usr/lib/libhunspell-1.2.so
65
+
52
66
  ## License
53
67
 
54
68
  See {file:LICENSE.txt} for license information.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
 
4
4
  begin
5
- gem 'ore-tasks', '~> 0.1.2'
5
+ gem 'ore-tasks', '~> 0.2.0'
6
6
  require 'ore/tasks'
7
7
 
8
8
  Ore::Tasks.new
@@ -21,6 +21,7 @@ rescue LoadError => e
21
21
  abort "Please run `gem install rspec` to install RSpec."
22
22
  end
23
23
  end
24
+ task :test => :spec
24
25
  task :default => :spec
25
26
 
26
27
  begin
data/ffi-hunspell.gemspec CHANGED
@@ -6,5 +6,5 @@ begin
6
6
  end
7
7
  rescue NameError
8
8
  STDERR.puts "The 'ffi-hunspell.gemspec' file requires Ore."
9
- STDERR.puts "Run `gem install ore` to install Ore."
9
+ STDERR.puts "Run `gem install ore-core` to install Ore."
10
10
  end
data/gemspec.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  name: ffi-hunspell
2
- version: 0.1.0
2
+ version: 0.2.0
3
3
  summary: FFI bindings for Hunspell
4
4
  description: Ruby FFI bindings for Hunspell spell checker.
5
5
  license: MIT
@@ -14,6 +14,6 @@ dependencies:
14
14
  ffi: ~> 0.6.0
15
15
 
16
16
  development_dependencies:
17
- ore: ~> 0.2.0
18
- ore-tasks: ~> 0.1.2
17
+ ore-tasks: ~> 0.2.0
18
+ rspec: ~> 2.0.0
19
19
  yard: ~> 0.6.0
@@ -2,8 +2,17 @@ require 'ffi/hunspell/hunspell'
2
2
 
3
3
  module FFI
4
4
  module Hunspell
5
+ #
6
+ # Represents a dictionary for a specific language.
7
+ #
5
8
  class Dictionary
6
9
 
10
+ # The affix file extension
11
+ AFF_EXT = 'aff'
12
+
13
+ # The dictionary file extension
14
+ DIC_EXT = 'dic'
15
+
7
16
  #
8
17
  # Creates a new dictionary.
9
18
  #
@@ -16,7 +25,18 @@ module FFI
16
25
  # @param [String] key
17
26
  # The optional key for encrypted dictionary files.
18
27
  #
28
+ # @raise [RuntimeError]
29
+ # Either the affix or dic files did not exist.
30
+ #
19
31
  def initialize(affix_path,dic_path,key=nil)
32
+ unless File.file?(affix_path)
33
+ raise("invalid affix path #{affix_path.inspect}")
34
+ end
35
+
36
+ unless File.file?(dic_path)
37
+ raise("invalid dic path #{dic_path.inspect}")
38
+ end
39
+
20
40
  @ptr = if key
21
41
  Hunspell.Hunspell_create_key(affix_path,dic_path,key)
22
42
  else
@@ -27,8 +47,8 @@ module FFI
27
47
  #
28
48
  # Opens a Hunspell dictionary.
29
49
  #
30
- # @param [String] path
31
- # The path prefix shared by the `.aff` and `.dic` files.
50
+ # @param [Symbol, String] name
51
+ # The name of the dictionary to open.
32
52
  #
33
53
  # @yield [dict]
34
54
  # The given block will be passed the Hunspell dictionary.
@@ -39,17 +59,31 @@ module FFI
39
59
  # @return [Dictionary]
40
60
  # If no block is given, the open dictionary will be returned.
41
61
  #
42
- def self.open(path)
43
- dict = self.new("#{path}.aff","#{path}.dic")
62
+ # @raise [RuntimeError]
63
+ # The dictionary files could not be found in any of the directories.
64
+ #
65
+ def self.open(name)
66
+ name = name.to_s
67
+
68
+ Hunspell.directories.each do |dir|
69
+ affix_path = File.join(dir,"#{name}.#{AFF_EXT}")
70
+ dic_path = File.join(dir,"#{name}.#{DIC_EXT}")
44
71
 
45
- if block_given?
46
- yield dict
72
+ if (File.file?(affix_path) && File.file?(dic_path))
73
+ dict = self.new(affix_path,dic_path)
47
74
 
48
- dict.close
49
- return nil
50
- else
51
- return dict
75
+ if block_given?
76
+ yield dict
77
+
78
+ dict.close
79
+ return nil
80
+ else
81
+ return dict
82
+ end
83
+ end
52
84
  end
85
+
86
+ raise("unable to find the dictionary #{name.dump} in any of the directories")
53
87
  end
54
88
 
55
89
  #
@@ -27,11 +27,65 @@ module FFI
27
27
  # attach_function :Hunspell_generate2, [:pointer, :pointer, :string, :pointer, :int], :int
28
28
  #
29
29
 
30
+ # The language to default to, if no 'LANG' env variable was set.
31
+ DEFAULT_LANG = 'en_US'
32
+
33
+ #
34
+ # The default language.
35
+ #
36
+ # @return [String]
37
+ # The name of the default language.
38
+ #
39
+ # @since 0.2.0
40
+ #
41
+ def Hunspell.lang
42
+ @lang ||= if ENV['LANG']
43
+ ENV['LANG'].split('.',2).first
44
+ else
45
+ DEFAULT_LANG
46
+ end
47
+ end
48
+
49
+ #
50
+ # Sets the default language.
51
+ #
52
+ # @param [String] name
53
+ # The new language name.
54
+ #
55
+ # @return [String]
56
+ # The name of the new default language.
57
+ #
58
+ # @since 0.2.0
59
+ #
60
+ def Hunspell.lang=(new_lang)
61
+ @lang = new_lang.to_s
62
+ end
63
+
64
+ #
65
+ # The directories to search for dictionary files.
66
+ #
67
+ # @return [Array]
68
+ # The directory paths.
69
+ #
70
+ # @since 0.2.0
71
+ #
72
+ def Hunspell.directories
73
+ @directories ||= [
74
+ '/usr/local/share/myspell',
75
+ '/usr/share/myspell'
76
+ ]
77
+ end
78
+
79
+ # prepend the ~/.hunspell_default directory to DIRS
80
+ if (home = (ENV['HOME'] || ENV['HOMEPATH']))
81
+ directories.unshift(File.join(home,'.hunspell_default'))
82
+ end
83
+
30
84
  #
31
85
  # Opens a Hunspell dictionary.
32
86
  #
33
- # @param [String] path
34
- # The path prefix shared by the `.aff` and `.dic` files.
87
+ # @param [Symbol, String] name
88
+ # The name of the dictionary to open.
35
89
  #
36
90
  # @yield [dict]
37
91
  # The given block will be passed the Hunspell dictionary.
@@ -41,8 +95,8 @@ module FFI
41
95
  #
42
96
  # @return [nil]
43
97
  #
44
- def Hunspell.dict(path,&block)
45
- Dictionary.open(path,&block)
98
+ def Hunspell.dict(name=Hunspell.lang,&block)
99
+ Dictionary.open(name,&block)
46
100
  end
47
101
  end
48
102
  end
@@ -4,53 +4,52 @@ require 'ffi/hunspell/dictionary'
4
4
  describe Hunspell::Dictionary do
5
5
  subject { Hunspell::Dictionary }
6
6
 
7
- let(:root) { '/usr/share/myspell' || ENV['HUNSPELL_ROOT'] }
8
- let(:path) { File.join(root,'en_US') }
7
+ let(:lang) { 'en_US' }
9
8
 
10
- let(:aff_path) { "#{path}.aff" }
11
- let(:dic_path) { "#{path}.dic" }
9
+ let(:affix_path) { File.join(Hunspell.directories.last,"#{lang}.aff") }
10
+ let(:dic_path) { File.join(Hunspell.directories.last,"#{lang}.dic") }
12
11
 
13
- it "should open a dictionary file from a path" do
14
- subject.open(path) do |dict|
12
+ it "should find and open a dictionary file for a given language" do
13
+ subject.open(lang) do |dict|
15
14
  dict.should_not be_nil
16
15
  end
17
16
  end
18
17
 
19
18
  it "should create a dictionary from '.aff' and '.dic' files" do
20
- dict = subject.new(aff_path,dic_path)
19
+ dict = subject.new(affix_path,dic_path)
21
20
  dict.should_not be_nil
22
21
 
23
22
  dict.close
24
23
  end
25
24
 
26
25
  it "should close the dictionary" do
27
- dict = subject.open(path)
26
+ dict = subject.open(lang)
28
27
  dict.close
29
28
 
30
29
  dict.should be_closed
31
30
  end
32
31
 
33
32
  it "should provide the encoding of the dictionary files" do
34
- subject.open(path) do |dict|
33
+ subject.open(lang) do |dict|
35
34
  dict.encoding.should_not be_empty
36
35
  end
37
36
  end
38
37
 
39
38
  it "should check if a word is valid" do
40
- subject.open(path) do |dict|
39
+ subject.open(lang) do |dict|
41
40
  dict.should be_valid('dog')
42
41
  dict.should_not be_valid('dxg')
43
42
  end
44
43
  end
45
44
 
46
45
  it "should find the stems of a word" do
47
- subject.open(path) do |dict|
46
+ subject.open(lang) do |dict|
48
47
  dict.stem('fishing').should == %w[fishing fish]
49
48
  end
50
49
  end
51
50
 
52
51
  it "should suggest alternate spellings for words" do
53
- subject.open(path) do |dict|
52
+ subject.open(lang) do |dict|
54
53
  dict.suggest('arbitrage').should == %w[
55
54
  arbitrage
56
55
  arbitrages
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'ffi/hunspell/hunspell'
3
+
4
+ describe Hunspell do
5
+ it "should have a default language" do
6
+ subject.lang.should_not be_nil
7
+ subject.lang.should_not be_empty
8
+ end
9
+
10
+ it "should have directories to search within" do
11
+ subject.directories.should_not be_empty
12
+ end
13
+
14
+ it "should open a dictionary file" do
15
+ subject.dict('en_US') do |dict|
16
+ dict.should_not be_nil
17
+ end
18
+ end
19
+
20
+ it "should open the dictionary file for the default language" do
21
+ subject.dict do |dict|
22
+ dict.should_not be_nil
23
+ end
24
+ end
25
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,12 @@
1
1
  require 'rspec'
2
- require 'ffi'
2
+ require 'ffi/hunspell'
3
3
 
4
4
  include FFI
5
+
6
+ RSpec.configure do |specs|
7
+ specs.before(:suite) do
8
+ if ENV['HUNSPELL_ROOT']
9
+ Hunspell.directories << ENV['HUNSPELL_ROOT']
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Postmodern
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-27 00:00:00 -07:00
17
+ date: 2011-01-22 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: ore
36
+ name: ore-tasks
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
@@ -48,7 +48,7 @@ dependencies:
48
48
  type: :development
49
49
  version_requirements: *id002
50
50
  - !ruby/object:Gem::Dependency
51
- name: ore-tasks
51
+ name: rspec
52
52
  prerelease: false
53
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
54
  none: false
@@ -56,10 +56,10 @@ dependencies:
56
56
  - - ~>
57
57
  - !ruby/object:Gem::Version
58
58
  segments:
59
- - 0
60
- - 1
61
59
  - 2
62
- version: 0.1.2
60
+ - 0
61
+ - 0
62
+ version: 2.0.0
63
63
  type: :development
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
@@ -86,6 +86,7 @@ extensions: []
86
86
  extra_rdoc_files:
87
87
  - README.md
88
88
  files:
89
+ - .gemtest
89
90
  - .rspec
90
91
  - .yardopts
91
92
  - ChangeLog.md
@@ -98,6 +99,7 @@ files:
98
99
  - lib/ffi/hunspell/dictionary.rb
99
100
  - lib/ffi/hunspell/hunspell.rb
100
101
  - spec/dictionary_spec.rb
102
+ - spec/hunspell_spec.rb
101
103
  - spec/spec_helper.rb
102
104
  has_rdoc: yard
103
105
  homepage: http://github.com/postmodern/ffi-hunspell
@@ -133,3 +135,4 @@ specification_version: 3
133
135
  summary: FFI bindings for Hunspell
134
136
  test_files:
135
137
  - spec/dictionary_spec.rb
138
+ - spec/hunspell_spec.rb