File indexing completed on 2024-12-22 04:33:42
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 * Netscape Communications Corporation. 0019 * Portions created by the Initial Developer are Copyright (C) 2001 0020 * the Initial Developer. All Rights Reserved. 0021 * 0022 * Contributor(s): 0023 * Shy Shalom <shooshX@gmail.com> 0024 * 0025 * Alternatively, the contents of this file may be used under the terms of 0026 * either the GNU General Public License Version 2 or later (the "GPL"), or 0027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 0028 * in which case the provisions of the GPL or the LGPL are applicable instead 0029 * of those above. If you wish to allow use of your version of this file only 0030 * under the terms of either the GPL or the LGPL, and not to allow others to 0031 * use your version of this file under the terms of the MPL, indicate your 0032 * decision by deleting the provisions above and replace them with the notice 0033 * and other provisions required by the GPL or the LGPL. If you do not delete 0034 * the provisions above, a recipient may use your version of this file under 0035 * the terms of any one of the MPL, the GPL or the LGPL. 0036 * 0037 * ***** END LICENSE BLOCK ***** */ 0038 0039 #pragma GCC visibility push(hidden) 0040 0041 #include <stdio.h> 0042 #include "nsSBCharSetProber.h" 0043 0044 nsProbingState nsSingleByteCharSetProber::HandleData(const char* aBuf, PRUint32 aLen) 0045 { 0046 unsigned char order; 0047 0048 for (PRUint32 i = 0; i < aLen; i++) 0049 { 0050 order = mModel->charToOrderMap[(unsigned char)aBuf[i]]; 0051 0052 if (order < SYMBOL_CAT_ORDER) 0053 mTotalChar++; 0054 if (order < SAMPLE_SIZE) 0055 { 0056 mFreqChar++; 0057 0058 if (mLastOrder < SAMPLE_SIZE) 0059 { 0060 mTotalSeqs++; 0061 if (!mReversed) 0062 ++(mSeqCounters[int(mModel->precedenceMatrix[mLastOrder*SAMPLE_SIZE+order])]); 0063 else // reverse the order of the letters in the lookup 0064 ++(mSeqCounters[int(mModel->precedenceMatrix[order*SAMPLE_SIZE+mLastOrder])]); 0065 } 0066 } 0067 mLastOrder = order; 0068 } 0069 0070 if (mState == eDetecting) 0071 if (mTotalSeqs > SB_ENOUGH_REL_THRESHOLD) 0072 { 0073 float cf = GetConfidence(); 0074 if (cf > POSITIVE_SHORTCUT_THRESHOLD) 0075 mState = eFoundIt; 0076 else if (cf < NEGATIVE_SHORTCUT_THRESHOLD) 0077 mState = eNotMe; 0078 } 0079 0080 return mState; 0081 } 0082 0083 void nsSingleByteCharSetProber::Reset(void) 0084 { 0085 mState = eDetecting; 0086 mLastOrder = 255; 0087 for (PRUint32 i = 0; i < NUMBER_OF_SEQ_CAT; i++) 0088 mSeqCounters[i] = 0; 0089 mTotalSeqs = 0; 0090 mTotalChar = 0; 0091 mFreqChar = 0; 0092 } 0093 0094 //#define NEGATIVE_APPROACH 1 0095 0096 float nsSingleByteCharSetProber::GetConfidence(void) 0097 { 0098 #ifdef NEGATIVE_APPROACH 0099 if (mTotalSeqs > 0) 0100 if (mTotalSeqs > mSeqCounters[NEGATIVE_CAT]*10 ) 0101 return ((float)(mTotalSeqs - mSeqCounters[NEGATIVE_CAT]*10))/mTotalSeqs * mFreqChar / mTotalChar; 0102 return (float)0.01; 0103 #else //POSITIVE_APPROACH 0104 float r; 0105 0106 if (mTotalSeqs > 0) { 0107 r = ((float)1.0) * mSeqCounters[POSITIVE_CAT] / mTotalSeqs / mModel->mTypicalPositiveRatio; 0108 r = r*mFreqChar/mTotalChar; 0109 if (r >= (float)1.00) 0110 r = (float)0.99; 0111 return r; 0112 } 0113 return (float)0.01; 0114 #endif 0115 } 0116 0117 const char* nsSingleByteCharSetProber::GetCharSetName() 0118 { 0119 if (!mNameProber) 0120 return mModel->charsetName; 0121 return mNameProber->GetCharSetName(); 0122 } 0123 0124 #ifdef DEBUG_chardet 0125 void nsSingleByteCharSetProber::DumpStatus() 0126 { 0127 printf(" SBCS: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName()); 0128 } 0129 #endif 0130 0131 #pragma GCC visibility pop 0132