| 1 | ############################################################################# | ||
| 2 | ## Name: Lingua::MSWordSpell | ||
| 3 | ## Purpose: Spell-check using MS Word | ||
| 4 | ## Author: Simon Flack | ||
| 5 | ## Modified by: $Author: mattheww $ on $Date: 2006/05/18 10:54:19 $ | ||
| 6 | ## Created: 21/03/2003 | ||
| 7 | ## RCS-ID: $Id: MSWordSpell.pm,v 1.11 2006/05/18 10:54:19 mattheww Exp $ | ||
| 8 | ############################################################################# | ||
| 9 | package Lingua::MSWordSpell; | ||
| 10 | |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Win32::OLE qw(in); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use Carp; | ||
| 14 | use vars '$VERSION'; | ||
| 15 | |||
| 16 | $VERSION = sprintf"%d.%03d", q$Revision: 1.11 $ =~ /: (\d+)\.(\d+)/; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | my $word = Win32::OLE->GetActiveObject('Word.Application') | ||
| 21 | || new Win32::OLE('Word.Application', sub {$_[0]->Quit}); | ||
| 22 | $word->{Visible} = 0; | ||
| 23 | |||
| 24 | carp (q[Couldn't launch Microsoft Word]) unless $word; | ||
| 25 | carp (q[Microsoft Word 9 or higher is required]) | ||
| 26 | unless $word->{Version} && $word->{Version} >= 9; | ||
| 27 | bless { _msword => $word, _temp_doc => $word->WordBasic()->FileNew() }, | ||
| 28 | $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub spellcheck { | ||
| 33 | my $self = shift; | ||
| 34 | my $text = shift; | ||
| 35 | |||
| 36 | my $msword = $self -> {_msword}; | ||
| 37 | my @errors; | ||
| 38 | while ($text =~ m/(\S+)\b/g) { | ||
| 39 | my $offset = pos($text) - length($1) + 1; | ||
| 40 | my $term = { term => $1, offset => $offset }; | ||
| 41 | |||
| 42 | # If is correctly spelled proceed to the next word | ||
| 43 | next if $msword->CheckSpelling($term -> {term}); | ||
| 44 | |||
| 45 | # Otherwise look for spelling suggestions | ||
| 46 | my $suggestions = $msword->GetSpellingSuggestions($term -> {term}); | ||
| 47 | |||
| 48 | # No suggestions: | ||
| 49 | if (!$suggestions || !$suggestions -> {Count}) { | ||
| 50 | $term -> {type} = 'none'; | ||
| 51 | $term -> {guesses} = []; | ||
| 52 | push @errors, $term; | ||
| 53 | |||
| 54 | # Some suggestions: | ||
| 55 | } else { | ||
| 56 | my @suggest; | ||
| 57 | foreach (in $suggestions) { | ||
| 58 | push @suggest, $_->{Name}; | ||
| 59 | } | ||
| 60 | $term -> {type} = 'guess'; | ||
| 61 | $term -> {guesses} = \@suggest; | ||
| 62 | push @errors, $term; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return @errors; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | |||
| 72 | =pod | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | Lingua::MSWordSpell - Word spellchecker | ||
| 77 | |||
| 78 | =head1 SYNOPSIS | ||
| 79 | |||
| 80 | use Lingua::MSWordSpell; | ||
| 81 | my $spchecker = new Lingua::MSWordSpell; | ||
| 82 | |||
| 83 | my @errors = $spchecker->spellcheck($text); | ||
| 84 | |||
| 85 | =head1 DESCRIPTION | ||
| 86 | |||
| 87 | This is a rough and ready replacement for Lingua::Ispell that uses Microsoft | ||
| 88 | Word's Spellchecker over OLE automation. It requires Microsoft Word 9 or | ||
| 89 | higher. | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =item spellcheck($text) | ||
| 96 | |||
| 97 | This method uses the Microsoft Word spellchecker to check each word in the | ||
| 98 | supplied C<$text> string. It will return a hash reference for each misspelled | ||
| 99 | word: | ||
| 100 | |||
| 101 | term # The mis-spelled word | ||
| 102 | offset # The offset of the first character of 'term' from the start of | ||
| 103 | # the string, where the first char is at offset '1' | ||
| 104 | guesses # List of alternative spelling suggestions | ||
| 105 | |||
| 106 | If there are no misspelled words in the string, C<spellcheck()> will return an | ||
| 107 | empty list. | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =head1 AUTHOR | ||
| 112 | |||
| 113 | Simon Flack <cpan _at_ bbc _dot_ co _dot_ uk> | ||
| 114 | |||
| 115 | =head1 SEE ALSO | ||
| 116 | |||
| 117 | =head2 B<Interfaces to online spelling services (google):> | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item L<Net::Google::Spelling> | ||
| 122 | |||
| 123 | =item L<WebService::GoogleHack::Spelling> | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =head2 B<Interfaces to local spell checking binaries and libraries (aspell, pspell, ispell):> | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item L<Lingua::Ispell> | ||
| 132 | |||
| 133 | =item L<Text::Pspell> | ||
| 134 | |||
| 135 | =item L<Text::Aspell> | ||
| 136 | |||
| 137 | =item L<Text::SpellCheck> | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head1 COPYRIGHT | ||
| 142 | |||
| 143 | (c) BBC 2005. This program is free software; you can redistribute it and/or | ||
| 144 | modify it under the GNU GPL. | ||
| 145 | |||
| 146 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 147 | |||
| 148 | 'Microsoft Word' and 'Microsoft' are trademarks owned by Microsoft (L<http://microsoft.com/>) | ||
| 149 | |||
| 150 | =cut | ||