Advertisement
WarPie90

Untitled

Dec 8th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.53 KB | None | 0 0
  1. program New;
  2.  
  3. function GetWords(str: string): TStrArray;
  4. var
  5.   h, i, r: Integer;
  6. begin
  7.   Result := Explode(' ', Replace(Replace(str, #10, #13), #13, ' '));
  8.   h := High(Result);
  9.   for i := 0 to h do  
  10.     if (Result[i] <> '') then
  11.     begin
  12.       Result[r] := Result[i];
  13.       Inc(r);
  14.     end;
  15.   SetLength(Result, r);
  16. end;
  17.  
  18.  
  19. function KeyDistance(CharA, CharB: Char): Extended;
  20. var
  21.   kbd:Array of TStrArray;
  22.   i,j,l:Integer;
  23.   A,B:TPoint;
  24.   CharASet,CharBSet:Boolean;
  25. begin
  26.   SetLength(kbd, 4);
  27.   SetLength(kbd[0], 12);
  28.   kbd[0][0] := '1!';
  29.   kbd[0][1] := '2@';
  30.   kbd[0][2] := '3#';
  31.   kbd[0][3] := '4$';
  32.   kbd[0][4] := '5%';
  33.   kbd[0][5] := '6^';
  34.   kbd[0][6] := '7&';
  35.   kbd[0][7] := '8*';
  36.   kbd[0][8] := '9(';
  37.   kbd[0][9] := '0)';  
  38.   kbd[0][10] := '-_';
  39.   kbd[0][11] := '+=';  
  40.   SetLength(kbd[1], 12);
  41.   kbd[1][0] := 'qQ';
  42.   kbd[1][1] := 'wW';
  43.   kbd[1][2] := 'eE';
  44.   kbd[1][3] := 'rR';
  45.   kbd[1][4] := 'tT';
  46.   kbd[1][4] := 'yY';
  47.   kbd[1][5] := 'uU';
  48.   kbd[1][6] := 'iI';
  49.   kbd[1][7] := 'oO';
  50.   kbd[1][8] := 'pP';
  51.   kbd[1][9] := '{[';
  52.   kbd[1][10] := '}]';
  53.   kbd[1][11] := '|\';  
  54.   SetLength(kbd[2], 11);
  55.   kbd[2][0] := 'aA';  
  56.   kbd[2][1] := 'sS';
  57.   kbd[2][2] := 'dD';
  58.   kbd[2][3] := 'fF';
  59.   kbd[2][4] := 'gG';
  60.   kbd[2][5] := 'hH';
  61.   kbd[2][6] := 'jJ';
  62.   kbd[2][7] := 'kK';
  63.   kbd[2][8] := 'lL';
  64.   kbd[2][9] := ':;';
  65.   kbd[2][10] := '"''';
  66.   SetLength(kbd[3], 11);
  67.   kbd[3][0] := '~';
  68.   kbd[3][1] := 'zZ';
  69.   kbd[3][2] := 'xX';
  70.   kbd[3][3] := 'cC';
  71.   kbd[3][4] := 'vV';
  72.   kbd[3][5] := 'bB';
  73.   kbd[3][6] := 'nN';
  74.   kbd[3][7] := 'mM';
  75.   kbd[3][8] := ',<';
  76.   kbd[3][9] := '.>';
  77.   kbd[3][10] := '?/';
  78.  
  79.   A.x := 2; //centralize unknown chars | to not muss up measuring.
  80.   A.y := 5;  
  81.   B.x := 2;
  82.   B.y := 5;
  83.   for l:=0 to High(kbd) do
  84.     for i:=0 to High(kbd[l]) do
  85.       for j:=1 to Length(kbd[l][i]) do
  86.       begin
  87.         if (kbd[l][i][j] = charA) and not(CharASet) then
  88.         begin
  89.           A.x := l;
  90.           A.y := i+j-1;
  91.           CharASet := True;
  92.         end;
  93.         if (kbd[l][i][j] = charB) and not(CharBSet) then
  94.         begin
  95.           B.x := l;
  96.           B.y := i+j-1;
  97.           CharBSet := True;
  98.         end;
  99.         if CharBSet and CharASet then Break;  
  100.       end;
  101.   Result := Sqrt(Sqr(A.x-B.x) + Sqr(A.y-B.y));  
  102. end;
  103.  
  104. function StrQwertyDist(StrA, StrB:String; InPercent:Boolean): Extended;
  105. var
  106.   i,j,Lw,Mw,L,lenA,lenB,c:Integer;
  107.   Undefined: Char;
  108.   WordsA, WordsB: TStrArray;
  109. begin
  110.   WordsA := GetWords(StrA);
  111.   WordsB := GetWords(StrB);
  112.   Undefined := '|';    
  113.   Lw := Min(High(WordsA), High(WordsB));
  114.   Mw := Max(High(WordsA), High(WordsB));
  115.   c := 0;
  116.   for j:=0 to Lw do
  117.   begin
  118.     LenA := Length(WordsA[j]);
  119.     LenB := Length(WordsB[j]);
  120.     L := Max(LenA, LenB);
  121.     for i:=1 to L do
  122.     begin
  123.       if (i > lenA) then begin
  124.         Result := Result + KeyDistance(Undefined, WordsB[j][i]);  
  125.       end else if (i > lenB) then begin
  126.         Result := Result + KeyDistance(WordsA[j][i], Undefined);  
  127.       end else
  128.         Result := Result + KeyDistance(WordsA[j][i],WordsB[j][i]);
  129.       Inc(c);
  130.     end;  
  131.   end;
  132.   Result := Result + (Abs(Lw-Mw) * 5.4);
  133.   if InPercent then Result := Result / (c * (Sqrt(Sqr(3) + Sqr(10)))) * 100;
  134. end;
  135.  
  136. begin
  137.   WriteLn(StrQwertyDist('Talk-to Banker / 4 more options', '$alk-to  Bakor / 4 more opt;ons', True));
  138.   WriteLn(StrQwertyDist('This is a long string', 'thix ia a lpng strkng', True));
  139.   WriteLn(StrQwertyDist('Testing someting else here', 'what in the world is going on?', True));
  140. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement