Class: Aspell

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

Constant Summary

ULTRA =
rb_str_new2("ultra")
FAST =
rb_str_new2("fast")
NORMAL =
rb_str_new2("normal")
BADSPELLERS =
rb_str_new2("bad-spellers")
DictionaryOptions =
rb_ary_new3(  11,
rb_str_new2("master"),
rb_str_new2("dict-dir"),
rb_str_new2("lang"),
rb_str_new2("size"),
rb_str_new2("jargon"),
rb_str_new2("word-list-path"),
rb_str_new2("module-search-order"),
rb_str_new2("personal"),
rb_str_new2("repl"),
rb_str_new2("extra-dicts"),
rb_str_new2("strip-accents"))
CheckerOptions =
rb_ary_new3(  11,
rb_str_new2("ignore"),
rb_str_new2("ignore-case"),
rb_str_new2("ignore-accents"),
rb_str_new2("ignore-repl"),
rb_str_new2("save-repl"),
rb_str_new2("sug-mode"),
rb_str_new2("module-search-order"),
rb_str_new2("personal"),
rb_str_new2("repl"),
rb_str_new2("extra-dicts"),
rb_str_new2("strip-accents"))
FilterOptions =
rb_ary_new3(  10,
rb_str_new2("filter"),
rb_str_new2("mode"),
rb_str_new2("encoding"),
rb_str_new2("add-email-quote"),
rb_str_new2("rem-email-quote"),
rb_str_new2("email-margin"),
rb_str_new2("sgml-check"),
rb_str_new2("sgml-extension"),
rb_str_new2("tex-command"),
rb_str_new2("tex-check-command"))
RunTogetherOptions =
rb_ary_new3( 3,
rb_str_new2("run-together"),
rb_str_new2("run-together-limit"),
rb_str_new2("run-together-min"))
MiscOptions =
rb_ary_new3( 8,
rb_str_new2("conf"),
rb_str_new2("conf-dir"),
rb_str_new2("data-dir"),
rb_str_new2("local-data-dir"),
rb_str_new2("home-dir"),
rb_str_new2("per-conf"),
rb_str_new2("prefix"),
rb_str_new2("set-prefix"))
UtilityOptions =
rb_ary_new3( 4,
rb_str_new2("backup"),
rb_str_new2("time"),
rb_str_new2("reverse"),
rb_str_new2("keymapping"))

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) list_dicts

*

List all available dictionaries.
@param class object
@return array of AspellDictInfo objects.


# File 'ext/raspell.c'

static VALUE aspell_s_list_dicts(VALUE klass) {
AspellConfig * config;
AspellDictInfoList * dlist;
AspellDictInfoEnumeration * dels;
const AspellDictInfo * entry;
VALUE result = rb_ary_new();

//get a list of dictionaries
config = new_aspell_config();
dlist = get_aspell_dict_info_list(config);
delete_aspell_config(config);

//iterate over list - fill ruby array
dels = aspell_dict_info_list_elements(dlist);
while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0) {
    rb_ary_push(result, Data_Wrap_Struct(cDictInfo, 0, 0, (AspellDictInfo *)entry));
}

+ (Object) new

*

Ctor for aspell objects:
Aspell.new(language, jargon, size, encoding)
Please note: All parameters are optional. If a parameter is omitted, a default value is assumed from
             the environment (eg lang from $LANG). To retain default values, you can use nil
             as value: to set only size: Aspell.new(nil, nil, "80")
@param language ISO639 language code plus optional ISO 3166 counry code as string (eg: "de" or "us_US")
@param jargon a special jargon of the selected language
@param size the size of the dictionary to chose (if there are options)
@param encoding the encoding to use
@exception Exception if the specified dictionary is not found.


# File 'ext/raspell.c'

static VALUE aspell_s_new(int argc, VALUE *argv, VALUE klass) {
VALUE vlang, vjargon, vsize, vencoding;
const char *tmp;
//aspell values
AspellCanHaveError * ret;
AspellSpeller * speller;
AspellConfig * config;

//create new config
config = new_aspell_config();

//extract values
rb_scan_args(argc, argv, "04", &vlang, &vjargon, &vsize, &vencoding);

//language:
if (RTEST(vlang)) set_option(config, "lang", StringValuePtr(vlang));
//jargon:
if (RTEST(vjargon)) set_option(config, "jargon", StringValuePtr(vjargon));
//size:
if (RTEST(vsize)) set_option(config, "size", StringValuePtr(vsize));
//encoding:
if (RTEST(vencoding)) set_option(config, "encoding", StringValuePtr(vencoding));

//create speller:
ret = new_aspell_speller(config);
delete_aspell_config(config);
if (aspell_error(ret) != 0) {
    tmp = strdup(aspell_error_message(ret));
    delete_aspell_can_have_error(ret);
    rb_raise(cAspellError, tmp);
}

+ (Object) new1

*

Ctor for aspell objects.
This is a custom constructor and takes a hash of config options: key, value pairs.
Common use:

a = Aspell.new({"lang"=>"de", "jargon"=>"berlin"})

For a list of config options, see aspell manual.
@param options hash of options


# File 'ext/raspell.c'

static VALUE aspell_s_new1(VALUE klass, VALUE options) {
//aspell values
AspellCanHaveError * ret;
AspellSpeller * speller;
AspellConfig * config;

//create new config
config = new_aspell_config();

//set options
set_options(config, options);

//create speller:
ret = new_aspell_speller(config);
delete_aspell_config(config);
if (aspell_error(ret) != 0) {
    const char *tmp = strdup(aspell_error_message(ret));
    delete_aspell_can_have_error(ret);
    rb_raise(cAspellError, tmp);
}

Instance Method Details

- (Object) add_to_personal

*

Add a given word to the list of known words inside my private dictionary.
You have to call aspell_save_all_wordlists to make sure the list gets persistent.
@param word the word to add.


# File 'ext/raspell.c'

static VALUE aspell_add_to_personal(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_add_to_personal(speller, StringValuePtr(word), -1);
    check_for_error(speller);
    return self;
}

- (Object) add_to_session

*

Add a given word to the list of known words inside my private dictionary.
You have to call aspell_save_all_wordlists to make sure the list gets persistent.
@param word the word to add.


# File 'ext/raspell.c'

static VALUE aspell_add_to_personal(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_add_to_personal(speller, StringValuePtr(word), -1);
    check_for_error(speller);
    return self;
}

- (Object) check

*

Check a given word for correctness.
@param word the word to check
@return true if the word is correct, otherwise false.


# File 'ext/raspell.c'

static VALUE aspell_check(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    VALUE result = Qfalse;
    int code = aspell_speller_check(speller, StringValuePtr(word), -1);
    if (code == 1)
        result = Qtrue;
    else if (code == 0)
        result = Qfalse;
    else
        rb_raise( cAspellError, aspell_speller_error_message(speller));
    return result;
}

- (Object) clear_session

*

Remove all words inside session.


# File 'ext/raspell.c'

static VALUE aspell_clear_session(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_clear_session(speller);
    check_for_error(speller);
    return self;
}

- (Object) correct_file

*

Simple utility function to correct a file.
The file gets read, content will be checked and write back.
Please note: This method will change the file! - no backup and of course: no warranty!
@param filename the name of the file as String.
@exception Exception due to lack of read/write permissions.


# File 'ext/raspell.c'

static VALUE aspell_correct_file(VALUE self, VALUE filename) {
if (rb_block_given_p()) {
    VALUE content = rb_funcall(rb_cFile, rb_intern("readlines"), 1, filename);
    VALUE newcontent = aspell_correct_lines(self, content);
    VALUE file = rb_file_open(StringValuePtr(filename), "w+");
    rb_funcall(file, rb_intern("write"), 1, newcontent);
    rb_funcall(file, rb_intern("close"), 0);
}

- (Object) correct_lines

*

This method will check an array of strings for misspelled words.
This method needs a block to work proper. Each misspelled word is yielded,
a correct word as result from the block is assumed.
Common use:

a = Aspell.new(...)
text = ...
a.correct_lines(text) { |badword|
   puts "Error: #{badword}\n"
   puts a.suggest(badword).join(" | ")
   gets #the input is returned as right word
}

@param ary the array of strings to check.
@result an array holding all lines with corrected words.


# File 'ext/raspell.c'

static VALUE aspell_correct_lines(VALUE self, VALUE ary) {
    VALUE result = ary;
    if (rb_block_given_p()) {
        //create checker
        AspellSpeller *speller = get_speller(self);
        AspellDocumentChecker * checker = get_checker(speller);
        AspellToken token;
        //some tmp values
        VALUE vline, sline;
        VALUE word, rword;
        char *line;
        int count=RARRAY_LEN(ary);
        int c=0;
        //create new result array
        result = rb_ary_new();
        //iterate over array
        while(c<count) {
int offset=0;
//fetch line
vline = RARRAY_PTR(ary)[c];
//save line
sline = rb_funcall(vline, rb_intern("dup"), 0);
//c representation
line = StringValuePtr(vline);
//process line
aspell_document_checker_process(checker, line, -1);
//iterate over all misspelled words
while (token = aspell_document_checker_next_misspelling(checker), token.len != 0) {
    //extract word by start/length qualifier
    word = rb_funcall(vline, rb_intern("[]"), 2, INT2FIX(token.offset), INT2FIX(token.len));
    //get the right word from the block
    rword = rb_yield(word);
    //nil -> do nothing
    if(rword == Qnil) continue;
    //check for string
    if (TYPE(rword) != T_STRING) rb_raise(cAspellError, "Need a String to substitute");
    //chomp the string
    rb_funcall(rword, rb_intern("chomp!"), 0);
    //empty string -> do nothing
    if(strlen(StringValuePtr(rword)) == 0) continue;
    //remember word for later suggestion
    aspell_speller_store_replacement(speller, StringValuePtr(word), -1, StringValuePtr(rword), -1);
    //substitute the word by replacement
    rb_funcall(sline, rb_intern("[]="), 3, INT2FIX(token.offset+offset), INT2FIX(token.len), rword);
    //adjust offset
    offset += strlen(StringValuePtr(rword))-strlen(StringValuePtr(word));
    //printf("replace >%s< with >%s< (offset now %d)\n", StringValuePtr(word), StringValuePtr(rword), offset);
}

- (Object) dump_config

*

Simply dump config.
Not very useful at all.


# File 'ext/raspell.c'

static VALUE aspell_dump_config(VALUE self) {
AspellSpeller *speller = get_speller(self);
AspellConfig *config = aspell_speller_config(speller);
AspellKeyInfoEnumeration * key_list = aspell_config_possible_elements( config, 0 );
const AspellKeyInfo * entry;

while ( (entry = aspell_key_info_enumeration_next(key_list) ) ) {
    printf("%20s:  %s\n", entry->name, aspell_config_retrieve(config, entry->name) );
}

- (Object) get_option

*

Retrieve the value of a specific option.
The options are listed inside
Aspell::[DictionaryOptions|CheckerOptions|FilterOptions|RunTogetherOptions|MiscOptions|UtilityOptions]
@param word the option as string.


# File 'ext/raspell.c'

static VALUE aspell_conf_retrieve(VALUE self, VALUE key) {
AspellSpeller *speller = get_speller(self);
AspellConfig *config = aspell_speller_config(speller);
VALUE result = rb_str_new2(aspell_config_retrieve(config, StringValuePtr(key)));
if (aspell_config_error(config) != 0) {
    rb_raise(cAspellError, aspell_config_error_message(config));
}

- (Object) get_option_as_list

*

Retrieve the value of a specific option as list.
@param word the option as string.


# File 'ext/raspell.c'

static VALUE aspell_conf_retrieve_list(VALUE self, VALUE key) {
AspellSpeller *speller = get_speller(self);
AspellConfig *config = aspell_speller_config(speller);
AspellStringList * list = new_aspell_string_list();
AspellMutableContainer * container  = aspell_string_list_to_mutable_container(list);
AspellStringEnumeration * els;
VALUE result = rb_ary_new();
const char *option_value;

//retrieve list
aspell_config_retrieve_list(config, StringValuePtr(key), container);
//check for error
if (aspell_config_error(config) != 0) {
    char *tmp = strdup(aspell_config_error_message(config));
    delete_aspell_string_list(list);
    rb_raise( cAspellError, tmp);
}

- (Object) list_misspelled

*

Return a list of all misspelled words inside a given array of strings.
@param ary an array of strings to check for.
@return array of strings: words that are misspelled.


# File 'ext/raspell.c'

static VALUE aspell_list_misspelled(VALUE self, VALUE ary) {
    VALUE result = rb_hash_new();
    //create checker
    AspellSpeller *speller = get_speller(self);
    AspellDocumentChecker * checker = get_checker(speller);
    AspellToken token;
    VALUE word, vline;
    int count=RARRAY_LEN(ary);
    int c=0;
    //iterate over array
    while(c<count) {
//process line
vline = RARRAY_PTR(ary)[c];
aspell_document_checker_process(checker, StringValuePtr(vline), -1);
//iterate over all misspelled words
while (token = aspell_document_checker_next_misspelling(checker), token.len != 0) {
    //extract word by start/length qualifier
    word = rb_funcall(vline, rb_intern("[]"), 2, INT2FIX(token.offset), INT2FIX(token.len));
    rb_hash_aset(result, word, Qnil);
    //yield block, if given
    if (rb_block_given_p())
        rb_yield(word);
}

- (Object) personal_wordlist

*

Returns the personal wordlist as array of strings.
@return array of strings


# File 'ext/raspell.c'

static VALUE aspell_personal_wordlist(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_personal_word_list(speller));
}

- (Object) remove_option

*

Delete an option.
@param option optionstring to remove from the options.


# File 'ext/raspell.c'

static VALUE aspell_remove_option(VALUE self, VALUE option) {
    AspellSpeller *speller = get_speller(self);
    aspell_config_remove(aspell_speller_config(speller), StringValuePtr(option));
    return self;
}

- (Object) save_all_word_lists

*

Synchronize all wordlists with the current session.


# File 'ext/raspell.c'

static VALUE aspell_save_all_wordlists(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_save_all_word_lists(speller);
    check_for_error(speller);
    return self;
}

- (Object) session_wordlist

*

Returns the session wordlist as array of strings.
@return array of strings


# File 'ext/raspell.c'

static VALUE aspell_session_wordlist(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_session_word_list(speller));
}

- (Object) set_option

*

@see set_option.


# File 'ext/raspell.c'

static VALUE aspell_set_option(VALUE self, VALUE option, VALUE value) {
    AspellSpeller *speller = get_speller(self);
    set_option(aspell_speller_config(speller), StringValuePtr(option), StringValuePtr(value));
    return self;
}

- (Object) store_replacement

*

Remember a correction.
This affects the suggestion of other words to fit this correction.
@param badword the bad spelled word as string.
@param badword the correction of the bad spelled word as string.
@result self


# File 'ext/raspell.c'

static VALUE aspell_store_replacement(VALUE self, VALUE badword, VALUE rightword) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_store_replacement(speller, StringValuePtr(badword), -1, StringValuePtr(rightword), -1);
    return self;
}

- (Object) suggest

*

Suggest words for the given misspelled word.
@param word the misspelled word.
@return array of strings.


# File 'ext/raspell.c'

static VALUE aspell_suggest(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_suggest(speller, StringValuePtr(word), -1));
}

- (Object) suggestion_mode=

*

To set the mode, words are suggested.
@param one of Aspell::[ULTRA|FAST|NORMAL|BADSPELLERS]


# File 'ext/raspell.c'

static VALUE aspell_set_suggestion_mode(VALUE self, VALUE value) {
    AspellSpeller *speller = get_speller(self);
    set_option(aspell_speller_config(speller), "sug-mode", StringValuePtr(value));
    return self;
}