CharDistribution.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
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 CharDistribution_h__
00039 #define CharDistribution_h__
00040 
00041 #include "nscore.h"
00042 
00043 #define ENOUGH_DATA_THRESHOLD 1024
00044  
00045 class CharDistributionAnalysis
00046 {
00047 public:
00048   CharDistributionAnalysis() {Reset();}
00049   virtual ~CharDistributionAnalysis() {}
00050 
00051   //feed a block of data and do distribution analysis
00052   void HandleData(const char* aBuf, PRUint32 aLen) {};
00053   
00054   //Feed a character with known length
00055   void HandleOneChar(const char* aStr, PRUint32 aCharLen)
00056   {
00057     PRInt32 order;
00058 
00059     //we only care about 2-bytes character in our distribution analysis
00060     order = (aCharLen == 2) ? GetOrder(aStr) : -1;
00061 
00062     if (order >= 0)
00063     {
00064       mTotalChars++;
00065       //order is valid
00066       if ((PRUint32)order < mTableSize)
00067       {
00068         if (512 > mCharToFreqOrder[order])
00069           mFreqChars++;
00070       }
00071     }
00072   };
00073 
00074   //return confidence base on existing data
00075   float GetConfidence();
00076 
00077   //Reset analyser, clear any state 
00078   void      Reset(void) 
00079   {
00080     mDone = PR_FALSE;
00081     mTotalChars = 0;
00082     mFreqChars = 0;
00083   };
00084 
00085   //This function is for future extension. Caller can use this function to control
00086   //analyser's behavior
00087   void      SetOpion(){};
00088 
00089   //It is not necessary to receive all data to draw conclusion. For charset detection,
00090   // certain amount of data is enough
00091   PRBool GotEnoughData() {return mTotalChars > ENOUGH_DATA_THRESHOLD;};
00092 
00093 protected:
00094   //we do not handle character base on its original encoding string, but 
00095   //convert this encoding string to a number, here called order.
00096   //This allow multiple encoding of a language to share one frequency table 
00097   virtual PRInt32 GetOrder(const char* str) {return -1;};
00098   
00099   //If this flag is set to PR_TRUE, detection is done and conclusion has been made
00100   PRBool   mDone;
00101 
00102   //The number of characters whose frequency order is less than 512
00103   PRUint32 mFreqChars;
00104 
00105   //Total character encounted.
00106   PRUint32 mTotalChars;
00107 
00108   //Mapping table to get frequency order from char order (get from GetOrder())
00109   const PRInt16  *mCharToFreqOrder;
00110 
00111   //Size of above table
00112   PRUint32 mTableSize;
00113 
00114   //This is a constant value varies from language to language, it is used in 
00115   //calculating confidence. See my paper for further detail.
00116   float    mTypicalDistributionRatio;
00117 };
00118 
00119 
00120 class EUCTWDistributionAnalysis: public CharDistributionAnalysis
00121 {
00122 public:
00123   EUCTWDistributionAnalysis();
00124   virtual ~EUCTWDistributionAnalysis() {}
00125 protected:
00126 
00127   //for euc-TW encoding, we are interested 
00128   //  first  byte range: 0xc4 -- 0xfe
00129   //  second byte range: 0xa1 -- 0xfe
00130   //no validation needed here. State machine has done that
00131   PRInt32 GetOrder(const char* str) 
00132   { if ((unsigned char)*str >= (unsigned char)0xc4)  
00133       return 94*((unsigned char)str[0]-(unsigned char)0xc4) + (unsigned char)str[1] - (unsigned char)0xa1;
00134     else
00135       return -1;
00136   };
00137 };
00138 
00139 
00140 class EUCKRDistributionAnalysis : public CharDistributionAnalysis
00141 {
00142 public:
00143   EUCKRDistributionAnalysis();
00144   virtual ~EUCKRDistributionAnalysis() {}
00145 protected:
00146   //for euc-KR encoding, we are interested 
00147   //  first  byte range: 0xb0 -- 0xfe
00148   //  second byte range: 0xa1 -- 0xfe
00149   //no validation needed here. State machine has done that
00150   PRInt32 GetOrder(const char* str) 
00151   { if ((unsigned char)*str >= (unsigned char)0xb0)  
00152       return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
00153     else
00154       return -1;
00155   };
00156 };
00157 
00158 class GB2312DistributionAnalysis : public CharDistributionAnalysis
00159 {
00160 public:
00161   GB2312DistributionAnalysis();
00162 protected:
00163   //for GB2312 encoding, we are interested 
00164   //  first  byte range: 0xb0 -- 0xfe
00165   //  second byte range: 0xa1 -- 0xfe
00166   //no validation needed here. State machine has done that
00167   PRInt32 GetOrder(const char* str) 
00168   { if ((unsigned char)*str >= (unsigned char)0xb0 && (unsigned char)str[1] >= (unsigned char)0xa1)  
00169       return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
00170     else
00171       return -1;
00172   };
00173 };
00174 
00175 
00176 class Big5DistributionAnalysis : public CharDistributionAnalysis
00177 {
00178 public:
00179   Big5DistributionAnalysis();
00180 protected:
00181   //for big5 encoding, we are interested 
00182   //  first  byte range: 0xa4 -- 0xfe
00183   //  second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
00184   //no validation needed here. State machine has done that
00185   PRInt32 GetOrder(const char* str) 
00186   { if ((unsigned char)*str >= (unsigned char)0xa4)  
00187       if ((unsigned char)str[1] >= (unsigned char)0xa1)
00188         return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0xa1 +63;
00189       else
00190         return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0x40;
00191     else
00192       return -1;
00193   };
00194 };
00195 
00196 class SJISDistributionAnalysis : public CharDistributionAnalysis
00197 {
00198 public:
00199   SJISDistributionAnalysis();
00200 protected:
00201   //for sjis encoding, we are interested 
00202   //  first  byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
00203   //  second byte range: 0x40 -- 0x7e,  0x81 -- oxfe
00204   //no validation needed here. State machine has done that
00205   PRInt32 GetOrder(const char* str) 
00206   { 
00207     PRInt32 order;
00208     if ((unsigned char)*str >= (unsigned char)0x81 && (unsigned char)*str <= (unsigned char)0x9f)  
00209       order = 188 * ((unsigned char)str[0]-(unsigned char)0x81);
00210     else if ((unsigned char)*str >= (unsigned char)0xe0 && (unsigned char)*str <= (unsigned char)0xef)  
00211       order = 188 * ((unsigned char)str[0]-(unsigned char)0xe0 + 31);
00212     else
00213       return -1;
00214     order += (unsigned char)*(str+1) - 0x40;
00215     if ((unsigned char)str[1] > (unsigned char)0x7f)
00216       order--;
00217     return order;
00218   };
00219 };
00220 
00221 class EUCJPDistributionAnalysis : public CharDistributionAnalysis
00222 {
00223 public:
00224   EUCJPDistributionAnalysis();
00225 protected:
00226   //for euc-JP encoding, we are interested 
00227   //  first  byte range: 0xa0 -- 0xfe
00228   //  second byte range: 0xa1 -- 0xfe
00229   //no validation needed here. State machine has done that
00230   PRInt32 GetOrder(const char* str) 
00231   { if ((unsigned char)*str >= (unsigned char)0xa0)  
00232       return 94*((unsigned char)str[0]-(unsigned char)0xa1) + (unsigned char)str[1] - (unsigned char)0xa1;
00233     else
00234       return -1;
00235   };
00236 };
00237 
00238 #endif //CharDistribution_h__
00239 
Generated by  doxygen 1.6.2-20100208