File indexing completed on 2024-12-22 04:33:41

0001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
0002 /* ***** BEGIN LICENSE BLOCK *****
0003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
0004  *
0005  * The contents of this file are subject to the Mozilla Public License Version
0006  * 1.1 (the "License"); you may not use this file except in compliance with
0007  * the License. You may obtain a copy of the License at
0008  * http://www.mozilla.org/MPL/
0009  *
0010  * Software distributed under the License is distributed on an "AS IS" basis,
0011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
0012  * for the specific language governing rights and limitations under the
0013  * License.
0014  *
0015  * The Original Code is Mozilla Universal charset detector code.
0016  *
0017  * The Initial Developer of the Original Code is
0018  *          Shy Shalom <shooshX@gmail.com>
0019  * Portions created by the Initial Developer are Copyright (C) 2005
0020  * the Initial Developer: All Rights Reserved.
0021  *
0022  * Contributor(s):
0023  *
0024  * Alternatively, the contents of this file may be used under the terms of
0025  * either the GNU General Public License Version 2 or later (the "GPL"), or
0026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
0027  * in which case the provisions of the GPL or the LGPL are applicable instead
0028  * of those above. If you wish to allow use of your version of this file only
0029  * under the terms of either the GPL or the LGPL, and not to allow others to
0030  * use your version of this file under the terms of the MPL, indicate your
0031  * decision by deleting the provisions above and replace them with the notice
0032  * and other provisions required by the GPL or the LGPL. If you do not delete
0033  * the provisions above, a recipient may use your version of this file under
0034  * the terms of any one of the MPL, the GPL or the LGPL.
0035  *
0036  * ***** END LICENSE BLOCK ***** */
0037 
0038 #ifndef nsHebrewProber_h__
0039 #define nsHebrewProber_h__
0040 
0041 #include "nsSBCharSetProber.h"
0042 
0043 // This prober doesn't actually recognize a language or a charset.
0044 // It is a helper prober for the use of the Hebrew model probers
0045 class nsHebrewProber: public nsCharSetProber
0046 {
0047 public:
0048   nsHebrewProber(void) :mLogicalProb(0), mVisualProb(0) { Reset(); }
0049 
0050   virtual ~nsHebrewProber(void) {}
0051   virtual nsProbingState HandleData(const char* aBuf, PRUint32 aLen);
0052   virtual const char* GetCharSetName();
0053   virtual void Reset(void);
0054 
0055   virtual nsProbingState GetState(void);
0056 
0057   virtual float     GetConfidence(void) { return (float)0.0; }
0058   virtual void      SetOpion() {}
0059 
0060   void SetModelProbers(nsCharSetProber *logicalPrb, nsCharSetProber *visualPrb) 
0061   { mLogicalProb = logicalPrb; mVisualProb = visualPrb; }
0062 
0063 #ifdef DEBUG_chardet
0064   virtual void  DumpStatus();
0065 #endif
0066 
0067 protected:
0068   static PRBool isFinal(char c);
0069   static PRBool isNonFinal(char c);
0070 
0071   PRInt32 mFinalCharLogicalScore, mFinalCharVisualScore;
0072 
0073   // The two last characters seen in the previous buffer.
0074   char mPrev, mBeforePrev;
0075 
0076   // These probers are owned by the group prober.
0077   nsCharSetProber *mLogicalProb, *mVisualProb;
0078 };
0079 
0080 /**
0081  * ** General ideas of the Hebrew charset recognition **
0082  *
0083  * Four main charsets exist in Hebrew:
0084  * "ISO-8859-8" - Visual Hebrew
0085  * "windows-1255" - Logical Hebrew 
0086  * "ISO-8859-8-I" - Logical Hebrew
0087  * "x-mac-hebrew" - ?? Logical Hebrew ??
0088  *
0089  * Both "ISO" charsets use a completely identical set of code points, whereas
0090  * "windows-1255" and "x-mac-hebrew" are two different proper supersets of 
0091  * these code points. windows-1255 defines additional characters in the range
0092  * 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific 
0093  * diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.
0094  * x-mac-hebrew defines similar additional code points but with a different 
0095  * mapping.
0096  *
0097  * As far as an average Hebrew text with no diacritics is concerned, all four 
0098  * charsets are identical with respect to code points. Meaning that for the 
0099  * main Hebrew alphabet, all four map the same values to all 27 Hebrew letters 
0100  * (including final letters).
0101  *
0102  * The dominant difference between these charsets is their directionality.
0103  * "Visual" directionality means that the text is ordered as if the renderer is
0104  * not aware of a BIDI rendering algorithm. The renderer sees the text and 
0105  * draws it from left to right. The text itself when ordered naturally is read 
0106  * backwards. A buffer of Visual Hebrew generally looks like so:
0107  * "[last word of first line spelled backwards] [whole line ordered backwards
0108  * and spelled backwards] [first word of first line spelled backwards] 
0109  * [end of line] [last word of second line] ... etc' "
0110  * adding punctuation marks, numbers and English text to visual text is
0111  * naturally also "visual" and from left to right.
0112  * 
0113  * "Logical" directionality means the text is ordered "naturally" according to
0114  * the order it is read. It is the responsibility of the renderer to display 
0115  * the text from right to left. A BIDI algorithm is used to place general 
0116  * punctuation marks, numbers and English text in the text.
0117  *
0118  * Texts in x-mac-hebrew are almost impossible to find on the Internet. From 
0119  * what little evidence I could find, it seems that its general directionality
0120  * is Logical.
0121  *
0122  * To sum up all of the above, the Hebrew probing mechanism knows about two
0123  * charsets:
0124  * Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are
0125  *    backwards while line order is natural. For charset recognition purposes
0126  *    the line order is unimportant (In fact, for this implementation, even 
0127  *    word order is unimportant).
0128  * Logical Hebrew - "windows-1255" - normal, naturally ordered text.
0129  *
0130  * "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be 
0131  *    specifically identified.
0132  * "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew
0133  *    that contain special punctuation marks or diacritics is displayed with
0134  *    some unconverted characters showing as question marks. This problem might
0135  *    be corrected using another model prober for x-mac-hebrew. Due to the fact
0136  *    that x-mac-hebrew texts are so rare, writing another model prober isn't 
0137  *    worth the effort and performance hit.
0138  *
0139  * *** The Prober ***
0140  *
0141  * The prober is divided between two nsSBCharSetProbers and an nsHebrewProber,
0142  * all of which are managed, created, fed data, inquired and deleted by the
0143  * nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in
0144  * fact some kind of Hebrew, Logical or Visual. The final decision about which
0145  * one is it is made by the nsHebrewProber by combining final-letter scores
0146  * with the scores of the two nsSBCharSetProbers to produce a final answer.
0147  *
0148  * The nsSBCSGroupProber is responsible for stripping the original text of HTML
0149  * tags, English characters, numbers, low-ASCII punctuation characters, spaces
0150  * and new lines. It reduces any sequence of such characters to a single space.
0151  * The buffer fed to each prober in the SBCS group prober is pure text in
0152  * high-ASCII.
0153  * The two nsSBCharSetProbers (model probers) share the same language model:
0154  * Win1255Model.
0155  * The first nsSBCharSetProber uses the model normally as any other
0156  * nsSBCharSetProber does, to recognize windows-1255, upon which this model was
0157  * built. The second nsSBCharSetProber is told to make the pair-of-letter
0158  * lookup in the language model backwards. This in practice exactly simulates
0159  * a visual Hebrew model using the windows-1255 logical Hebrew model.
0160  *
0161  * The nsHebrewProber is not using any language model. All it does is look for
0162  * final-letter evidence suggesting the text is either logical Hebrew or visual
0163  * Hebrew. Disjointed from the model probers, the results of the nsHebrewProber
0164  * alone are meaningless. nsHebrewProber always returns 0.00 as confidence
0165  * since it never identifies a charset by itself. Instead, the pointer to the
0166  * nsHebrewProber is passed to the model probers as a helper "Name Prober".
0167  * When the Group prober receives a positive identification from any prober,
0168  * it asks for the name of the charset identified. If the prober queried is a
0169  * Hebrew model prober, the model prober forwards the call to the
0170  * nsHebrewProber to make the final decision. In the nsHebrewProber, the
0171  * decision is made according to the final-letters scores maintained and Both
0172  * model probers scores. The answer is returned in the form of the name of the
0173  * charset identified, either "windows-1255" or "ISO-8859-8".
0174  *
0175  */
0176 #endif /* nsHebrewProber_h__ */