Delphi中比较两个字符串相似性的百分比算法
2010-07-27 20:47
399 查看
用百分比比较两个字符串(彼此之间有多少相似度)
返回 byte 类型,从 0 到 100%
function
CompareStringsInPercent(Str1, Str2: string
): Byte;
type
TLink = array
[0..1] of
Byte;
var
tmpPattern: TLink;
PatternA, PatternB: array of
TLink;
IndexA, IndexB, LengthStr: Integer;
begin
Result := 100;
// Building pattern tables
LengthStr := Max(Length(Str1), Length(Str2));
for
IndexA := 1 to
LengthStr do
begin
if
Length(Str1) >= IndexA then
begin
SetLength(PatternA, (Length(PatternA) + 1));
PatternA[Length(PatternA) - 1][0] := Byte(Str1[IndexA]);
PatternA[Length(PatternA) - 1][1] := IndexA;
end
;
if
Length(Str2) >= IndexA then
begin
SetLength(PatternB, (Length(PatternB) + 1));
PatternB[Length(PatternB) - 1][0] := Byte(Str2[IndexA]);
PatternB[Length(PatternB) - 1][1] := IndexA;
end
;
end
;
// Quick Sort of pattern tables
IndexA := 0;
IndexB := 0;
while
((IndexA < (Length(PatternA) - 1)) and
(IndexB < (Length(PatternB) - 1))) do
begin
if
Length(PatternA) > IndexA then
begin
if
PatternA[IndexA][0] < PatternA[IndexA + 1][0] then
begin
tmpPattern[0] := PatternA[IndexA][0];
tmpPattern[1] := PatternA[IndexA][1];
PatternA[IndexA][0] := PatternA[IndexA + 1][0];
PatternA[IndexA][1] := PatternA[IndexA + 1][1];
PatternA[IndexA + 1][0] := tmpPattern[0];
PatternA[IndexA + 1][1] := tmpPattern[1];
if
IndexA > 0 then
Dec(IndexA);
end
else
Inc(IndexA);
end
;
if
Length(PatternB) > IndexB then
begin
if
PatternB[IndexB][0] < PatternB[IndexB + 1][0] then
begin
tmpPattern[0] := PatternB[IndexB][0];
tmpPattern[1] := PatternB[IndexB][1];
PatternB[IndexB][0] := PatternB[IndexB + 1][0];
PatternB[IndexB][1] := PatternB[IndexB + 1][1];
PatternB[IndexB + 1][0] := tmpPattern[0];
PatternB[IndexB + 1][1] := tmpPattern[1];
if
IndexB > 0 then
Dec(IndexB);
end
else
Inc(IndexB);
end
;
end
;
// Calculating simularity percentage
LengthStr := Min(Length(PatternA), Length(PatternB));
for
IndexA := 0 to
(LengthStr - 1) do
begin
if
PatternA[IndexA][0] = PatternB[IndexA][0] then
begin
if
Max(PatternA[IndexA][1], PatternB[IndexA][1]) - Min(PatternA[IndexA][1],
PatternB[IndexA][1]) > 0 then
Dec(Result,
((100 div
LengthStr) div
(Max(PatternA[IndexA][1], PatternB[IndexA][1]) -
Min(PatternA[IndexA][1], PatternB[IndexA][1]))))
else if
Result < 100 then
Inc(Result);
end
else
Dec(Result, (100 div
LengthStr))
end
;
SetLength(PatternA, 0);
SetLength(PatternB, 0);
end
;
返回 byte 类型,从 0 到 100%
function
CompareStringsInPercent(Str1, Str2: string
): Byte;
type
TLink = array
[0..1] of
Byte;
var
tmpPattern: TLink;
PatternA, PatternB: array of
TLink;
IndexA, IndexB, LengthStr: Integer;
begin
Result := 100;
// Building pattern tables
LengthStr := Max(Length(Str1), Length(Str2));
for
IndexA := 1 to
LengthStr do
begin
if
Length(Str1) >= IndexA then
begin
SetLength(PatternA, (Length(PatternA) + 1));
PatternA[Length(PatternA) - 1][0] := Byte(Str1[IndexA]);
PatternA[Length(PatternA) - 1][1] := IndexA;
end
;
if
Length(Str2) >= IndexA then
begin
SetLength(PatternB, (Length(PatternB) + 1));
PatternB[Length(PatternB) - 1][0] := Byte(Str2[IndexA]);
PatternB[Length(PatternB) - 1][1] := IndexA;
end
;
end
;
// Quick Sort of pattern tables
IndexA := 0;
IndexB := 0;
while
((IndexA < (Length(PatternA) - 1)) and
(IndexB < (Length(PatternB) - 1))) do
begin
if
Length(PatternA) > IndexA then
begin
if
PatternA[IndexA][0] < PatternA[IndexA + 1][0] then
begin
tmpPattern[0] := PatternA[IndexA][0];
tmpPattern[1] := PatternA[IndexA][1];
PatternA[IndexA][0] := PatternA[IndexA + 1][0];
PatternA[IndexA][1] := PatternA[IndexA + 1][1];
PatternA[IndexA + 1][0] := tmpPattern[0];
PatternA[IndexA + 1][1] := tmpPattern[1];
if
IndexA > 0 then
Dec(IndexA);
end
else
Inc(IndexA);
end
;
if
Length(PatternB) > IndexB then
begin
if
PatternB[IndexB][0] < PatternB[IndexB + 1][0] then
begin
tmpPattern[0] := PatternB[IndexB][0];
tmpPattern[1] := PatternB[IndexB][1];
PatternB[IndexB][0] := PatternB[IndexB + 1][0];
PatternB[IndexB][1] := PatternB[IndexB + 1][1];
PatternB[IndexB + 1][0] := tmpPattern[0];
PatternB[IndexB + 1][1] := tmpPattern[1];
if
IndexB > 0 then
Dec(IndexB);
end
else
Inc(IndexB);
end
;
end
;
// Calculating simularity percentage
LengthStr := Min(Length(PatternA), Length(PatternB));
for
IndexA := 0 to
(LengthStr - 1) do
begin
if
PatternA[IndexA][0] = PatternB[IndexA][0] then
begin
if
Max(PatternA[IndexA][1], PatternB[IndexA][1]) - Min(PatternA[IndexA][1],
PatternB[IndexA][1]) > 0 then
Dec(Result,
((100 div
LengthStr) div
(Max(PatternA[IndexA][1], PatternB[IndexA][1]) -
Min(PatternA[IndexA][1], PatternB[IndexA][1]))))
else if
Result < 100 then
Inc(Result);
end
else
Dec(Result, (100 div
LengthStr))
end
;
SetLength(PatternA, 0);
SetLength(PatternB, 0);
end
;
相关文章推荐
- Delphi中比较两个字符串相似性的百分比算法
- 比较两个字符串的最长的相同字符串 及其位置和百分比
- 比较两个字符串的最长的相同字符串 及其位置和百分比
- 两个字符串比较求最优算法
- C# 两个字符串的比较
- 如何比较两个时间字符串的大小
- 比较两个字符串大小
- Edit Distance(编辑距离)算法。计算两个字符串的相似程度。
- 从“比较两个含有多个不同元素的集合是否相同”引申出的几种算法
- Java 比较两个对象是否相等【含字符串,区分与不区分大小写】
- C#下比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- php比较两个字符串长度的方法
- Delphi: 使用通配符进行字符串比较的函数MatchesMask
- PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法
- 比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- 第16周项目2-比较两个字符串长度(指针作形参)
- 比较两个字符串的大小
- 比较两个字符串
- 第十七周上机任务-项目2(7)-比较两个字符串
- 每天三道冲刺工作--比较两个字符串,用 O(n)时间和恒量空间。