hunspell-i18n 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format progress
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Ruby bindings for Hunspell 1.2.x
2
+
3
+ This version expects to receive the word in UTF-8 encoding, and it will
4
+ then use iconv behind the scenes in order to convert to and from
5
+ the dictionary encoding.
6
+
7
+ ## Usage
8
+
9
+ prefix = '/usr/share/myspell/' # Fedora
10
+ speller = Hunspell.new(prefix + 'en_US.aff', prefix + 'en_US.dic')
11
+ speller.valid?('dog') # -> true
12
+ speller.valid?('dpg') # -> false
13
+ speller.suggest('dpg') # -> ["dog", "dg", "pg", "deg", "dig", ...]
14
+
15
+
16
+ Copyright 2011 Roman Shterenzon, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Default: create gemspec'
5
+ task :default => :gemspec
6
+
7
+ desc 'Generate documentation'
8
+ Rake::RDocTask.new(:rdoc) do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'Hunspell'
11
+ rdoc.options << '--line-numbers' << '--inline-source'
12
+ rdoc.rdoc_files.include('README')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ begin
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ gem.name = "hunspell-i18n"
20
+ gem.summary = %Q{Ruby bindings for libhunspell-1.2 with i18n support}
21
+ gem.description = gem.summary
22
+ gem.email = "romanbsd@yahoo.com"
23
+ gem.homepage = "http://github.com/romanbsd/hunspell"
24
+ gem.authors = ["Roman Shterenzon"]
25
+ gem.extensions = ['ext/extconf.rb']
26
+ end
27
+ Jeweler::GemcutterTasks.new
28
+ rescue LoadError
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/ext/extconf.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ unless have_header('hunspell/hunspell.h') and have_library('hunspell-1.2')
4
+ raise 'Hunspell not found.'
5
+ end
6
+
7
+ create_makefile('hunspell')
@@ -0,0 +1,129 @@
1
+ #include <ruby.h>
2
+ #include <hunspell/hunspell.h>
3
+
4
+ static VALUE rb_cHunspell;
5
+ static VALUE rb_cIconv;
6
+ static VALUE enc_utf8;
7
+ static VALUE enc_iso8859_1;
8
+
9
+ static void dict_destroy(Hunhandle *handle) {
10
+ Hunspell_destroy(handle);
11
+ }
12
+
13
+ /*
14
+ * call-seq:
15
+ * Hunspell.new(affix_path, dic_path)
16
+ *
17
+ * Instantiate Hunspell with paths pointing to affix and dictionary files.
18
+ *
19
+ * Example:
20
+ * speller = Hunspell.new("/usr/share/myspell/en_US.aff", "/usr/share/myspell/en_US.dic")
21
+ *
22
+ */
23
+ static VALUE dict_init(VALUE self, VALUE affix_path, VALUE dic_path) {
24
+ VALUE affpath = StringValue(affix_path), dpath = StringValue(dic_path);
25
+ Hunhandle *handle = Hunspell_create(RSTRING_PTR(affpath), RSTRING_PTR(dpath));
26
+ if (!handle) {
27
+ rb_raise(rb_eRuntimeError, "Failed to initialize Hunspell handle.");
28
+ }
29
+ VALUE dict = Data_Wrap_Struct(rb_cHunspell, NULL, dict_destroy, handle);
30
+ rb_iv_set(self, "dict", dict);
31
+ return self;
32
+ }
33
+
34
+ static inline Hunhandle* get_handle(VALUE self) {
35
+ Hunhandle *handle = NULL;
36
+ Data_Get_Struct(rb_iv_get(self, "dict"), Hunhandle, handle);
37
+ if (!handle) {
38
+ rb_raise(rb_eRuntimeError, "Hunspell handle not found.");
39
+ }
40
+ return handle;
41
+ }
42
+
43
+ /*
44
+ * call-seq:
45
+ * hunspell.encoding -> string
46
+ *
47
+ * Returns the encoding of the dictionary.
48
+ */
49
+ static VALUE wrap_encoding(VALUE self) {
50
+ Hunhandle *handle = get_handle(self);
51
+ char *enc = Hunspell_get_dic_encoding(handle);
52
+ return rb_str_new2(enc);
53
+ }
54
+
55
+ static VALUE recode_if_needed(VALUE self, VALUE str, int dir) {
56
+ VALUE enc = wrap_encoding(self);
57
+ VALUE from, to;
58
+
59
+ if (dir == 0) {
60
+ from = enc_utf8;
61
+ to = enc;
62
+ } else {
63
+ from = enc;
64
+ to = enc_utf8;
65
+ }
66
+ if ( rb_str_equal(enc, enc_iso8859_1) == Qfalse && rb_str_equal(enc, enc_utf8) == Qfalse) {
67
+ rb_funcall(str, rb_intern("force_encoding"), 1, enc_utf8);
68
+ return rb_funcall(rb_cIconv, rb_intern("conv"), 3, to, from, str);
69
+ } else {
70
+ return str;
71
+ }
72
+ }
73
+
74
+ /*
75
+ * call-seq:
76
+ * hunspell.suggest(misspeledword) -> ary
77
+ *
78
+ * Returns a list of suggestions.
79
+ */
80
+ static VALUE wrap_suggest(VALUE self, VALUE word) {
81
+ VALUE str = recode_if_needed(self, StringValue(word), 0);
82
+ VALUE res;
83
+ char** slst = NULL;
84
+ int i, count = 0;
85
+ Hunhandle *handle = get_handle(self);
86
+
87
+ count = Hunspell_suggest(handle, &slst, RSTRING_PTR(str));
88
+
89
+ res = rb_ary_new2(count);
90
+ for (i=0; i<count; ++i) {
91
+ rb_ary_push(res, recode_if_needed(self, rb_str_new2(slst[i]), 1));
92
+ free(slst[i]);
93
+ }
94
+
95
+ if (slst) {
96
+ free(slst);
97
+ }
98
+
99
+ return res;
100
+ }
101
+
102
+ /*
103
+ * call-seq:
104
+ * hunspell.valid?(word) -> bool
105
+ *
106
+ * Checks if the word is in the dictionary.
107
+ */
108
+ static VALUE wrap_check(VALUE self, VALUE word) {
109
+ VALUE str = recode_if_needed(self, StringValue(word), 0);
110
+ Hunhandle *handle = get_handle(self);
111
+ int rc = Hunspell_spell(handle, RSTRING_PTR(str));
112
+ return rc == 0 ? Qfalse : Qtrue;
113
+ }
114
+
115
+
116
+ void Init_hunspell(void) {
117
+ rb_cHunspell = rb_define_class("Hunspell", rb_cObject);
118
+ rb_define_method(rb_cHunspell, "initialize", dict_init, 2);
119
+ //rb_define_method(rb_cHunspell, "check?", wrap_check, 1);
120
+ rb_define_method(rb_cHunspell, "valid?", wrap_check, 1);
121
+ rb_define_method(rb_cHunspell, "suggest", wrap_suggest, 1);
122
+ rb_define_method(rb_cHunspell, "encoding", wrap_encoding, 0);
123
+ rb_require("iconv");
124
+ rb_cIconv = rb_const_get(rb_cObject, rb_intern("Iconv"));
125
+ enc_iso8859_1 = rb_str_new2("ISO8859-1");
126
+ enc_utf8 = rb_str_new2("UTF-8");
127
+ rb_global_variable(&enc_iso8859_1);
128
+ rb_global_variable(&enc_utf8);
129
+ }
@@ -0,0 +1,44 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "hunspell-i18n"
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Roman Shterenzon"]
12
+ s.date = "2012-01-21"
13
+ s.description = "Ruby bindings for libhunspell-1.2 with i18n support"
14
+ s.email = "romanbsd@yahoo.com"
15
+ s.extensions = ["ext/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".rspec",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "ext/extconf.rb",
25
+ "ext/hunspell_wrap.c",
26
+ "hunspell-i18n.gemspec",
27
+ "spec/hunspell_spec.rb",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.homepage = "http://github.com/romanbsd/hunspell"
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = "1.8.15"
33
+ s.summary = "Ruby bindings for libhunspell-1.2 with i18n support"
34
+
35
+ if s.respond_to? :specification_version then
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
+ else
40
+ end
41
+ else
42
+ end
43
+ end
44
+
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hunspell do
4
+
5
+ before :all do
6
+ @speller = Hunspell.new('en_US.aff', 'en_US.dic')
7
+ end
8
+
9
+ it "should check if a word is valid" do
10
+ @speller.should be_valid('dog')
11
+ @speller.should_not be_valid('dxg')
12
+ end
13
+
14
+ it "survives GC" do
15
+ 10.times do
16
+ @speller.suggest('dxg')
17
+ end
18
+ GC.start
19
+ 10.times do
20
+ @speller.suggest('dxg')
21
+ end
22
+ end
23
+
24
+ it "should suggest alternate spellings for words" do
25
+ @speller.suggest('arbitrage').should == %w[
26
+ arbitrage
27
+ arbitrages
28
+ arbitrager
29
+ arbitraged
30
+ arbitrate
31
+ ]
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require 'hunspell'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hunspell-i18n
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Roman Shterenzon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-01-21 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Ruby bindings for libhunspell-1.2 with i18n support
17
+ email: romanbsd@yahoo.com
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - .rspec
26
+ - README.md
27
+ - Rakefile
28
+ - VERSION
29
+ - ext/extconf.rb
30
+ - ext/hunspell_wrap.c
31
+ - hunspell-i18n.gemspec
32
+ - spec/hunspell_spec.rb
33
+ - spec/spec_helper.rb
34
+ homepage: http://github.com/romanbsd/hunspell
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.15
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Ruby bindings for libhunspell-1.2 with i18n support
61
+ test_files: []
62
+