我们为社区开发的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;@H_403_7@实施:
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;@H_403_7@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;@H_403_7@