Class: Hunspell

Inherits:
Object
  • Object
show all
Defined in:
ext/hunspell_wrap.c

Instance Method Summary collapse

Constructor Details

#new(affix_path, dic_path) ⇒ Object

Instantiate Hunspell with paths pointing to affix and dictionary files.

Example:

speller = Hunspell.new("/usr/share/myspell/en_US.aff", "/usr/share/myspell/en_US.dic")


26
27
28
29
30
31
32
33
34
35
# File 'ext/hunspell_wrap.c', line 26

static VALUE hunspell_initialize(VALUE self, VALUE affix_path, VALUE dic_path) {
    VALUE affpath = StringValue(affix_path), dpath = StringValue(dic_path);
    Hunhandle *handle = Hunspell_create(RSTRING_PTR(affpath), RSTRING_PTR(dpath));
    if (!handle) {
      rb_raise(rb_eRuntimeError, "Failed to initialize Hunspell handle.");
    }
    DATA_PTR(self) = handle;

    return self;
}

Instance Method Details

#encodingString

Returns the encoding of the dictionary.

Returns:

  • (String)


43
44
45
46
47
# File 'ext/hunspell_wrap.c', line 43

static VALUE wrap_encoding(VALUE self) {
    Hunhandle *handle = (Hunhandle *)DATA_PTR(self);
    char *enc = Hunspell_get_dic_encoding(handle);
    return rb_str_new2(enc);
}

#suggest(misspeledword) ⇒ Array

Returns a list of suggestions.

Returns:

  • (Array)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/hunspell_wrap.c', line 73

static VALUE wrap_suggest(VALUE self, VALUE word) {
    VALUE str = recode_if_needed(self, StringValue(word), 0);
    VALUE res;
    char** slst = NULL;
    int i, count = 0;
    Hunhandle *handle = (Hunhandle *)DATA_PTR(self);

    count = Hunspell_suggest(handle, &slst, RSTRING_PTR(str));

    res = rb_ary_new2(count);
    for (i=0; i<count; ++i) {
        rb_ary_push(res, recode_if_needed(self, rb_str_new2(slst[i]), 1));
        free(slst[i]);
    }

    if (slst) {
        free(slst);
    }

    return res;
}

#valid?(word) ⇒ Boolean

Checks if the word is in the dictionary.

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'ext/hunspell_wrap.c', line 101

static VALUE wrap_check(VALUE self, VALUE word) {
    VALUE str = recode_if_needed(self, StringValue(word), 0);
    Hunhandle *handle = (Hunhandle *)DATA_PTR(self);
    int rc = Hunspell_spell(handle, RSTRING_PTR(str));
    return rc == 0 ? Qfalse : Qtrue;
}