📄 jvqspellchecker.pas
字号:
if FileExists(Dictionary) then
begin
System.AssignFile(AFile, Dictionary);
System.Reset(AFile);
try
repeat
Readln(AFile, Value);
if Value <> '' then
begin
// (rom) simple compession for dictionary
N := Ord(Value[1]) - Ord('0');
Value := Copy(Value, 2, Length(Value) - 1);
if N > 0 then
Value := Copy(LastValue, 1, N) + Value;
LastValue := Value;
Value := AnsiLowerCase(Value);
StrAddRef(Value);
AddWord(Value);
SoundexVal := Soundex(Value);
AddSoundex(SoundexVal, Value);
end;
until Eof(AFile);
finally
System.Close(AFile);
end;
for I := 0 to UserDictionary.Count - 1 do
if UserDictionary[I] <> '' then
begin
Value := AnsiLowerCase(UserDictionary[I]);
StrAddRef(Value);
AddWord(Value);
SoundexVal := Soundex(Value);
AddSoundex(SoundexVal, Value);
end;
end;
end;
procedure TJvDefaultSpellChecker.ClearTables;
var
I, J: Integer;
List: TList;
S: string;
begin
if FSoundexTable <> nil then
for I := 0 to FSoundexTable.Count - 1 do
begin
TList(FSoundexTable[I]).Free;
FSoundexTable[I] := nil;
end;
// FSoundexTable.Clear;
if FWordTable <> nil then
for I := 0 to FWordTable.Count - 1 do
begin
List := TList(FWordTable[I]);
if List <> nil then
begin
for J := 0 to List.Count - 1 do
begin
S := PChar(List[J]);
StrDecRef(S);
end;
TList(FWordTable[I]).Free;
FWordTable[I] := nil;
end;
end;
// FWordTable.Clear;
end;
function TJvDefaultSpellChecker.GetSuggestions: TStrings;
begin
Result := FSuggestions;
end;
function TJvDefaultSpellChecker.GetText: string;
begin
Result := FText;
end;
procedure TJvDefaultSpellChecker.GetWordSuggestions(const Value: string; AStrings: TStrings);
var
SoundexVal: TSoundex;
I, Hash: Integer;
List: TList;
begin
if AStrings = nil then
Exit;
AStrings.BeginUpdate;
try
AStrings.Clear;
SoundexVal := Soundex(Value);
Hash := SoundexHash(SoundexVal) mod SoundexTableSize;
if FSoundexTable[Hash] <> nil then
begin
List := TList(FSoundexTable[Hash]);
for I := 0 to List.Count - 1 do
AStrings.Add(string(List[I]));
end;
finally
AStrings.EndUpdate;
end;
end;
function TJvDefaultSpellChecker.Next(out StartIndex, WordLength: Integer): WordBool;
var
S: PChar;
begin
StartIndex := 0;
WordLength := 0;
Result := False;
if FPosition <= 0 then
FPosition := 1;
if FPosition >= Length(FText) then
Exit;
S := PChar(Text) + FPosition - 1;
if (S = nil) or (S^ = #0) then
Exit;
while True do
begin
FCurrentWord := '';
GetNextWord(S, FCurrentWord, Delimiters);
WordLength := Length(FCurrentWord);
StartIndex := S - PChar(Text) - WordLength + 1;
FPosition := StartIndex + WordLength;
if (S = nil) or (S^ = #0) then
Break;
if (FCurrentWord <> '') and not CanIgnore(FCurrentWord) then
begin
FSuggestions.Clear;
Result := not WordExists(FCurrentWord);
if Result then
begin
GetWordSuggestions(FCurrentWord, FSuggestions);
Break;
end;
end;
end;
end;
procedure TJvDefaultSpellChecker.Seek(Position: Integer);
begin
FPosition := Position;
end;
procedure TJvDefaultSpellChecker.SetText(const Value: string);
begin
FText := Value;
FPosition := 1;
end;
function TJvDefaultSpellChecker.WordExists(const Value: string): Boolean;
var
I: Integer;
Hash: Integer;
List: TList;
FWord: string;
begin
FWord := AnsiLowerCase(Value);
Hash := ELFHash(FWord) mod WordTableSize;
if FWordTable[Hash] <> nil then
begin
List := TList(FWordTable[Hash]);
for I := 0 to List.Count - 1 do
if AnsiCompareText(PChar(List[I]), FWord) = 0 then
begin
Result := True;
Exit;
end;
end;
// ignore or user word?
Result := (UserDictionary.IndexOf(FWord) > -1) or (Ignores.IndexOf(FWord) > -1);
end;
function TJvDefaultSpellChecker.GetDictionary: string;
begin
Result := FDictionary;
end;
function TJvDefaultSpellChecker.GetUserDictionary: TStrings;
begin
Result := FUserDictionary;
end;
procedure TJvDefaultSpellChecker.SetDictionary(const Value: string);
begin
if FDictionary <> Value then
begin
FDictionary := Value;
BuildTables;
end;
end;
procedure TJvDefaultSpellChecker.SetUserDictionary(const Value: TStrings);
begin
FUserDictionary.Assign(Value);
end;
function TJvDefaultSpellChecker.GetIgnores: TStrings;
begin
Result := FIgnores;
end;
procedure TJvDefaultSpellChecker.SetIgnores(const Value: TStrings);
begin
FIgnores.Assign(Value);
end;
function TJvDefaultSpellChecker.GetDelimiters: TSysCharSet;
begin
Result := FDelimiters;
end;
procedure TJvDefaultSpellChecker.SetDelimiters(const Value: TSysCharSet);
begin
FDelimiters := Value;
end;
function TJvDefaultSpellChecker.CanIgnore(const Value: string): Boolean;
begin
Result := False;
if Assigned(FOnCanIgnore) then
FOnCanIgnore(Self, Value, Result);
end;
function TJvDefaultSpellChecker.GetCanIgnore: TJvSpellCheckIgnoreEvent;
begin
Result := FOnCanIgnore;
end;
procedure TJvDefaultSpellChecker.SetCanIgnore(const Value: TJvSpellCheckIgnoreEvent);
begin
FOnCanIgnore := Value;
end;
function TJvDefaultSpellChecker.GetCurrentWord: string;
begin
Result := FCurrentWord;
end;
//=== TJvSpellChecker ========================================================
function TJvSpellChecker.GetCanIgnore: TJvSpellCheckIgnoreEvent;
begin
Result := SpellChecker.OnCanIgnore;
end;
function TJvSpellChecker.GetDelimiters: TSysCharSet;
begin
Result := SpellChecker.Delimiters;
end;
function TJvSpellChecker.GetDictionary: TFileName;
begin
Result := SpellChecker.Dictionary;
end;
function TJvSpellChecker.GetIgnores: TStrings;
begin
Result := SpellChecker.Ignores;
end;
function TJvSpellChecker.GetSpellChecker: IJvSpellChecker;
begin
if FSpellChecker = nil then
begin
if not Assigned(CreateSpellChecker) then
raise EJVCLException.Create(RsENoSpellCheckerAvailable);
FSpellChecker := CreateSpellChecker;
end;
Result := FSpellChecker;
end;
function TJvSpellChecker.GetText: string;
begin
Result := SpellChecker.GetText;
end;
function TJvSpellChecker.GetUserDictionary: TStrings;
begin
Result := SpellChecker.UserDictionary;
end;
procedure TJvSpellChecker.SetCanIgnore(const Value: TJvSpellCheckIgnoreEvent);
begin
SpellChecker.OnCanIgnore := Value;
end;
procedure TJvSpellChecker.SetDelimiters(const Value: TSysCharSet);
begin
SpellChecker.Delimiters := Value;
end;
procedure TJvSpellChecker.SetDictionary(const Value: TFileName);
begin
SpellChecker.Dictionary := Value;
end;
procedure TJvSpellChecker.SetIgnores(const Value: TStrings);
begin
SpellChecker.Ignores := Value;
end;
procedure TJvSpellChecker.SetText(const Value: string);
begin
SpellChecker.SetText(Value);
end;
procedure TJvSpellChecker.SetUserDictionary(const Value: TStrings);
begin
SpellChecker.UserDictionary := Value;
end;
initialization
@CreateSpellChecker := @InternalCreateSpellChecker;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -