| 1 |
unit EUCSampler; |
| 2 |
|
| 3 |
interface |
| 4 |
|
| 5 |
const |
| 6 |
FreqSize = 94; |
| 7 |
type |
| 8 |
rEUCStatistics = record |
| 9 |
mFirstByteFreq: array[0..Pred(FreqSize)] of Double; |
| 10 |
mFirstByteStdDev, |
| 11 |
mFirstByteMean, |
| 12 |
mFirstByteWeight: Double; |
| 13 |
mSecoundByteFreq: array [0..Pred(FreqSize)] of Double; |
| 14 |
mSecoundByteStdDev, |
| 15 |
mSecoundByteMean, |
| 16 |
mSecoundByteWeight: Double; |
| 17 |
end; |
| 18 |
prEUCStatistics = ^rEUCStatistics; |
| 19 |
|
| 20 |
type |
| 21 |
TEUCSampler = class (TObject) |
| 22 |
private |
| 23 |
mTotal, |
| 24 |
mThreshold, |
| 25 |
mState: integer; |
| 26 |
mFirstByteCnt: array [0..Pred(FreqSize)] of integer; |
| 27 |
mSecondByteCnt: array [0..Pred(FreqSize)] of integer; |
| 28 |
mFirstByteFreq: array [0..Pred(FreqSize)] of double; |
| 29 |
mSecondByteFreq: array [0..Pred(FreqSize)] of double; |
| 30 |
public |
| 31 |
constructor Create; |
| 32 |
destructor Destroy; override; |
| 33 |
|
| 34 |
function Sample(aIn: pAnsiChar; aLen: integer): Boolean; |
| 35 |
function GetSomeData: Boolean; |
| 36 |
function EnoughData: Boolean; |
| 37 |
procedure CalFreq; |
| 38 |
function GetScore(const aFirstByteFreq: array of Double; aFirstByteWeight: Double; |
| 39 |
const aSecondByteFreq: array of Double; aSecondByteWeight: Double): double; overload; virtual; |
| 40 |
function GetScore(const array1, array2: array of Double): double; overload; virtual; |
| 41 |
procedure Reset; |
| 42 |
end; |
| 43 |
|
| 44 |
implementation |
| 45 |
|
| 46 |
{ TEUCSampler } |
| 47 |
|
| 48 |
procedure TEUCSampler.CalFreq; |
| 49 |
var |
| 50 |
i: integer; |
| 51 |
begin |
| 52 |
for i := 0 to Pred(FreqSize) do |
| 53 |
begin |
| 54 |
mFirstByteFreq[i] := mFirstByteCnt[i] / mTotal; |
| 55 |
mSecondByteFreq[i] := mSecondByteCnt[i] / mTotal; |
| 56 |
end; |
| 57 |
end; |
| 58 |
|
| 59 |
constructor TEUCSampler.Create; |
| 60 |
begin |
| 61 |
inherited; |
| 62 |
Reset; |
| 63 |
end; |
| 64 |
|
| 65 |
destructor TEUCSampler.Destroy; |
| 66 |
begin |
| 67 |
|
| 68 |
inherited; |
| 69 |
end; |
| 70 |
|
| 71 |
function TEUCSampler.EnoughData: Boolean; |
| 72 |
begin |
| 73 |
Result := mTotal > mThreshold; |
| 74 |
end; |
| 75 |
|
| 76 |
function TEUCSampler.GetScore(const aFirstByteFreq: array of Double; aFirstByteWeight: Double; |
| 77 |
const aSecondByteFreq: array of Double; aSecondByteWeight: Double): double; |
| 78 |
begin |
| 79 |
Result := aFirstByteWeight * GetScore(aFirstByteFreq, mFirstByteFreq) + |
| 80 |
aSecondByteWeight * GetScore(aSecondByteFreq, mSecondByteFreq); |
| 81 |
end; |
| 82 |
|
| 83 |
function TEUCSampler.GetScore(const array1, array2: array of Double): double; |
| 84 |
var |
| 85 |
s, |
| 86 |
sum: Double; |
| 87 |
i: integer; |
| 88 |
begin |
| 89 |
sum := 0.0; |
| 90 |
for i := 0 to Pred(FreqSize) do |
| 91 |
begin |
| 92 |
s := array1[i] - array2[i]; |
| 93 |
sum := sum + s * s; |
| 94 |
end; |
| 95 |
Result := sqrt(sum) / (FreqSize*1.0); |
| 96 |
end; |
| 97 |
|
| 98 |
function TEUCSampler.GetSomeData: Boolean; |
| 99 |
begin |
| 100 |
Result := mTotal > 1; |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TEUCSampler.Reset; |
| 104 |
var |
| 105 |
i: integer; |
| 106 |
begin |
| 107 |
mTotal := 0; |
| 108 |
mThreshold := 200; |
| 109 |
mState := 0; |
| 110 |
for i := 0 to Pred(FreqSize) do |
| 111 |
begin |
| 112 |
mFirstByteCnt[i] := 0; |
| 113 |
mSecondByteCnt[i] := 0; |
| 114 |
end; |
| 115 |
end; |
| 116 |
|
| 117 |
function TEUCSampler.Sample(aIn: pAnsiChar; aLen: integer): Boolean; |
| 118 |
const |
| 119 |
MAX_LENGTH: integer = MaxInt;// $80000000; |
| 120 |
var |
| 121 |
i: integer; |
| 122 |
p: pAnsiChar; |
| 123 |
begin |
| 124 |
if (mState = 1) then |
| 125 |
begin |
| 126 |
Result := FALSE; |
| 127 |
exit; |
| 128 |
end; |
| 129 |
|
| 130 |
p := aIn; |
| 131 |
if (aLen + mTotal > MAX_LENGTH) then |
| 132 |
aLen := MAX_LENGTH - mTotal; |
| 133 |
|
| 134 |
i := 0; |
| 135 |
while (i < aLen) and (mState <> 1) do |
| 136 |
begin |
| 137 |
case mState of |
| 138 |
0: |
| 139 |
if (byte(p^) and $0080) > 0 then |
| 140 |
begin |
| 141 |
if (byte(p^) = $00ff) or ( byte(p^) < $00a1) then |
| 142 |
mState := 1 |
| 143 |
else |
| 144 |
begin |
| 145 |
Inc(mTotal); |
| 146 |
Inc(mFirstByteCnt[byte(p^) - $00a1]); |
| 147 |
mState := 2; |
| 148 |
end; |
| 149 |
end; |
| 150 |
1: |
| 151 |
begin |
| 152 |
end; |
| 153 |
2: |
| 154 |
if ( byte(p^) and $0080) > 0 then |
| 155 |
begin |
| 156 |
if(byte(p^) = $00ff) or (byte(p^) < $00a1) then |
| 157 |
mState := 1 |
| 158 |
else |
| 159 |
begin |
| 160 |
Inc(mTotal); |
| 161 |
Inc(mSecondByteCnt[byte(p^) - $00a1]); |
| 162 |
mState := 0; |
| 163 |
end; |
| 164 |
end |
| 165 |
else |
| 166 |
mState := 1; |
| 167 |
|
| 168 |
else |
| 169 |
mState := 1; |
| 170 |
end; |
| 171 |
Inc(i); |
| 172 |
Inc(p); |
| 173 |
|
| 174 |
end; |
| 175 |
Result := ( mState <> 1 ); |
| 176 |
end; |
| 177 |
|
| 178 |
end. |