Delphi的拼写检查组件

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

解决方法

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;

实施:

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;

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;

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...