| 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: nsHebrewProber.pas,v 1.4 2013/04/23 19:47:10 ya_nick Exp $ |
| 20 |
|
| 21 |
unit nsHebrewProber; |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses |
| 26 |
nsCore, |
| 27 |
CustomDetector; |
| 28 |
(* This prober doesn't actually recognize a language or a charset.*) |
| 29 |
(* It is a helper prober for the use of the Hebrew model probers*) |
| 30 |
type |
| 31 |
TnsHebrewProber = class(TCustomDetector) |
| 32 |
protected |
| 33 |
mFinalCharLogicalScore: int32; |
| 34 |
mFinalCharVisualScore: int32; |
| 35 |
(* The two last characters seen in the previous buffer.*) |
| 36 |
mPrev: AnsiChar; |
| 37 |
mBeforePrev: AnsiChar; |
| 38 |
(* These probers are owned by the group prober.*) |
| 39 |
mLogicalProb: TCustomDetector; |
| 40 |
mVisualProb: TCustomDetector; |
| 41 |
|
| 42 |
public |
| 43 |
constructor Create; override; |
| 44 |
destructor Destroy; override; |
| 45 |
function HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; override; |
| 46 |
function GetDetectedCharset: eInternalCharsetID; override; |
| 47 |
procedure Reset; override; |
| 48 |
function GetState: eProbingState; override; |
| 49 |
procedure SetModelProbers(logicalPrb: TCustomDetector; visualPrb: TCustomDetector); |
| 50 |
{$ifdef DEBUG_chardet} |
| 51 |
procedure DumpStatus; override; |
| 52 |
{$endif} |
| 53 |
function isFinal(c: AnsiChar): Boolean; |
| 54 |
function isNonFinal(c: AnsiChar): Boolean; |
| 55 |
function GetConfidence: float; override; |
| 56 |
|
| 57 |
end; |
| 58 |
|
| 59 |
implementation |
| 60 |
(* windows-1255 / ISO-8859-8 code points of interest*) |
| 61 |
const |
| 62 |
FINAL_KAF = (#$ea); |
| 63 |
NORMAL_KAF = (#$eb); |
| 64 |
FINAL_MEM = (#$ed); |
| 65 |
NORMAL_MEM = (#$ee); |
| 66 |
FINAL_NUN = (#$ef); |
| 67 |
NORMAL_NUN = (#$f0); |
| 68 |
FINAL_PE = (#$f3); |
| 69 |
NORMAL_PE = (#$f4); |
| 70 |
FINAL_TSADI = (#$f5); |
| 71 |
// YaN - Not used |
| 72 |
// NORMAL_TSADI = (#$f6); |
| 73 |
(* Minimum Visual vs Logical final letter score difference.*) |
| 74 |
(* If the difference is below this, don't rely solely on the final letter score distance.*) |
| 75 |
MIN_FINAL_CHAR_DISTANCE = (5); |
| 76 |
(* Minimum Visual vs Logical model score difference.*) |
| 77 |
(* If the difference is below this, don't rely at all on the model score distance.*) |
| 78 |
MIN_MODEL_DISTANCE = (0.01); |
| 79 |
var |
| 80 |
VISUAL_HEBREW_CHARSET, |
| 81 |
LOGICAL_HEBREW_CHARSET: eInternalCharsetID; |
| 82 |
|
| 83 |
{ TnsHebrewProber } |
| 84 |
|
| 85 |
constructor TnsHebrewProber.Create; |
| 86 |
begin |
| 87 |
inherited Create; |
| 88 |
mLogicalProb := nil; |
| 89 |
mVisualProb := nil; |
| 90 |
|
| 91 |
VISUAL_HEBREW_CHARSET := ISO_8859_8_CHARSET; |
| 92 |
LOGICAL_HEBREW_CHARSET := WINDOWS_1255_CHARSET; |
| 93 |
|
| 94 |
Reset; |
| 95 |
end; |
| 96 |
|
| 97 |
destructor TnsHebrewProber.Destroy; |
| 98 |
begin |
| 99 |
|
| 100 |
inherited; |
| 101 |
end; |
| 102 |
|
| 103 |
{$ifdef DEBUG_chardet} |
| 104 |
procedure TnsHebrewProber.DumpStatus; |
| 105 |
begin |
| 106 |
printf(' HEB: %d - %d [Logical-Visual score]r'#13#10'',mFinalCharLogicalScore,mFinalCharVisualScore); |
| 107 |
end; |
| 108 |
{$endif} |
| 109 |
|
| 110 |
(* Make the decision: is it Logical or Visual?*) |
| 111 |
function TnsHebrewProber.GetDetectedCharset: eInternalCharsetID; |
| 112 |
var |
| 113 |
finalsub: int32; |
| 114 |
modelsub: float; |
| 115 |
begin |
| 116 |
(* If the final letter score distance is dominant enough, rely on it.*) |
| 117 |
finalsub := mFinalCharLogicalScore-mFinalCharVisualScore; |
| 118 |
if finalsub >= MIN_FINAL_CHAR_DISTANCE then |
| 119 |
begin |
| 120 |
Result := LOGICAL_HEBREW_CHARSET; |
| 121 |
exit; |
| 122 |
end; |
| 123 |
if finalsub <= -(MIN_FINAL_CHAR_DISTANCE) then |
| 124 |
begin |
| 125 |
Result:= VISUAL_HEBREW_CHARSET; |
| 126 |
exit; |
| 127 |
end; |
| 128 |
(* It's not dominant enough, try to rely on the model scores instead.*) |
| 129 |
modelsub := mLogicalProb.GetConfidence - mVisualProb.GetConfidence; |
| 130 |
if modelsub > MIN_MODEL_DISTANCE then |
| 131 |
begin |
| 132 |
Result := LOGICAL_HEBREW_CHARSET; |
| 133 |
exit; |
| 134 |
end; |
| 135 |
if modelsub < -(MIN_MODEL_DISTANCE) then |
| 136 |
begin |
| 137 |
Result := VISUAL_HEBREW_CHARSET; |
| 138 |
exit; |
| 139 |
end; |
| 140 |
(* Still no good, back to final letter distance, maybe it'll save the day.*) |
| 141 |
if finalsub < 0 then |
| 142 |
begin |
| 143 |
Result := VISUAL_HEBREW_CHARSET; |
| 144 |
exit; |
| 145 |
end; |
| 146 |
(* (finalsub > 0 - Logical) or (don't know what to do) default to Logical.*) |
| 147 |
Result:= LOGICAL_HEBREW_CHARSET; |
| 148 |
end; |
| 149 |
|
| 150 |
function TnsHebrewProber.GetConfidence: float; |
| 151 |
begin |
| 152 |
Result := 0.0; |
| 153 |
end; |
| 154 |
|
| 155 |
function TnsHebrewProber.GetState: eProbingState; |
| 156 |
begin |
| 157 |
(* Remain active as long as any of the model probers are active.*) |
| 158 |
if (mLogicalProb.GetState = psNotMe) and |
| 159 |
(mVisualProb.GetState = psNotMe) then |
| 160 |
begin |
| 161 |
Result := psNotMe; |
| 162 |
exit; |
| 163 |
end; |
| 164 |
Result := psDetecting; |
| 165 |
end; |
| 166 |
|
| 167 |
function TnsHebrewProber.HandleData(aBuf: pAnsiChar; aLen: integer): eProbingState; |
| 168 |
(* |
| 169 |
* Final letter analysis for logical-visual decision. |
| 170 |
* Look for evidence that the received buffer is either logical Hebrew or |
| 171 |
* visual Hebrew. |
| 172 |
* The following cases are checked: |
| 173 |
* 1) A word longer than 1 letter, ending with a final letter. This is an |
| 174 |
* indication that the text is laid out "naturally" since the final letter |
| 175 |
* really appears at the end. +1 for logical score. |
| 176 |
* 2) A word longer than 1 letter, ending with a Non-Final letter. In normal |
| 177 |
* Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi, should not end with |
| 178 |
* the Non-Final form of that letter. Exceptions to this rule are mentioned |
| 179 |
* above in isNonFinal(). This is an indication that the text is laid out |
| 180 |
* backwards. +1 for visual score |
| 181 |
* 3) A word longer than 1 letter, starting with a final letter. Final letters |
| 182 |
* should not appear at the beginning of a word. This is an indication that |
| 183 |
* the text is laid out backwards. +1 for visual score. |
| 184 |
* |
| 185 |
* The visual score and logical score are accumulated throughout the text and |
| 186 |
* are finally checked against each other in GetCharSetName(). |
| 187 |
* No checking for final letters in the middle of words is done since that case |
| 188 |
* is not an indication for either Logical or Visual text. |
| 189 |
* |
| 190 |
* The input buffer should not contain any white spaces that are not (' ') |
| 191 |
* or any low-ascii punctuation marks. |
| 192 |
*) |
| 193 |
var |
| 194 |
curPtr: pAnsiChar; |
| 195 |
endPtr: pAnsiChar; |
| 196 |
cur: AnsiChar; |
| 197 |
begin |
| 198 |
(* check prober enabled*) |
| 199 |
inherited HandleData(aBuf, aLen); |
| 200 |
|
| 201 |
(* Both model probers say it's not them. No reason to continue.*) |
| 202 |
if GetState = psNotMe then |
| 203 |
begin |
| 204 |
Result:= psNotMe; |
| 205 |
exit; |
| 206 |
end; |
| 207 |
|
| 208 |
endPtr := aBuf + aLen; |
| 209 |
curPtr := aBuf; |
| 210 |
while curPtr < endPtr do |
| 211 |
begin |
| 212 |
cur := curPtr^; |
| 213 |
if cur = ' ' then |
| 214 |
begin |
| 215 |
(* We stand on a space - a word just ended*) |
| 216 |
if mBeforePrev <> ' ' then |
| 217 |
begin |
| 218 |
(* *(curPtr-2) was not a space so prev is not a 1 letter word*) |
| 219 |
if isFinal(mPrev) then |
| 220 |
inc(mFinalCharLogicalScore) |
| 221 |
else |
| 222 |
(* case (1) [-2:not space][-1:final letter][cur:space]*) |
| 223 |
if isNonFinal(mPrev) then |
| 224 |
inc(mFinalCharVisualScore); |
| 225 |
(* case (2) [-2:not space][-1:Non-Final letter][cur:space]*) |
| 226 |
end; |
| 227 |
end |
| 228 |
else |
| 229 |
begin |
| 230 |
(* Not standing on a space*) |
| 231 |
if (mBeforePrev = ' ') and |
| 232 |
isFinal(mPrev) and |
| 233 |
(cur <> ' ') then |
| 234 |
inc(mFinalCharVisualScore); |
| 235 |
(* case (3) [-2:space][-1:final letter][cur:not space]*) |
| 236 |
end; |
| 237 |
mBeforePrev := mPrev; |
| 238 |
mPrev := cur; |
| 239 |
|
| 240 |
inc(curPtr); |
| 241 |
end; |
| 242 |
(* Forever detecting, till the end or until both model probers return psNotMe (handled above).*) |
| 243 |
Result := psDetecting; |
| 244 |
end; |
| 245 |
|
| 246 |
function TnsHebrewProber.isFinal(c: AnsiChar): Boolean; |
| 247 |
begin |
| 248 |
Result := ((c=FINAL_KAF))or((c=FINAL_MEM))or((c=FINAL_NUN))or((c=FINAL_PE))or((c=FINAL_TSADI)); |
| 249 |
end; |
| 250 |
|
| 251 |
function TnsHebrewProber.isNonFinal(c: AnsiChar): Boolean; |
| 252 |
begin |
| 253 |
Result:= ((c=NORMAL_KAF))or((c=NORMAL_MEM))or((c=NORMAL_NUN))or((c=NORMAL_PE)); |
| 254 |
(* The normal Tsadi is not a good Non-Final letter due to words like *) |
| 255 |
(* 'lechotet' (to chat) containing an apostrophe after the tsadi. This *) |
| 256 |
(* apostrophe is converted to a space in FilterWithoutEnglishLetters causing *) |
| 257 |
(* the Non-Final tsadi to appear at an end of a word even though this is not *) |
| 258 |
(* the case in the original text.*) |
| 259 |
(* The letters Pe and Kaf rarely display a related behavior of not being a *) |
| 260 |
(* good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak' for *) |
| 261 |
(* example legally end with a Non-Final Pe or Kaf. However, the benefit of *) |
| 262 |
(* these letters as Non-Final letters outweighs the damage since these words *) |
| 263 |
(* are quite rare.*) |
| 264 |
end; |
| 265 |
|
| 266 |
procedure TnsHebrewProber.Reset; |
| 267 |
begin |
| 268 |
mFinalCharLogicalScore := 0; |
| 269 |
mFinalCharVisualScore := 0; |
| 270 |
mPrev := ' '; |
| 271 |
mBeforePrev := ' '; |
| 272 |
(* mPrev and mBeforePrev are initialized to space in order to simulate a word *) |
| 273 |
(* delimiter at the beginning of the data*) |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TnsHebrewProber.SetModelProbers(logicalPrb, visualPrb: TCustomDetector); |
| 277 |
begin |
| 278 |
mLogicalProb := logicalPrb; |
| 279 |
mVisualProb := visualPrb; |
| 280 |
end; |
| 281 |
|
| 282 |
(** |
| 283 |
* ** General ideas of the Hebrew charset recognition ** |
| 284 |
* |
| 285 |
* Four main charsets exist in Hebrew: |
| 286 |
* "ISO-8859-8" - Visual Hebrew |
| 287 |
* "windows-1255" - Logical Hebrew |
| 288 |
* "ISO-8859-8-I" - Logical Hebrew |
| 289 |
* "x-mac-hebrew" - ?? Logical Hebrew ?? |
| 290 |
* |
| 291 |
* Both "ISO" charsets use a completely identical set of code points, whereas |
| 292 |
* "windows-1255" and "x-mac-hebrew" are two different proper supersets of |
| 293 |
* these code points. windows-1255 defines additional characters in the range |
| 294 |
* 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific |
| 295 |
* diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6. |
| 296 |
* x-mac-hebrew defines similar additional code points but with a different |
| 297 |
* mapping. |
| 298 |
* |
| 299 |
* As as an average Hebrew text with no diacritics is concerned, all four |
| 300 |
* charsets are identical with respect to code points. Meaning that for the |
| 301 |
* main Hebrew alphabet, all four map the same values to all 27 Hebrew letters |
| 302 |
* (including final letters). |
| 303 |
* |
| 304 |
* The dominant difference between these charsets is their directionality. |
| 305 |
* "Visual" directionality means that the text is ordered as if the renderer is |
| 306 |
* not aware of a BIDI rendering algorithm. The renderer sees the text and |
| 307 |
* draws it from left to right. The text itself when ordered naturally is read |
| 308 |
* backwards. A buffer of Visual Hebrew generally looks like so: |
| 309 |
* "[last word of first line spelled backwards] [whole line ordered backwards |
| 310 |
* and spelled backwards] [first word of first line spelled backwards] |
| 311 |
* [end of line] [last word of second line] ... etc' " |
| 312 |
* adding punctuation marks, numbers and English text to visual text is |
| 313 |
* naturally also "visual" and from left to right. |
| 314 |
* |
| 315 |
* "Logical" directionality means the text is ordered "naturally" according to |
| 316 |
* the order it is read. It is the responsibility of the renderer to display |
| 317 |
* the text from right to left. A BIDI algorithm is used to place general |
| 318 |
* punctuation marks, numbers and English text in the text. |
| 319 |
* |
| 320 |
* Texts in x-mac-hebrew are almost impossible to find on the Internet. From |
| 321 |
* what little evidence I could find, it seems that its general directionality |
| 322 |
* is Logical. |
| 323 |
* |
| 324 |
* To sum up all of the above, the Hebrew probing mechanism knows about two |
| 325 |
* charsets: |
| 326 |
* Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are |
| 327 |
* backwards while line order is natural. For charset recognition purposes |
| 328 |
* the line order is unimportant (In fact, for this implementation, even |
| 329 |
* word order is unimportant). |
| 330 |
* Logical Hebrew - "windows-1255" - normal, naturally ordered text. |
| 331 |
* |
| 332 |
* "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be |
| 333 |
* specifically identified. |
| 334 |
* "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew |
| 335 |
* that contain special punctuation marks or diacritics is displayed with |
| 336 |
* some unconverted characters showing as question marks. This problem might |
| 337 |
* be corrected using another model prober for x-mac-hebrew. Due to the fact |
| 338 |
* that x-mac-hebrew texts are so rare, writing another model prober isn't |
| 339 |
* worth the effort and performance hit. |
| 340 |
* |
| 341 |
* *** The Prober *** |
| 342 |
* |
| 343 |
* The prober is divided between two nsSBCharSetProbers and an nsHebrewProber, |
| 344 |
* all of which are managed, created, fed data, inquired and deleted by the |
| 345 |
* nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in |
| 346 |
* fact some kind of Hebrew, Logical or Visual. The final decision about which |
| 347 |
* one is it is made by the nsHebrewProber by combining final-letter scores |
| 348 |
* with the scores of the two nsSBCharSetProbers to produce a final answer. |
| 349 |
* |
| 350 |
* The nsSBCSGroupProber is responsible for stripping the original text of HTML |
| 351 |
* tags, English characters, numbers, low-ASCII punctuation characters, spaces |
| 352 |
* and new lines. It reduces any sequence of such characters to a single space. |
| 353 |
* The buffer fed to each prober in the SBCS group prober is pure text in |
| 354 |
* high-ASCII. |
| 355 |
* The two nsSBCharSetProbers (model probers) share the same language model: |
| 356 |
* Win1255Model. |
| 357 |
* The first nsSBCharSetProber uses the model normally as any other |
| 358 |
* nsSBCharSetProber does, to recognize windows-1255, upon which this model was |
| 359 |
* built. The second nsSBCharSetProber is told to make the pair-of-letter |
| 360 |
* lookup in the language model backwards. This in practice exactly simulates |
| 361 |
* a visual Hebrew model using the windows-1255 logical Hebrew model. |
| 362 |
* |
| 363 |
* The nsHebrewProber is not using any language model. All it does is look for |
| 364 |
* final-letter evidence suggesting the text is either logical Hebrew or visual |
| 365 |
* Hebrew. Disjointed from the model probers, the results of the nsHebrewProber |
| 366 |
* alone are meaningless. nsHebrewProber always returns 0.00 as confidence |
| 367 |
* since it never identifies a charset by itself. Instead, the pointer to the |
| 368 |
* nsHebrewProber is passed to the model probers as a helper "Name Prober". |
| 369 |
* When the Group prober receives a positive identification from any prober, |
| 370 |
* it asks for the name of the charset identified. If the prober queried is a |
| 371 |
* Hebrew model prober, the model prober forwards the call to the |
| 372 |
* nsHebrewProber to make the final decision. In the nsHebrewProber, the |
| 373 |
* decision is made according to the final-letters scores maintained and Both |
| 374 |
* model probers scores. The answer is returned in the form of the name of the |
| 375 |
* charset identified, either "windows-1255" or "ISO-8859-8". |
| 376 |
* |
| 377 |
*) |
| 378 |
end. |