00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 00002 /* ***** BEGIN LICENSE BLOCK ***** 00003 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 00004 * 00005 * The contents of this file are subject to the Mozilla Public License Version 00006 * 1.1 (the "License"); you may not use this file except in compliance with 00007 * the License. You may obtain a copy of the License at 00008 * http://www.mozilla.org/MPL/ 00009 * 00010 * Software distributed under the License is distributed on an "AS IS" basis, 00011 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 00012 * for the specific language governing rights and limitations under the 00013 * License. 00014 * 00015 * The Original Code is Mozilla Universal charset detector code. 00016 * 00017 * The Initial Developer of the Original Code is 00018 * Shy Shalom <shooshX@gmail.com> 00019 * Portions created by the Initial Developer are Copyright (C) 2005 00020 * the Initial Developer: All Rights Reserved. 00021 * 00022 * Contributor(s): 00023 * 00024 * Alternatively, the contents of this file may be used under the terms of 00025 * either the GNU General Public License Version 2 or later (the "GPL"), or 00026 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00027 * in which case the provisions of the GPL or the LGPL are applicable instead 00028 * of those above. If you wish to allow use of your version of this file only 00029 * under the terms of either the GPL or the LGPL, and not to allow others to 00030 * use your version of this file under the terms of the MPL, indicate your 00031 * decision by deleting the provisions above and replace them with the notice 00032 * and other provisions required by the GPL or the LGPL. If you do not delete 00033 * the provisions above, a recipient may use your version of this file under 00034 * the terms of any one of the MPL, the GPL or the LGPL. 00035 * 00036 * ***** END LICENSE BLOCK ***** */ 00037 00038 #ifndef nsHebrewProber_h__ 00039 #define nsHebrewProber_h__ 00040 00041 #include "nsSBCharSetProber.h" 00042 00043 // This prober doesn't actually recognize a language or a charset. 00044 // It is a helper prober for the use of the Hebrew model probers 00045 class nsHebrewProber: public nsCharSetProber 00046 { 00047 public: 00048 nsHebrewProber(void) :mLogicalProb(0), mVisualProb(0) { Reset(); } 00049 00050 virtual ~nsHebrewProber(void) {} 00051 virtual nsProbingState HandleData(const char* aBuf, PRUint32 aLen); 00052 virtual const char* GetCharSetName(); 00053 virtual void Reset(void); 00054 00055 virtual nsProbingState GetState(void); 00056 00057 virtual float GetConfidence(void) { return (float)0.0; } 00058 virtual void SetOpion() {}; 00059 00060 void SetModelProbers(nsCharSetProber *logicalPrb, nsCharSetProber *visualPrb) 00061 { mLogicalProb = logicalPrb; mVisualProb = visualPrb; } 00062 00063 #ifdef DEBUG_chardet 00064 virtual void DumpStatus(); 00065 #endif 00066 00067 protected: 00068 static PRBool isFinal(char c); 00069 static PRBool isNonFinal(char c); 00070 00071 PRInt32 mFinalCharLogicalScore, mFinalCharVisualScore; 00072 00073 // The two last characters seen in the previous buffer. 00074 char mPrev, mBeforePrev; 00075 00076 // These probers are owned by the group prober. 00077 nsCharSetProber *mLogicalProb, *mVisualProb; 00078 }; 00079 00080 /** 00081 * ** General ideas of the Hebrew charset recognition ** 00082 * 00083 * Four main charsets exist in Hebrew: 00084 * "ISO-8859-8" - Visual Hebrew 00085 * "windows-1255" - Logical Hebrew 00086 * "ISO-8859-8-I" - Logical Hebrew 00087 * "x-mac-hebrew" - ?? Logical Hebrew ?? 00088 * 00089 * Both "ISO" charsets use a completely identical set of code points, whereas 00090 * "windows-1255" and "x-mac-hebrew" are two different proper supersets of 00091 * these code points. windows-1255 defines additional characters in the range 00092 * 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific 00093 * diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6. 00094 * x-mac-hebrew defines similar additional code points but with a different 00095 * mapping. 00096 * 00097 * As far as an average Hebrew text with no diacritics is concerned, all four 00098 * charsets are identical with respect to code points. Meaning that for the 00099 * main Hebrew alphabet, all four map the same values to all 27 Hebrew letters 00100 * (including final letters). 00101 * 00102 * The dominant difference between these charsets is their directionality. 00103 * "Visual" directionality means that the text is ordered as if the renderer is 00104 * not aware of a BIDI rendering algorithm. The renderer sees the text and 00105 * draws it from left to right. The text itself when ordered naturally is read 00106 * backwards. A buffer of Visual Hebrew generally looks like so: 00107 * "[last word of first line spelled backwards] [whole line ordered backwards 00108 * and spelled backwards] [first word of first line spelled backwards] 00109 * [end of line] [last word of second line] ... etc' " 00110 * adding punctuation marks, numbers and English text to visual text is 00111 * naturally also "visual" and from left to right. 00112 * 00113 * "Logical" directionality means the text is ordered "naturally" according to 00114 * the order it is read. It is the responsibility of the renderer to display 00115 * the text from right to left. A BIDI algorithm is used to place general 00116 * punctuation marks, numbers and English text in the text. 00117 * 00118 * Texts in x-mac-hebrew are almost impossible to find on the Internet. From 00119 * what little evidence I could find, it seems that its general directionality 00120 * is Logical. 00121 * 00122 * To sum up all of the above, the Hebrew probing mechanism knows about two 00123 * charsets: 00124 * Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are 00125 * backwards while line order is natural. For charset recognition purposes 00126 * the line order is unimportant (In fact, for this implementation, even 00127 * word order is unimportant). 00128 * Logical Hebrew - "windows-1255" - normal, naturally ordered text. 00129 * 00130 * "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be 00131 * specifically identified. 00132 * "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew 00133 * that contain special punctuation marks or diacritics is displayed with 00134 * some unconverted characters showing as question marks. This problem might 00135 * be corrected using another model prober for x-mac-hebrew. Due to the fact 00136 * that x-mac-hebrew texts are so rare, writing another model prober isn't 00137 * worth the effort and performance hit. 00138 * 00139 * *** The Prober *** 00140 * 00141 * The prober is divided between two nsSBCharSetProbers and an nsHebrewProber, 00142 * all of which are managed, created, fed data, inquired and deleted by the 00143 * nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in 00144 * fact some kind of Hebrew, Logical or Visual. The final decision about which 00145 * one is it is made by the nsHebrewProber by combining final-letter scores 00146 * with the scores of the two nsSBCharSetProbers to produce a final answer. 00147 * 00148 * The nsSBCSGroupProber is responsible for stripping the original text of HTML 00149 * tags, English characters, numbers, low-ASCII punctuation characters, spaces 00150 * and new lines. It reduces any sequence of such characters to a single space. 00151 * The buffer fed to each prober in the SBCS group prober is pure text in 00152 * high-ASCII. 00153 * The two nsSBCharSetProbers (model probers) share the same language model: 00154 * Win1255Model. 00155 * The first nsSBCharSetProber uses the model normally as any other 00156 * nsSBCharSetProber does, to recognize windows-1255, upon which this model was 00157 * built. The second nsSBCharSetProber is told to make the pair-of-letter 00158 * lookup in the language model backwards. This in practice exactly simulates 00159 * a visual Hebrew model using the windows-1255 logical Hebrew model. 00160 * 00161 * The nsHebrewProber is not using any language model. All it does is look for 00162 * final-letter evidence suggesting the text is either logical Hebrew or visual 00163 * Hebrew. Disjointed from the model probers, the results of the nsHebrewProber 00164 * alone are meaningless. nsHebrewProber always returns 0.00 as confidence 00165 * since it never identifies a charset by itself. Instead, the pointer to the 00166 * nsHebrewProber is passed to the model probers as a helper "Name Prober". 00167 * When the Group prober receives a positive identification from any prober, 00168 * it asks for the name of the charset identified. If the prober queried is a 00169 * Hebrew model prober, the model prober forwards the call to the 00170 * nsHebrewProber to make the final decision. In the nsHebrewProber, the 00171 * decision is made according to the final-letters scores maintained and Both 00172 * model probers scores. The answer is returned in the form of the name of the 00173 * charset identified, either "windows-1255" or "ISO-8859-8". 00174 * 00175 */ 00176 #endif /* nsHebrewProber_h__ */