summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/aspell/src/spellcheck.pp
blob: 38be5a8c77a6a06a6479d30f88e7b2c916cb732d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
unit SpellCheck;

{ 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
  SysUtils, Classes, Aspell;

type
  TSuggestionArray = array of string;
  
  TWordError = record
    Word: string; // the word itself
    Pos: LongWord; // word position in line
    Length: LongWord; // word length
    Suggestions: TSuggestionArray; // suggestions for the given word
  end;
  
  TLineErrors = array of TWordError;
  TLineErrorsArray = array of TLineErrors;

  { TSpellCheck }

  TSpeller = class // abstract class, basis for all checkers
   protected
    FMode: string;
    FEncoding: string;
    FLanguage: string;
    procedure SetEncoding(const AValue: string);
    procedure SetLanguage(const AValue: string);
    procedure SetMode(const AValue: string);
    procedure CreateSpeller; virtual; abstract;
    procedure FreeSpeller; virtual; abstract;
   public
    constructor Create;
    destructor Destroy; override;
   public
    property Mode: string read FMode write SetMode;
    property Encoding: string read FEncoding write SetEncoding;
    property Language: string read FLanguage write SetLanguage;
  end;
  
  { 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;
   public
    function SpellCheck(const Word: string): TSuggestionArray; // use to check single words, parsed out by you
  end;
  
  { TDocumentSpeller }

  TDocumentSpeller = class(TWordSpeller)
   private
    FChecker: PAspellDocumentChecker;
    FLineErrors: TLineErrorsArray;
    FNameSuggestions: Boolean;
    function GetLineErrors(i: Integer): TLineErrors;
    function GetLineErrorsCount: Integer;
   protected
    procedure CreateSpeller; override;
    procedure FreeSpeller; override;
    procedure DoNameSuggestions(const Word: string; var aWordError: TWordError);
   public
    constructor Create;
    function CheckLine(const aLine: string): TLineErrors;
    function CheckDocument(const FileName: string): Integer; // returns number of spelling errors found or -1 for error
    function CheckDocument(aStringList: TStringList): Integer; // returns number of spelling errors found or -1 for error
    procedure Reset;
   public
    property LineErrors[i: Integer]: TLineErrors read GetLineErrors;
    property LineErrorsCount: Integer read GetLineErrorsCount;
    property NameSuggestions: Boolean read FNameSuggestions write FNameSuggestions;
  end;

implementation

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;

{ TSpeller }

procedure TSpeller.SetEncoding(const AValue: string);
begin
  FEncoding := aValue;
  CreateSpeller;
end;

procedure TSpeller.SetLanguage(const AValue: string);
begin
  FLanguage := aValue;
  CreateSpeller;
end;

procedure TSpeller.SetMode(const AValue: string);
begin
  FMode := aValue;
  CreateSpeller;
end;

constructor TSpeller.Create;
begin
  FEncoding := DEFAULT_ENCODING;
  FLanguage := GetDefaultLanguage;
  FMode := DEFAULT_MODE;

  CreateSpeller;
end;

destructor TSpeller.Destroy;
begin
  FreeSpeller;
end;

{ TWordSpeller }

function TWordSpeller.DoCreateSpeller(Lang, Enc, aMode: pChar): PAspellSpeller;
var
  Error: Paspellcanhaveerror;
begin
  Result := new_aspell_config();

  if Length(FLanguage) > 0 then
    aspell_config_replace(Result, 'lang', Lang);
  if Length(FEncoding) > 0 then
    aspell_config_replace(Result, 'encoding', Enc);
  if Length(FMode) > 0 then
    aspell_config_replace(Result, 'mode', aMode);

  Error := new_aspell_speller(Result);

  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;

  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;
begin
  if Assigned(FSpeller) then begin
    delete_aspell_speller(FSpeller);
    FSpeller := nil;
  end;
end;

function TWordSpeller.SpellCheck(const Word: string): TSuggestionArray;
var
  sgs: Paspellwordlist;
  elm: Paspellstringenumeration;
  tmp: pChar;
  i: Integer = 0;
begin
  SetLength(Result, 0);

  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(Result) then
        SetLength(Result, Length(Result) + 10);

      tmp := aspell_string_enumeration_next(elm);

      if tmp <> nil then begin
        Result[i] := tmp;
        Inc(i);
      end;
    until tmp = nil;

    SetLength(Result, i);

    delete_aspell_string_enumeration(elm);
  end;
end;

{ TDocumentSpeller }

function TDocumentSpeller.GetLineErrors(i: Integer): TLineErrors;
begin
  Result := FLineErrors[i];
end;

function TDocumentSpeller.GetLineErrorsCount: Integer;
begin
  Result := Length(FLineErrors);
end;

procedure TDocumentSpeller.CreateSpeller;
var
  Error: PAspellCanHaveError;
begin
  inherited CreateSpeller;
  
  Error := new_aspell_document_checker(FSpeller);

  if aspell_error_number(Error) <> 0 then
    raise Exception.Create('Error on checker creation: ' + aspell_error_message(Error))
  else
    FChecker := to_aspell_document_checker(Error);
end;

procedure TDocumentSpeller.FreeSpeller;
begin
  if Assigned(FChecker) then begin
    delete_aspell_document_checker(FChecker);
    FChecker := nil;
  end;

  inherited FreeSpeller;
end;

procedure TDocumentSpeller.DoNameSuggestions(const Word: string;
  var aWordError: TWordError);
begin
  aWordError.Suggestions := SpellCheck(Word);
end;

constructor TDocumentSpeller.Create;
begin
  inherited Create;
  
  FNameSuggestions := True;
end;

function TDocumentSpeller.CheckLine(const aLine: string): TLineErrors;
const
  CHUNK_SIZE = 10;
var
  i, Count: Integer;
  Token: AspellToken;
begin
  aspell_document_checker_process(FChecker, pChar(aLine), Length(aLine));

  SetLength(Result, CHUNK_SIZE);
  i := 0;
  Count := 0;
  repeat
    Token := aspell_document_checker_next_misspelling(FChecker);

    if Token.len > 0 then begin
      if Length(Result) <= i then
        SetLength(Result, Length(Result) + CHUNK_SIZE);

      Result[i].Word := Copy(aLine, Token.offset + 1, Token.len);
      Result[i].Pos := Token.offset + 1; // C goes from 0, we go from 1
      Result[i].Length := Token.len;

      if FNameSuggestions then
        DoNameSuggestions(Copy(aLine, Token.offset + 1, Token.len), Result[i]);
        
      Inc(Count);
    end;

    Inc(i);
  until Token.len = 0;
  
  SetLength(Result, Count);
end;

function TDocumentSpeller.CheckDocument(const FileName: string): Integer;
var
  s: TStringList;
begin
  Result := 0;
  if FileExists(FileName) then try
    s := TStringList.Create;
    s.LoadFromFile(FileName);
    Result := CheckDocument(s);
  finally
    s.Free;
  end;
end;

function TDocumentSpeller.CheckDocument(aStringList: TStringList): Integer;
var
  i: Integer;
begin
  Result := 0;
  SetLength(FLineErrors, aStringList.Count);

  for i := 0 to aStringList.Count - 1 do begin
    FLineErrors[i] := CheckLine(aStringList[i]);
    Inc(Result, Length(FLineErrors[i]));
  end;
end;

procedure TDocumentSpeller.Reset;
begin
  aspell_document_checker_reset(FChecker);
end;

end.