Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program New;
- function GetWords(str: string): TStrArray;
- var
- h, i, r: Integer;
- begin
- Result := Explode(' ', Replace(Replace(str, #10, #13), #13, ' '));
- h := High(Result);
- for i := 0 to h do
- if (Result[i] <> '') then
- begin
- Result[r] := Result[i];
- Inc(r);
- end;
- SetLength(Result, r);
- end;
- function KeyDistance(CharA, CharB: Char): Extended;
- var
- kbd:Array of TStrArray;
- i,j,l:Integer;
- A,B:TPoint;
- CharASet,CharBSet:Boolean;
- begin
- SetLength(kbd, 4);
- SetLength(kbd[0], 12);
- kbd[0][0] := '1!';
- kbd[0][1] := '2@';
- kbd[0][2] := '3#';
- kbd[0][3] := '4$';
- kbd[0][4] := '5%';
- kbd[0][5] := '6^';
- kbd[0][6] := '7&';
- kbd[0][7] := '8*';
- kbd[0][8] := '9(';
- kbd[0][9] := '0)';
- kbd[0][10] := '-_';
- kbd[0][11] := '+=';
- SetLength(kbd[1], 12);
- kbd[1][0] := 'qQ';
- kbd[1][1] := 'wW';
- kbd[1][2] := 'eE';
- kbd[1][3] := 'rR';
- kbd[1][4] := 'tT';
- kbd[1][4] := 'yY';
- kbd[1][5] := 'uU';
- kbd[1][6] := 'iI';
- kbd[1][7] := 'oO';
- kbd[1][8] := 'pP';
- kbd[1][9] := '{[';
- kbd[1][10] := '}]';
- kbd[1][11] := '|\';
- SetLength(kbd[2], 11);
- kbd[2][0] := 'aA';
- kbd[2][1] := 'sS';
- kbd[2][2] := 'dD';
- kbd[2][3] := 'fF';
- kbd[2][4] := 'gG';
- kbd[2][5] := 'hH';
- kbd[2][6] := 'jJ';
- kbd[2][7] := 'kK';
- kbd[2][8] := 'lL';
- kbd[2][9] := ':;';
- kbd[2][10] := '"''';
- SetLength(kbd[3], 11);
- kbd[3][0] := '~';
- kbd[3][1] := 'zZ';
- kbd[3][2] := 'xX';
- kbd[3][3] := 'cC';
- kbd[3][4] := 'vV';
- kbd[3][5] := 'bB';
- kbd[3][6] := 'nN';
- kbd[3][7] := 'mM';
- kbd[3][8] := ',<';
- kbd[3][9] := '.>';
- kbd[3][10] := '?/';
- A.x := 2; //centralize unknown chars | to not muss up measuring.
- A.y := 5;
- B.x := 2;
- B.y := 5;
- for l:=0 to High(kbd) do
- for i:=0 to High(kbd[l]) do
- for j:=1 to Length(kbd[l][i]) do
- begin
- if (kbd[l][i][j] = charA) and not(CharASet) then
- begin
- A.x := l;
- A.y := i+j-1;
- CharASet := True;
- end;
- if (kbd[l][i][j] = charB) and not(CharBSet) then
- begin
- B.x := l;
- B.y := i+j-1;
- CharBSet := True;
- end;
- if CharBSet and CharASet then Break;
- end;
- Result := Sqrt(Sqr(A.x-B.x) + Sqr(A.y-B.y));
- end;
- function StrQwertyDist(StrA, StrB:String; InPercent:Boolean): Extended;
- var
- i,j,Lw,Mw,L,lenA,lenB,c:Integer;
- Undefined: Char;
- WordsA, WordsB: TStrArray;
- begin
- WordsA := GetWords(StrA);
- WordsB := GetWords(StrB);
- Undefined := '|';
- Lw := Min(High(WordsA), High(WordsB));
- Mw := Max(High(WordsA), High(WordsB));
- c := 0;
- for j:=0 to Lw do
- begin
- LenA := Length(WordsA[j]);
- LenB := Length(WordsB[j]);
- L := Max(LenA, LenB);
- for i:=1 to L do
- begin
- if (i > lenA) then begin
- Result := Result + KeyDistance(Undefined, WordsB[j][i]);
- end else if (i > lenB) then begin
- Result := Result + KeyDistance(WordsA[j][i], Undefined);
- end else
- Result := Result + KeyDistance(WordsA[j][i],WordsB[j][i]);
- Inc(c);
- end;
- end;
- Result := Result + (Abs(Lw-Mw) * 5.4);
- if InPercent then Result := Result / (c * (Sqrt(Sqr(3) + Sqr(10)))) * 100;
- end;
- begin
- WriteLn(StrQwertyDist('Talk-to Banker / 4 more options', '$alk-to Bakor / 4 more opt;ons', True));
- WriteLn(StrQwertyDist('This is a long string', 'thix ia a lpng strkng', True));
- WriteLn(StrQwertyDist('Testing someting else here', 'what in the world is going on?', True));
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement