Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

works

  • Loading branch information...
commit dee0dc24e6bbdbfd936068e511e04ee04757c371 0 parents
@ahx authored
29 README.rdoc
@@ -0,0 +1,29 @@
+= hunspell-ffi
+
+A Ruby FFI interface to the Hunspell spelling checker
+
+It should work wherever Ruby FFI works (tested on Ruby 1.9.2, 1.8.7, JRuby 1.5.1).
+
+== Installation
+ 1. Install hunspell (OSX: 'brew install hunspell' Debian: 'apt-get install hunspell')
+ 2. gem install hunspell-ffi
+
+== Usage
+ require 'hunspell-ffi'
+ dict = Hunspell.new("path/to/cakes.aff", "path/to/cakes.dic")
+ dict.spell("Baumkuchen") # => true same as #check
+ dict.spell("Bomcuken") # => false
+ dict.suggest("Baumgurken") # => ["Baumkuchen"]
+ dict.suggest("qwss43easd") # => []
+
+== Author
+Andreas Haller
+andreashaller@gmail.com
+
+== License
+Hereby placed under public domain, do what you want, just do not hold me accountable...
+
+== TODO
+Add other hunspell methods (add, remove, analyze, stem ...)
+
+Test on Windows
6 Rakefile
@@ -0,0 +1,6 @@
+require 'rake/testtask'
+task :default => [:test]
+Rake::TestTask.new(:test) do |t|
+ t.test_files = FileList['test/test_*.rb']
+ t.ruby_opts = ['-rubygems -I.'] if defined? Gem
+end
14 hunspell-ffi.gemspec
@@ -0,0 +1,14 @@
+# encoding: utf-8
+Gem::Specification.new do |s|
+ s.name = 'hunspell-ffi'
+ s.version = '0.1'
+ s.date = '2010-08-6'
+ s.authors = ["Andreas Haller"]
+ s.email = ["andreashaller@gmail.com"]
+ s.homepage = "http://github.com/ahaller/hunspell-ffi"
+ s.summary = "A Ruby FFI interface to the Hunspell spelling checker"
+ s.required_rubygems_version = ">= 1.3.6"
+ s.files = Dir['test/cakes*'] + Dir['test/*.rb'] + Dir['lib/**/*.rb'] + %w(Rakefile README.rdoc)
+ s.test_files = Dir['test/test_*.rb']
+ s.require_path = 'lib'
+end
29 lib/hunspell-ffi.rb
@@ -0,0 +1,29 @@
+# encoding: utf-8
+require 'ffi'
+class Hunspell
+ module C
+ extend FFI::Library
+ ffi_lib ['hunspell', 'libhunspell', 'hunspell-1.2', 'libhunspell-1.2']
+ attach_function :Hunspell_create, [:string, :string], :pointer
+ attach_function :Hunspell_spell, [:pointer, :string], :bool
+ attach_function :Hunspell_suggest, [:pointer, :pointer, :string], :int
+ end
+
+ # TODO RDoc
+
+ def initialize(affpath, dicpath)
+ @handler = C.Hunspell_create(affpath, dicpath)
+ end
+
+ def spell(word)
+ C.Hunspell_spell(@handler, word)
+ end
+ alias_method :check, :spell
+
+ def suggest(word)
+ ptr = FFI::MemoryPointer.new(:pointer, 1)
+ len = Hunspell::C.Hunspell_suggest(@handler, ptr, word)
+ str_ptr = ptr.read_pointer
+ str_ptr.null? ? [] : str_ptr.get_array_of_string(0, len).compact
+ end
+end
0  test/cakes.aff
No changes.
21 test/cakes.dic
@@ -0,0 +1,21 @@
+20
+Apfelkuchen
+Baumkuchen
+Bienenstich
+Butterkuchen
+Donauwelle
+Eierschecke
+Guglhupf
+Hefekuchen
+Käsekuchen
+Marmorkuchen
+Obstkuchen
+Panettone
+Pflaumenkuchen
+Russischer Zupfkuchen
+Rührkuchen
+Sandkuchen
+Schmandkuchen
+Stollen
+Streuselkuchen
+Zwiebelkuchen
17 test/test_hunspell.rb
@@ -0,0 +1,17 @@
+# encoding: utf-8
+require "test/unit"
+require File.expand_path(File.dirname(__FILE__)) + '/../lib/hunspell-ffi'
+class TestHunspell < Test::Unit::TestCase
+ def setup
+ @dict_dir = File.dirname(__FILE__)
+ @dict = Hunspell.new("#{@dict_dir}/cakes.aff", "#{@dict_dir}/cakes.dic")
+ end
+
+ def test_basic_spelling
+ assert @dict.spell("Baumkuchen") == true
+ assert @dict.check("Baumkuchen") == true # check alias
+ assert @dict.spell("Bomcuken") == false
+ assert_equal ["Baumkuchen"], @dict.suggest("Baumgurken")
+ assert_equal [], @dict.suggest("qwss43easd")
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.