File indexing completed on 2024-04-28 03:53:02

0001 /*  -*- C++ -*-
0002     SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "CharDistribution.h"
0008 
0009 #include "tables/Big5Freq.tab"
0010 #include "tables/EUCKRFreq.tab"
0011 #include "tables/GB2312Freq.tab"
0012 #include "tables/JISFreq.tab"
0013 
0014 #define SURE_YES 0.99f
0015 #define SURE_NO 0.01f
0016 
0017 namespace kencodingprober
0018 {
0019 // return confidence base on received data
0020 float CharDistributionAnalysis::GetConfidence()
0021 {
0022     // if we didn't receive any character in our consideration range, return negative answer
0023     if (mTotalChars == 0) {
0024         return SURE_NO;
0025     }
0026 
0027     if (mTotalChars != mFreqChars) {
0028         float r = mFreqChars / ((mTotalChars - mFreqChars) * mTypicalDistributionRatio);
0029 
0030         if (r < SURE_YES) {
0031             return r;
0032         }
0033     }
0034     // normalize confidence, (we don't want to be 100% sure)
0035     return SURE_YES;
0036 }
0037 
0038 EUCKRDistributionAnalysis::EUCKRDistributionAnalysis()
0039 {
0040     mCharToFreqOrder = EUCKRCharToFreqOrder;
0041     mTableSize = EUCKR_TABLE_SIZE;
0042     mTypicalDistributionRatio = EUCKR_TYPICAL_DISTRIBUTION_RATIO;
0043 }
0044 
0045 GB2312DistributionAnalysis::GB2312DistributionAnalysis()
0046 {
0047     mCharToFreqOrder = GB2312CharToFreqOrder;
0048     mTableSize = GB2312_TABLE_SIZE;
0049     mTypicalDistributionRatio = GB2312_TYPICAL_DISTRIBUTION_RATIO;
0050 }
0051 
0052 Big5DistributionAnalysis::Big5DistributionAnalysis()
0053 {
0054     mCharToFreqOrder = Big5CharToFreqOrder;
0055     mTableSize = BIG5_TABLE_SIZE;
0056     mTypicalDistributionRatio = BIG5_TYPICAL_DISTRIBUTION_RATIO;
0057 }
0058 
0059 SJISDistributionAnalysis::SJISDistributionAnalysis()
0060 {
0061     mCharToFreqOrder = JISCharToFreqOrder;
0062     mTableSize = JIS_TABLE_SIZE;
0063     mTypicalDistributionRatio = JIS_TYPICAL_DISTRIBUTION_RATIO;
0064 }
0065 
0066 EUCJPDistributionAnalysis::EUCJPDistributionAnalysis()
0067 {
0068     mCharToFreqOrder = JISCharToFreqOrder;
0069     mTableSize = JIS_TABLE_SIZE;
0070     mTypicalDistributionRatio = JIS_TYPICAL_DISTRIBUTION_RATIO;
0071 }
0072 }