Example #1
0
/**
 * Retrieve the value of a specific option as list.
 * @param word the option as string.
 */
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, STR2CSTR(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, "%s", tmp);
    }

    //iterate over list
    els = aspell_string_list_elements(list);
    while ( (option_value = aspell_string_enumeration_next(els)) != 0) {
        //push the option value to result
        rb_ary_push(result, rb_str_new2(option_value));
    }
    //free list
    delete_aspell_string_enumeration(els);
    delete_aspell_string_list(list);

    return result;
}
Example #2
0
/**
 * 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.
 */
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, STR2CSTR(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);
        }
        c++;
    }
    //free checker
    delete_aspell_document_checker(checker);
    result = rb_funcall(result, rb_intern("keys"), 0);
    return result;
}
Example #3
0
/**
 * 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.
 */
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 = STR2CSTR(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, "%s", "Need a String to substitute");
                //chomp the string
                rb_funcall(rword, rb_intern("chomp!"), 0);
                //empty string -> do nothing
                if(strlen(STR2CSTR(rword)) == 0) continue;
                //remember word for later suggestion
                aspell_speller_store_replacement(speller, STR2CSTR(word), -1, STR2CSTR(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(STR2CSTR(rword))-strlen(STR2CSTR(word));
                //printf("replace >%s< with >%s< (offset now %d)\n", STR2CSTR(word), STR2CSTR(rword), offset);
            }
            //push the substituted line to result
            rb_ary_push(result, sline);
            c++;
        }
        //free checker
        delete_aspell_document_checker(checker);
    } else {
        rb_raise(cAspellError, "%s", "No block given. How to correct?");
    }
    return result;
}
Example #4
0
/**
 * 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.
 */
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, STR2CSTR(key)));
    if (aspell_config_error(config) != 0) {
        rb_raise(cAspellError, "%s", aspell_config_error_message(config));
    }
    return result;
}
Example #5
0
/**
 * Check a given word for correctness.
 * @param word the word to check
 * @return true if the word is correct, otherwise false.
 */
static VALUE aspell_check(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    VALUE result = Qfalse;
    int code = aspell_speller_check(speller, STR2CSTR(word), -1);
    if (code == 1)
        result = Qtrue;
    else if (code == 0)
        result = Qfalse;
    else
        rb_raise( cAspellError, "%s", aspell_speller_error_message(speller));
    return result;
}
Example #6
0
/**
 * Simply dump config.
 * Not very useful at all.
 */
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) );
    }
    delete_aspell_key_info_enumeration(key_list);
    return self;
}
Example #7
0
/**
 * Add a given word to the list of known words just for the lifetime of this object.
 * @param word the word to add.
 */
static VALUE aspell_add_to_session(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_add_to_session(speller, STR2CSTR(word), -1);
    check_for_error(speller);
    return self;
}
Example #8
0
/**
 * 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.
 */
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;
}
Example #9
0
/**
 * Remove all words inside session.
 */
static VALUE aspell_clear_session(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_clear_session(speller);
    check_for_error(speller);
    return self;
}
Example #10
0
/**
 * Synchronize all wordlists with the current session.
 */
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;
}
Example #11
0
/**
 * Returns the main wordlist as array of strings.
 * @return array of strings
 */
static VALUE aspell_main_wordlist(VALUE self) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_main_word_list(speller));
}
Example #12
0
/**
 * To set the mode, words are suggested.
 * @param one of Aspell::[ULTRA|FAST|NORMAL|BADSPELLERS]
 */
static VALUE aspell_set_suggestion_mode(VALUE self, VALUE value) {
    AspellSpeller *speller = get_speller(self);
    set_option(aspell_speller_config(speller), "sug-mode", STR2CSTR(value));
    return self;
}
Example #13
0
/**
 * Delete an option.
 * @param option optionstring to remove from the options.
 */
static VALUE aspell_remove_option(VALUE self, VALUE option) {
    AspellSpeller *speller = get_speller(self);
    aspell_config_remove(aspell_speller_config(speller), STR2CSTR(option));
    return self;
}
Example #14
0
/**
 * @see set_option. 
 */
static VALUE aspell_set_option(VALUE self, VALUE option, VALUE value) {
    AspellSpeller *speller = get_speller(self);
    set_option(aspell_speller_config(speller), STR2CSTR(option), STR2CSTR(value));
    return self;
}
Example #15
0
/**
 * 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
 */
static VALUE aspell_store_replacement(VALUE self, VALUE badword, VALUE rightword) {
    AspellSpeller *speller = get_speller(self);
    aspell_speller_store_replacement(speller, STR2CSTR(badword), -1, STR2CSTR(rightword), -1);
    return self;
}
Example #16
0
/**
 * Suggest words for the given misspelled word.
 * @param word the misspelled word.
 * @return array of strings.
 */
static VALUE aspell_suggest(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_suggest(speller, STR2CSTR(word), -1));
}
Example #17
0
/**
 * Suggest words for the given misspelled word.
 * @param word the misspelled word.
 * @return array of strings.
 */
static VALUE aspell_suggest(VALUE self, VALUE word) {
    AspellSpeller *speller = get_speller(self);
    return get_list(aspell_speller_suggest(speller, StringValuePtr(word), -1));
}