| 1 |
// +----------------------------------------------------------------------+ |
| 2 |
// | chsdet - Charset Detector Library | |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright (C) 2006, Nick Yakowlew http://chsdet.sourceforge.net | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Based on Mozilla sources http://www.mozilla.org/projects/intl/ | |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | This library is free software; you can redistribute it and/or modify | |
| 9 |
// | it under the terms of the GNU General Public License as published by | |
| 10 |
// | the Free Software Foundation; either version 2 of the License, or | |
| 11 |
// | (at your option) any later version. | |
| 12 |
// | This library is distributed in the hope that it will be useful | |
| 13 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 15 |
// | See the GNU Lesser General Public License for more details. | |
| 16 |
// | http://www.opensource.org/licenses/lgpl-license.php | |
| 17 |
// +----------------------------------------------------------------------+ |
| 18 |
// |
| 19 |
// $Id: MBUnicodeMultiProber.pas,v 1.3 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit MBUnicodeMultiProber; |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses |
| 26 |
{$I dbg.inc} |
| 27 |
nsCore, |
| 28 |
MultiModelProber; |
| 29 |
|
| 30 |
type |
| 31 |
TMBUnicodeMultiProber = class (TMultiModelProber) |
| 32 |
public |
| 33 |
constructor Create; reintroduce; |
| 34 |
destructor Destroy; override; |
| 35 |
function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override; |
| 36 |
// function GetConfidence: double; override; |
| 37 |
end; |
| 38 |
|
| 39 |
implementation |
| 40 |
uses |
| 41 |
SysUtils, |
| 42 |
nsCodingStateMachine; |
| 43 |
|
| 44 |
{$I '.\mbclass\UTF8LangModel.inc'} |
| 45 |
{$I '.\mbclass\UCS2BELangModel.inc'} |
| 46 |
{$I '.\mbclass\UCS2LELangModel.inc'} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
{ TMBUnicodeMultiProber } |
| 51 |
const |
| 52 |
NUM_OF_PROBERS = 3; |
| 53 |
{$IFDEF FPC}{$NOTES OFF}{$ENDIF} |
| 54 |
ONE_CHAR_PROB: float = 0.50; |
| 55 |
|
| 56 |
{$ifdef DEBUG_chardet} |
| 57 |
{$endif} |
| 58 |
|
| 59 |
constructor TMBUnicodeMultiProber.Create; |
| 60 |
begin |
| 61 |
inherited Create; |
| 62 |
|
| 63 |
AddCharsetModel(UTF8LangModel); |
| 64 |
// AddCharsetModel(UCS2BELangModel); |
| 65 |
// AddCharsetModel(UCS2LELangModel); |
| 66 |
end; |
| 67 |
|
| 68 |
destructor TMBUnicodeMultiProber.Destroy; |
| 69 |
begin |
| 70 |
inherited; |
| 71 |
end; |
| 72 |
|
| 73 |
function TMBUnicodeMultiProber.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; |
| 74 |
var |
| 75 |
i: integer; (*do filtering to reduce load to probers*) |
| 76 |
highbyteBuf: pAnsiChar; |
| 77 |
hptr: pAnsiChar; |
| 78 |
keepNext: Boolean; |
| 79 |
begin |
| 80 |
keepNext := TRUE; |
| 81 |
(*assume previous is not ascii, it will do no harm except add some noise*) |
| 82 |
highbyteBuf := AllocMem(aLen); |
| 83 |
try |
| 84 |
hptr:= highbyteBuf; |
| 85 |
if hptr = nil then |
| 86 |
begin |
| 87 |
Result := mState; |
| 88 |
exit; |
| 89 |
end; |
| 90 |
for i:=0 to Pred(aLen) do |
| 91 |
begin |
| 92 |
if (Byte(aBuf[i]) > $80) then |
| 93 |
begin |
| 94 |
hptr^ := aBuf[i]; |
| 95 |
inc(hptr); |
| 96 |
keepNext:= TRUE; |
| 97 |
end |
| 98 |
else |
| 99 |
begin |
| 100 |
(*if previous is highbyte, keep this even it is a ASCII*) |
| 101 |
if keepNext = TRUE then |
| 102 |
begin |
| 103 |
hptr^ := aBuf[i]; |
| 104 |
inc(hptr); |
| 105 |
keepNext:= FALSE; |
| 106 |
end; |
| 107 |
end; |
| 108 |
end; |
| 109 |
|
| 110 |
// inherited HandleData(highbyteBuf, hptr - highbyteBuf); |
| 111 |
inherited HandleData(aBuf, aLen); |
| 112 |
finally |
| 113 |
FreeMem(highbyteBuf, aLen); |
| 114 |
end; |
| 115 |
|
| 116 |
Result := mState; |
| 117 |
end; |
| 118 |
|
| 119 |
//function TMBUnicodeMultiProber.GetConfidence: double; |
| 120 |
//var |
| 121 |
// i: integer; |
| 122 |
// conf, |
| 123 |
// bestConf: double; |
| 124 |
//begin |
| 125 |
// mBestGuess := -1; |
| 126 |
// bestConf := SURE_NO; |
| 127 |
// for i := 0 to Pred(mCharsetsCount) do |
| 128 |
// begin |
| 129 |
// if (mSMState[i] = psFoundIt) or |
| 130 |
// (mSMState[i] = psNotMe) then |
| 131 |
// continue; |
| 132 |
// |
| 133 |
// if conf > bestConf then |
| 134 |
// begin |
| 135 |
// mBestGuess := i; |
| 136 |
// bestConf := conf; |
| 137 |
// end; |
| 138 |
// end; |
| 139 |
// Result := bestConf; |
| 140 |
// if mBestGuess > 0 then |
| 141 |
// mDetectedCharset := mCodingSM[mBestGuess].GetCharsetID |
| 142 |
// else |
| 143 |
// mDetectedCharset := UNKNOWN_CHARSET; |
| 144 |
//end; |
| 145 |
|
| 146 |
end. |