{從字符串中提取單詞的函數}
procedure StrToWordList(str: string; var List: TStringList); var
p: PChar;
i: Integer;
begin
if List = nil then List := TStringList.Create;
List.Clear;
{去除重復}
List.Sorted := True;
List.Duplicates := dupIgnore;
p := PChar(str);
{把單詞以外的字符轉為空格, 並把大寫字母轉小寫}
while p^ <> #0 do
begin
case p^ of
'A'..'Z': p^ := Chr(Ord(p^) + 32);
'a'..'z', '0'..'9', '''', '-': ;
else p^ := #32;
end;
Inc(p);
end;
{用空格分離單詞到列表}
List.Delimiter := #32;
List.DelimitedText := str;
{單詞的開頭應該是字母, 去除其他}
for i := List.Count - 1 downto 0 do
begin
if CharInSet(List[i][1], ['0'..'9', '-', '''']) then
List.Delete(i);
end;
end;
{從字符串中提取漢字的函數}
procedure StrToHanZiList(str: string; var List: TStringList);
var
p: PWideChar;
begin
if List = nil then List := TStringList.Create;
List.Clear;
{去除重復}
List.Sorted := True;
List.Duplicates := dupIgnore;
p := PWideChar(str);
while p^ <> #0 do
begin
case p^ of
#$4E00..#$9FA5: List.Add(p^);
end;
Inc(p);
end;
end;