scheck.pp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. unit sCheck;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Aspell;
  6. type
  7. TSuggestionArray = array of string;
  8. function SpellCheck(const Word, Lang: string; out Suggestions: TSuggestionArray): Integer;
  9. var
  10. Encoding: string = 'utf-8';
  11. implementation
  12. function SpellCheck(const Word, Lang: string; out Suggestions: TSuggestionArray): Integer;
  13. var
  14. cnf: aspellconfig;
  15. ape: aspellcanhaveerror;
  16. spl: aspellspeller;
  17. sgs: aspellwordlist;
  18. elm: aspellstringenumeration;
  19. tmp: pChar;
  20. i: Integer = 0;
  21. begin
  22. SetLength(Suggestions, 10);
  23. Result := -1;
  24. cnf := new_aspell_config();
  25. aspell_config_replace(cnf, 'lang', pChar(Lang));
  26. aspell_config_replace(cnf, 'encoding', pChar(Encoding));
  27. ape := new_aspell_speller(cnf);
  28. delete_aspell_config(cnf);
  29. spl := nil;
  30. if aspell_error_number(ape) <> 0 then
  31. Exit
  32. else
  33. spl := to_aspell_speller(ape);
  34. if aspell_speller_check(spl, pChar(Word), Length(Word)) > 0 then
  35. Exit(0)
  36. else begin
  37. sgs := aspell_speller_suggest(spl, pChar(Word), Length(Word));
  38. elm := aspell_word_list_elements(sgs);
  39. repeat
  40. if i >= Length(Suggestions) then
  41. SetLength(Suggestions, Length(Suggestions) + 10);
  42. tmp := aspell_string_enumeration_next(elm);
  43. if tmp <> nil then begin
  44. Suggestions[i] := tmp;
  45. Inc(i);
  46. end;
  47. until tmp = nil;
  48. SetLength(Suggestions, i);
  49. Result := i;
  50. delete_aspell_string_enumeration(elm);
  51. end;
  52. delete_aspell_speller(spl);
  53. end;
  54. end.