Skip to content
This repository has been archived by the owner on Jan 23, 2020. It is now read-only.

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Haller committed Aug 6, 2010
0 parents commit dee0dc2
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 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 changes: 6 additions & 0 deletions 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 changes: 14 additions & 0 deletions 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 changes: 29 additions & 0 deletions 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
Empty file added test/cakes.aff
Empty file.
21 changes: 21 additions & 0 deletions 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 changes: 17 additions & 0 deletions 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

0 comments on commit dee0dc2

Please sign in to comment.