Delphi的拼写检查组件

ska*_*adt 7 delphi spell-checking

我们为社区开发的Twitter客户端的一个要求是拼写检查组件.您在应用程序中使用了哪些拼写检查组件/系统,以及您使用它的经验是什么?

Moh*_*man 11

Addict Component Suite是Delphi中最完整的一个,但它不是免费的.

但是我认为你正在为你的twitter实用程序寻找免费软件,我已经使用LS Speller免费项目并且与我合作,它基于ISpell,所以你可以用更新的胜利更新它.

但是还没有D2009更新,似乎没有积极开发.

使用内置词典中MS Word的另一种选择.


Ian*_*oyd 5

Windows附带拼写检查API(Windows 8).

TWindow8SpellChecker = class(TCustomSpellChecker)
private
    FSpellChecker: ISpellChecker;
public
    constructor Create(LanguageTag: UnicodeString='en-US');

    procedure Check(const text: UnicodeString; const Errors: TList); override; //gives a list of TSpellingError objects
    function Suggest(const word: UnicodeString; const Suggestions: TStrings): Boolean; override;
end;
Run Code Online (Sandbox Code Playgroud)

实施:

constructor TWindow8SpellChecker.Create(LanguageTag: UnicodeString='en-US');
var
    factory: ISpellCheckerFactory;
begin
    inherited Create;

    factory := CoSpellCheckerFactory.Create;
    OleCheck(factory.CreateSpellChecker(LanguageTag, {out}FSpellChecker));
end;

procedure TWindow8SpellChecker.Check(const text: UnicodeString; const Errors: TList);
var
    enumErrors: IEnumSpellingError;
    error: ISpellingError;
    spellingError: TSpellingError;
begin
    if text = '' then
        Exit;

    OleCheck(FSpellChecker.Check(text, {out}enumErrors));

    while (enumErrors.Next({out}error) = S_OK) do
    begin
        spellingError := TSpellingError.Create(
                error.StartIndex,
                error.Length,
                error.CorrectiveAction,
                error.Replacement);
        Errors.Add(spellingError);
    end;
end;

function TWindow8SpellChecker.Suggest(const word: UnicodeString; const Suggestions: TStrings): Boolean;
var
    hr: HRESULT;
    enumSuggestions: IEnumString;
    ws: PWideChar;
    fetched: LongInt;
begin
    if (word = '') then
    begin
        Result := False;
        Exit;
    end;

    hr := FSpellChecker.Suggest(word, {out}enumSuggestions);
    OleCheck(hr);

    Result := (hr = S_OK); //returns S_FALSE if the word is spelled correctly

    ws := '';
    while enumSuggestions.Next(1, {out}ws, {out}@fetched) = S_OK do
    begin
        if fetched < 1 then
            Continue;

        Suggestions.Add(ws);

        CoTaskMemFree(ws);
    end;
end;
Run Code Online (Sandbox Code Playgroud)

TSpellingError对象是围绕四个值的琐碎包装:

TSpellingError = class(TObject)
protected
    FStartIndex: ULONG;
    FLength: ULONG;
    FCorrectiveAction: CORRECTIVE_ACTION;
    FReplacement: UnicodeString;
public
    constructor Create(StartIndex, Length: ULONG; CorrectiveAction: CORRECTIVE_ACTION; Replacement: UnicodeString);
    property StartIndex: ULONG read FStartIndex;
    property Length: ULONG read FLength;
    property CorrectiveAction: CORRECTIVE_ACTION read FCorrectiveAction;
    property Replacement: UnicodeString read FReplacement;
end;
Run Code Online (Sandbox Code Playgroud)