Search::Tools::SpellCheck - offer spelling suggestions


Search-Tools documentation Contained in the Search-Tools distribution.

Index


Code Index:

NAME

Top

Search::Tools::SpellCheck - offer spelling suggestions

SYNOPSIS

Top

 use Search::Tools::SpellCheck;

 my $query = 'the quick fox color:brown and "lazy dog" not jumped';

 my $spellcheck = 
    Search::Tools::SpellCheck->new(
                        dict        => 'path/to/my/dictionary',
                        max_suggest => 4,
                        );

 my $suggestions = $spellcheck->suggest($query);

 


DESCRIPTION

Top

This module offers suggestions for alternate spellings using Text::Aspell.

METHODS

Top

new( %opts )

Create a new SpellCheck object. %opts should include:

dict

Path(s) to your dictionary.

lang

Language to use. Default is en_US.

max_suggest

Maximum number of suggested spellings to return. Default is 4.

query_parser

A Search::Tools::QueryParser object.

init

Called internally by new().

suggest( @terms )

Returns an arrayref of hashrefs. Each hashref is composed of the following key/value pairs:

word

The keyword used.

suggestions

If value is 0 (zero) then the word was found in the dictionary and is spelled correctly.

If value is an arrayref, the array contains a list of suggested spellings.

aspell

If you need access to the Text::Aspell object used internally, this accessor will get/set it.

__END__

AUTHOR

Top

Peter Karman <karman@cpan.org>

ACKNOWLEDGEMENTS

Top

Thanks to Atomic Learning www.atomiclearning.com for sponsoring the development of this module.

Thanks to Bill Moseley, Text::Aspell maintainer, for the API suggestions for this module.

BUGS

Top

Please report any bugs or feature requests to bug-search-tools at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Search-Tools. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Search::Tools




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Search-Tools

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Search-Tools

* CPAN Ratings

http://cpanratings.perl.org/d/Search-Tools

* Search CPAN

http://search.cpan.org/dist/Search-Tools/

COPYRIGHT

Top

SEE ALSO

Top

Search::Tools::QueryParser, Text::Aspell


Search-Tools documentation Contained in the Search-Tools distribution.

package Search::Tools::SpellCheck;
use strict;
use warnings;
use Carp;
use base qw( Search::Tools::Object );
use Text::Aspell;
use Search::Tools::QueryParser;

our $VERSION = '0.59';

__PACKAGE__->mk_accessors(
    qw(
        max_suggest
        dict
        aspell
        query_parser
        )
);

sub init {
    my $self = shift;
    $self->SUPER::init(@_);
    $self->{query_parser} ||= Search::Tools::QueryParser->new();
    $self->{max_suggest}  ||= 4;
    $self->aspell(
               Text::Aspell->new
            or croak "can't get new() Text::Aspell"
    );

    $self->aspell->set_option( 'lang', $self->{query_parser}->lang );
    $self->_check_err;
    $self->aspell->set_option( 'sug-mode', 'fast' );
    $self->_check_err;
    $self->aspell->set_option( 'master', $self->dict ) if $self->dict;
    $self->_check_err;

}

sub _check_err {
    my $self = shift;
    carp $self->aspell->errstr if $self->aspell->errstr;
}

sub suggest {
    my $self        = shift;
    my $query_str   = shift or croak "query required";
    my $suggest     = [];
    my $phr_del     = $self->query_parser->phrase_delim;
    my $ignore_case = $self->query_parser->ignore_case;
    my $query       = $self->query_parser->parse($query_str);

    for my $term ( @{ $query->terms } ) {

        $term =~ s/$phr_del//g;
        my @w = split( m/\ +/, $term );

    WORD: for my $word (@w) {

            my $s = { word => $word };
            if ( $self->aspell->check($word) ) {
                $self->_check_err;
                $s->{suggestions} = 0;
            }
            else {
                my @sg = $self->aspell->suggest($word);
                $self->_check_err;
                if ( !@sg or !defined $sg[0] ) {
                    $s->{suggestions} = [];
                }
                else {

                    if ($ignore_case) {

                        # make them unique but preserve order
                        my $c = 0;
                        my %u = map { lc($_) => $c++ } @sg;
                        @sg = sort { $u{$a} <=> $u{$b} } keys %u;
                    }

                    $s->{suggestions}
                        = [ splice( @sg, 0, $self->max_suggest ) ];
                }
            }
            push( @$suggest, $s );

        }
    }

    return $suggest;
}

1;

__END__