Back to index

lightning-sunbird  0.9+nobinonly
nsSBCSGroupProber.cpp
Go to the documentation of this file.
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  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *          Shy Shalom <shooshX@gmail.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include "prmem.h"
00041 
00042 #include "nsSBCharSetProber.h"
00043 #include "nsSBCSGroupProber.h"
00044 
00045 #include "nsHebrewProber.h"
00046 
00047 nsSBCSGroupProber::nsSBCSGroupProber()
00048 {
00049   mProbers[0] = new nsSingleByteCharSetProber(&Win1251Model);
00050   mProbers[1] = new nsSingleByteCharSetProber(&Koi8rModel);
00051   mProbers[2] = new nsSingleByteCharSetProber(&Latin5Model);
00052   mProbers[3] = new nsSingleByteCharSetProber(&MacCyrillicModel);
00053   mProbers[4] = new nsSingleByteCharSetProber(&Ibm866Model);
00054   mProbers[5] = new nsSingleByteCharSetProber(&Ibm855Model);
00055   mProbers[6] = new nsSingleByteCharSetProber(&Latin7Model);
00056   mProbers[7] = new nsSingleByteCharSetProber(&Win1253Model);
00057   mProbers[8] = new nsSingleByteCharSetProber(&Latin5BulgarianModel);
00058   mProbers[9] = new nsSingleByteCharSetProber(&Win1251BulgarianModel);
00059 
00060   nsHebrewProber *hebprober = new nsHebrewProber();
00061   // Notice: Any change in these indexes - 10,11,12 must be reflected
00062   // in the code below as well.
00063   mProbers[10] = hebprober;
00064   mProbers[11] = new nsSingleByteCharSetProber(&Win1255Model, PR_FALSE, hebprober); // Logical Hebrew
00065   mProbers[12] = new nsSingleByteCharSetProber(&Win1255Model, PR_TRUE, hebprober); // Visual Hebrew
00066   // Tell the Hebrew prober about the logical and visual probers
00067   if (mProbers[10] && mProbers[11] && mProbers[12]) // all are not null
00068   {
00069     hebprober->SetModelProbers(mProbers[11], mProbers[12]);
00070   }
00071   else // One or more is null. avoid any Hebrew probing, null them all
00072   {
00073     for (PRUint32 i = 10; i <= 12; ++i)
00074     { 
00075       delete mProbers[i]; 
00076       mProbers[i] = 0; 
00077     }
00078   }
00079 
00080   // disable latin2 before latin1 is available, otherwise all latin1 
00081   // will be detected as latin2 because of their similarity.
00082   //mProbers[10] = new nsSingleByteCharSetProber(&Latin2HungarianModel);
00083   //mProbers[11] = new nsSingleByteCharSetProber(&Win1250HungarianModel);
00084 
00085   Reset();
00086 }
00087 
00088 nsSBCSGroupProber::~nsSBCSGroupProber()
00089 {
00090   for (PRUint32 i = 0; i < NUM_OF_SBCS_PROBERS; i++)
00091   {
00092     delete mProbers[i];
00093   }
00094 }
00095 
00096 
00097 const char* nsSBCSGroupProber::GetCharSetName()
00098 {
00099   //if we have no answer yet
00100   if (mBestGuess == -1)
00101   {
00102     GetConfidence();
00103     //no charset seems positive
00104     if (mBestGuess == -1)
00105       //we will use default.
00106       mBestGuess = 0;
00107   }
00108   return mProbers[mBestGuess]->GetCharSetName();
00109 }
00110 
00111 void  nsSBCSGroupProber::Reset(void)
00112 {
00113   mActiveNum = 0;
00114   for (PRUint32 i = 0; i < NUM_OF_SBCS_PROBERS; i++)
00115   {
00116     if (mProbers[i]) // not null
00117     {
00118       mProbers[i]->Reset();
00119       mIsActive[i] = PR_TRUE;
00120       ++mActiveNum;
00121     }
00122     else
00123       mIsActive[i] = PR_FALSE;
00124   }
00125   mBestGuess = -1;
00126   mState = eDetecting;
00127 }
00128 
00129 
00130 nsProbingState nsSBCSGroupProber::HandleData(const char* aBuf, PRUint32 aLen)
00131 {
00132   nsProbingState st;
00133   PRUint32 i;
00134   char *newBuf1 = 0;
00135   PRUint32 newLen1 = 0;
00136 
00137   //apply filter to original buffer, and we got new buffer back
00138   //depend on what script it is, we will feed them the new buffer 
00139   //we got after applying proper filter
00140   //this is done without any consideration to KeepEnglishLetters
00141   //of each prober since as of now, there are no probers here which
00142   //recognize languages with English characters.
00143   if (!FilterWithoutEnglishLetters(aBuf, aLen, &newBuf1, newLen1))
00144     goto done;
00145   
00146   if (newLen1 == 0)
00147     goto done; // Nothing to see here, move on.
00148 
00149   for (i = 0; i < NUM_OF_SBCS_PROBERS; i++)
00150   {
00151      if (!mIsActive[i])
00152        continue;
00153      st = mProbers[i]->HandleData(newBuf1, newLen1);
00154      if (st == eFoundIt)
00155      {
00156        mBestGuess = i;
00157        mState = eFoundIt;
00158        break;
00159      }
00160      else if (st == eNotMe)
00161      {
00162        mIsActive[i] = PR_FALSE;
00163        mActiveNum--;
00164        if (mActiveNum <= 0)
00165        {
00166          mState = eNotMe;
00167          break;
00168        }
00169      }
00170   }
00171 
00172 done:
00173   PR_FREEIF(newBuf1);
00174 
00175   return mState;
00176 }
00177 
00178 float nsSBCSGroupProber::GetConfidence(void)
00179 {
00180   PRUint32 i;
00181   float bestConf = 0.0, cf;
00182 
00183   switch (mState)
00184   {
00185   case eFoundIt:
00186     return (float)0.99; //sure yes
00187   case eNotMe:
00188     return (float)0.01;  //sure no
00189   default:
00190     for (i = 0; i < NUM_OF_SBCS_PROBERS; i++)
00191     {
00192       if (!mIsActive[i])
00193         continue;
00194       cf = mProbers[i]->GetConfidence();
00195       if (bestConf < cf)
00196       {
00197         bestConf = cf;
00198         mBestGuess = i;
00199       }
00200     }
00201   }
00202   return bestConf;
00203 }
00204 
00205 #ifdef DEBUG_chardet
00206 void nsSBCSGroupProber::DumpStatus()
00207 {
00208   PRUint32 i;
00209   float cf;
00210   
00211   cf = GetConfidence();
00212   printf(" SBCS Group Prober --------begin status \r\n");
00213   for (i = 0; i < NUM_OF_SBCS_PROBERS; i++)
00214   {
00215     if (!mIsActive[i])
00216       printf("  inactive: [%s] (i.e. confidence is too low).\r\n", mProbers[i]->GetCharSetName());
00217     else
00218       mProbers[i]->DumpStatus();
00219   }
00220   printf(" SBCS Group found best match [%s] confidence %f.\r\n",  
00221          mProbers[mBestGuess]->GetCharSetName(), cf);
00222 }
00223 #endif