* update spellcheck unit (change to default failure behavior)

* update example

git-svn-id: trunk@12239 -
This commit is contained in:
Almindor 2008-11-27 09:53:13 +00:00
parent 52bb5ac229
commit 563d2b2ecb
2 changed files with 55 additions and 31 deletions

View File

@ -3,24 +3,32 @@ program Example;
{$mode objfpc}{$H+}
uses
sCheck;
SpellCheck;
var
i, j, n: Integer;
i, j: Integer;
s: TSuggestionArray; { in case the word is wrong, this array contains
a list of suggestions }
Speller: TWordSpeller;
begin
if Paramcount < 2 then // check if user has used valid input
Writeln('Usage: ', ParamStr(0), ' <lang> <word1> <word2> ...')
else for i := 2 to ParamCount do begin // go for each word specified
n := SpellCheck(ParamStr(i), ParamStr(1), s); // spellcheck each word
if n > 0 then begin // if n > 0 then the word is wrong and we need to write suggestions
Write(ParamStr(i), ' is wrong. Here are some suggestions: ');
for j := 0 to High(s) do
Write(s[j], ' '); // write out the suggestions
Writeln; // to keep format
end else
Writeln(ParamStr(i), ' is spelled correctly!');
else begin
Speller := TWordSpeller.Create;
Speller.Language := ParamStr(1);
for i := 2 to ParamCount do begin // go for each word specified
s := Speller.SpellCheck(ParamStr(i)); // spellcheck each word
if Length(s) > 0 then begin // we need to write suggestions
Write(ParamStr(i), ' is wrong. Here are some suggestions: ');
for j := 0 to High(s) do
Write(s[j], ' '); // write out the suggestions
Writeln; // to keep format
end else
Writeln(ParamStr(i), ' is spelled correctly!');
end;
Speller.Free;
end;
end.

View File

@ -23,8 +23,7 @@ type
TLineErrors = array of TWordError;
TLineErrorsArray = array of TLineErrors;
{ TSpeller }
{ Abstract ancestor, don't use directly }
{ TSpellCheck }
TSpeller = class // abstract class, basis for all checkers
protected
@ -44,13 +43,14 @@ type
property Encoding: string read FEncoding write SetEncoding;
property Language: string read FLanguage write SetLanguage;
end;
{ TWordSpeller }
{ Basic spelling class for spelling single words without context }
{ TWordSpeller }
TWordSpeller = class(TSpeller) // class for simple per-word checking
private
FSpeller: PAspellSpeller;
FLastError: string;
function DoCreateSpeller(Lang, Enc, aMode: pChar): PAspellSpeller;
protected
procedure CreateSpeller; override;
procedure FreeSpeller; override;
@ -59,9 +59,6 @@ type
end;
{ TDocumentSpeller }
{ This speller is used to spellcheck lines or even whole documents.
It is usefull when different mode (like "tex") is used so you can pass
everything to aspell and let it take care of the context }
TDocumentSpeller = class(TWordSpeller)
private
@ -136,29 +133,48 @@ end;
{ TWordSpeller }
procedure TWordSpeller.CreateSpeller;
function TWordSpeller.DoCreateSpeller(Lang, Enc, aMode: pChar): PAspellSpeller;
var
Config: Paspellconfig;
Error: Paspellcanhaveerror;
begin
Config := new_aspell_config();
Result := new_aspell_config();
if Length(FLanguage) > 0 then
aspell_config_replace(Config, 'lang', pChar(FLanguage));
aspell_config_replace(Result, 'lang', Lang);
if Length(FEncoding) > 0 then
aspell_config_replace(Config, 'encoding', pChar(FEncoding));
aspell_config_replace(Result, 'encoding', Enc);
if Length(FMode) > 0 then
aspell_config_replace(Config, 'mode', pChar(FMode));
aspell_config_replace(Result, 'mode', aMode);
Error := new_aspell_speller(Config);
Error := new_aspell_speller(Result);
delete_aspell_config(Config);
delete_aspell_config(Result);
if aspell_error_number(Error) <> 0 then begin
FLastError := aspell_error_message(Error);
delete_aspell_can_have_error(Error);
Result := nil;
end else
Result := to_aspell_speller(Error);
end;
procedure TWordSpeller.CreateSpeller;
begin
FLastError := '';
FreeSpeller;
if aspell_error_number(Error) <> 0 then
raise Exception.Create('Error on speller creation: ' + aspell_error_message(Error))
else
FSpeller := to_aspell_speller(Error);
FSpeller := DoCreateSpeller(pChar(FLanguage), pChar(FEncoding), pChar(FMode));
if not Assigned(FSpeller) then
FSpeller := DoCreateSpeller(nil, pChar(FEncoding), pChar(FMode));
if not Assigned(FSpeller) then
FSpeller := DoCreateSpeller(nil, pChar(FEncoding), nil);
if not Assigned(FSpeller) then
FSpeller := DoCreateSpeller(nil, nil, pChar(FMode));
if not Assigned(FSpeller) then
FSpeller := DoCreateSpeller(nil, nil, nil);
if not Assigned(FSpeller) then
raise Exception.Create('Error on speller creation: ' + FLastError);
end;
procedure TWordSpeller.FreeSpeller;