Class: Aspell

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

Constant Summary collapse

ULTRA =

CONSTANTS================================================================

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

.list_dictsObject

List all available dictionaries.

Parameters:

  • class

    object

Returns:

  • array of AspellDictInfo objects.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/raspell.c', line 270

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));
    }
    delete_aspell_dict_info_enumeration(dels);
    return result;
}

.new(*args) ⇒ Object

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")

Parameters:

  • language

    ISO639 language code plus optional ISO 3166 counry code as string (eg: “de” or “us_US”)

  • jargon

    a special jargon of the selected language

  • size

    the size of the dictionary to chose (if there are options)

  • encoding

    the encoding to use



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/raspell.c', line 188

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);
    }

    speller = to_aspell_speller(ret);

    //wrap pointer
    return Data_Wrap_Struct(klass, 0, aspell_free, speller);
}

.new1(options) ⇒ Object

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

a = Aspell.new(“jargon”=>“berlin”)

For a list of config options, see aspell manual.

Parameters:

  • options

    hash of options



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/raspell.c', line 238

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);
    }

    speller = to_aspell_speller(ret);

    //wrap pointer
    return Data_Wrap_Struct(klass, 0, aspell_free, speller);
}

Instance Method Details

#add_to_personal(word) ⇒ Object

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.

Parameters:

  • word

    the word to add.



383
384
385
386
387
388
# File 'ext/raspell.c', line 383

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;
}

#add_to_session(word) ⇒ Object

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.

Parameters:

  • word

    the word to add.



383
384
385
386
387
388
# File 'ext/raspell.c', line 383

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;
}

#check(word) ⇒ Object

Check a given word for correctness.

Parameters:

  • word

    the word to check

Returns:

  • true if the word is correct, otherwise false.



474
475
476
477
478
479
480
481
482
483
484
485
# File 'ext/raspell.c', line 474

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;
}

#clear_sessionObject

Remove all words inside session.



361
362
363
364
365
366
# File 'ext/raspell.c', line 361

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

#correct_file(filename) ⇒ Object

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!

Parameters:

  • filename

    the name of the file as String.



584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/raspell.c', line 584

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);
    } else {
        rb_raise(cAspellError, "No block given. How to correct?");
    }
    return self;

}

#correct_lines(ary) ⇒ Object

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

}

Parameters:

  • ary

    the array of strings to check.



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'ext/raspell.c', line 504

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);
            }
            //push the substituted line to result
            rb_ary_push(result, sline);
            c++;
        }
        //free checker
        delete_aspell_document_checker(checker);
    } else {
        rb_raise(cAspellError, "No block given. How to correct?");
    }
    return result;
}

#dump_configObject

Simply dump config. Not very useful at all.



456
457
458
459
460
461
462
463
464
465
466
467
# File 'ext/raspell.c', line 456

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;
}

#get_option(key) ⇒ Object

Retrieve the value of a specific option. The options are listed inside Aspell::

Parameters:

  • word

    the option as string.



407
408
409
410
411
412
413
414
415
# File 'ext/raspell.c', line 407

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));
    }
    return result;
}

#get_option_as_list(key) ⇒ Object

Retrieve the value of a specific option as list.

Parameters:

  • word

    the option as string.



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/raspell.c', line 421

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);
    }

    //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;
}

#list_misspelled(ary) ⇒ Object

Return a list of all misspelled words inside a given array of strings.

Parameters:

  • ary

    an array of strings to check for.

Returns:

  • array of strings: words that are misspelled.



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'ext/raspell.c', line 603

static VALUE aspell_list_misspelled(VALUE self, VALUE ary) {
    Check_Type(ary, T_ARRAY);
    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];
        Check_Type(vline, T_STRING);
        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);
        }
        c++;
    }
    //free checker
    delete_aspell_document_checker(checker);
    result = rb_funcall(result, rb_intern("keys"), 0);
    return result;
}

#personal_wordlistObject

Returns the personal wordlist as array of strings.

Returns:

  • array of strings



325
326
327
328
# File 'ext/raspell.c', line 325

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

#remove_option(option) ⇒ Object

Delete an option.

Parameters:

  • option

    optionstring to remove from the options.



305
306
307
308
309
# File 'ext/raspell.c', line 305

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;
}

#save_all_word_listsObject

Synchronize all wordlists with the current session.



351
352
353
354
355
356
# File 'ext/raspell.c', line 351

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;
}

#session_wordlistObject

Returns the session wordlist as array of strings.

Returns:

  • array of strings



334
335
336
337
# File 'ext/raspell.c', line 334

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

#set_option(option, value) ⇒ Object

See Also:



294
295
296
297
298
# File 'ext/raspell.c', line 294

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;
}

#store_replacement(badword, rightword) ⇒ Object

Remember a correction. This affects the suggestion of other words to fit this correction.

Parameters:

  • badword

    the bad spelled word as string.

  • badword

    the correction of the bad spelled word as string.



571
572
573
574
575
# File 'ext/raspell.c', line 571

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;
}

#suggest(word) ⇒ Object

Suggest words for the given misspelled word.

Parameters:

  • word

    the misspelled word.

Returns:

  • array of strings.



373
374
375
376
# File 'ext/raspell.c', line 373

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

#suggestion_mode=(value) ⇒ Object

To set the mode, words are suggested.

Parameters:



315
316
317
318
319
# File 'ext/raspell.c', line 315

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;
}