* new aspell (proper ctypes, all functions/types etc.)

* new scheck (OOP, will be extended with new functionality gradually)

git-svn-id: trunk@10147 -
This commit is contained in:
Almindor 2008-02-02 13:51:41 +00:00
parent 0109c29e3f
commit 3f2c2ed8be
2 changed files with 1599 additions and 212 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,76 +1,151 @@
unit sCheck;
{ Simple unit to simplify/OOP-ize pascal-style the aspell interface. Currently
very limited, will be expanded eventually. Use like you wish. }
{$mode objfpc}{$H+}
interface
uses
Aspell;
SysUtils, Aspell;
type
TSuggestionArray = array of string;
function SpellCheck(const Word, Lang: string; out Suggestions: TSuggestionArray): Integer;
{ TSpellCheck }
var
Encoding: string = 'utf-8';
TSpellCheck = class
protected
FSpeller: PAspellSpeller;
FMode: string;
FEncoding: string;
FLanguage: string;
procedure SetEncoding(const AValue: string);
procedure SetLanguage(const AValue: string);
procedure SetMode(const AValue: string);
procedure CreateSpeller;
procedure FreeSpeller;
public
constructor Create;
destructor Destroy; override;
function SpellCheck(const Word: string): TSuggestionArray;
public
property Mode: string read FMode write SetMode;
property Encoding: string read FEncoding write SetEncoding;
property Language: string read FLanguage write SetLanguage;
end;
implementation
function SpellCheck(const Word, Lang: string; out Suggestions: TSuggestionArray): Integer;
const
DEFAULT_ENCODING = 'utf-8';
DEFAULT_LANGUAGE = 'en';
DEFAULT_MODE = '';
function GetDefaultLanguage: string;
begin
Result := GetEnvironmentVariable('LANG');
if Length(Result) = 0 then
Result := DEFAULT_LANGUAGE;
end;
{ TSpellCheck }
procedure TSpellCheck.SetEncoding(const AValue: string);
begin
FEncoding := aValue;
CreateSpeller;
end;
procedure TSpellCheck.SetLanguage(const AValue: string);
begin
FLanguage := aValue;
CreateSpeller;
end;
procedure TSpellCheck.SetMode(const AValue: string);
begin
FMode := aValue;
CreateSpeller;
end;
procedure TSpellCheck.CreateSpeller;
var
cnf: aspellconfig;
ape: aspellcanhaveerror;
spl: aspellspeller;
sgs: aspellwordlist;
elm: aspellstringenumeration;
Config: Paspellconfig;
Error: Paspellcanhaveerror;
begin
Config := new_aspell_config();
if Length(FLanguage) > 0 then
aspell_config_replace(Config, 'lang', pChar(FLanguage));
if Length(FEncoding) > 0 then
aspell_config_replace(Config, 'encoding', pChar(FEncoding));
if Length(FMode) > 0 then
aspell_config_replace(Config, 'mode', pChar(FMode));
Error := new_aspell_speller(Config);
delete_aspell_config(Config);
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);
end;
procedure TSpellCheck.FreeSpeller;
begin
if Assigned(FSpeller) then begin
delete_aspell_speller(FSpeller);
FSpeller := nil;
end;
end;
constructor TSpellCheck.Create;
begin
FEncoding := DEFAULT_ENCODING;
FLanguage := GetDefaultLanguage;
FMode := DEFAULT_MODE;
CreateSpeller;
end;
destructor TSpellCheck.Destroy;
begin
FreeSpeller;
end;
function TSpellCheck.SpellCheck(const Word: string): TSuggestionArray;
var
sgs: Paspellwordlist;
elm: Paspellstringenumeration;
tmp: pChar;
i: Integer = 0;
begin
SetLength(Suggestions, 10);
Result := -1;
SetLength(Result, 0);
cnf := new_aspell_config();
aspell_config_replace(cnf, 'lang', pChar(Lang));
aspell_config_replace(cnf, 'encoding', pChar(Encoding));
ape := new_aspell_speller(cnf);
delete_aspell_config(cnf);
spl := nil;
if aspell_error_number(ape) <> 0 then
Exit
else
spl := to_aspell_speller(ape);
if aspell_speller_check(spl, pChar(Word), Length(Word)) > 0 then
Exit(0)
else begin
sgs := aspell_speller_suggest(spl, pChar(Word), Length(Word));
if aspell_speller_check(FSpeller, pChar(Word), Length(Word)) = 0 then begin
sgs := aspell_speller_suggest(FSpeller, pChar(Word), Length(Word));
elm := aspell_word_list_elements(sgs);
repeat
if i >= Length(Suggestions) then
SetLength(Suggestions, Length(Suggestions) + 10);
if i >= Length(Result) then
SetLength(Result, Length(Result) + 10);
tmp := aspell_string_enumeration_next(elm);
if tmp <> nil then begin
Suggestions[i] := tmp;
Result[i] := tmp;
Inc(i);
end;
until tmp = nil;
SetLength(Suggestions, i);
Result := i;
SetLength(Result, i);
delete_aspell_string_enumeration(elm);
end;
delete_aspell_speller(spl);
end;
end.