diff options
| author | Almindor <Almindor@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2008-11-27 09:53:13 +0000 |
|---|---|---|
| committer | Almindor <Almindor@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2008-11-27 09:53:13 +0000 |
| commit | eb8c8f00ede3811afb685ba4902f9f83df0acb03 (patch) | |
| tree | 1b528d9725ec9822d0f932610a70c78d991d4eb3 /packages | |
| parent | 217faa099957d64c591b4028755ff0f2fca1a42a (diff) | |
| download | fpc-eb8c8f00ede3811afb685ba4902f9f83df0acb03.tar.gz | |
* update spellcheck unit (change to default failure behavior)
* update example
git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@12239 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/aspell/examples/example.pas | 30 | ||||
| -rw-r--r-- | packages/aspell/src/spellcheck.pp | 56 |
2 files changed, 55 insertions, 31 deletions
diff --git a/packages/aspell/examples/example.pas b/packages/aspell/examples/example.pas index 62359451f8..8f5221bda2 100644 --- a/packages/aspell/examples/example.pas +++ b/packages/aspell/examples/example.pas @@ -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. diff --git a/packages/aspell/src/spellcheck.pp b/packages/aspell/src/spellcheck.pp index 33c1f07178..38be5a8c77 100644 --- a/packages/aspell/src/spellcheck.pp +++ b/packages/aspell/src/spellcheck.pp @@ -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(Result); - Error := new_aspell_speller(Config); + delete_aspell_config(Result); - delete_aspell_config(Config); + 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; |
