michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CharDistribution.h" michael@0: michael@0: #include "JISFreq.tab" michael@0: #include "Big5Freq.tab" michael@0: #include "EUCKRFreq.tab" michael@0: #include "EUCTWFreq.tab" michael@0: #include "GB2312Freq.tab" michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #define SURE_YES 0.99f michael@0: #define SURE_NO 0.01f michael@0: michael@0: //return confidence base on received data michael@0: float CharDistributionAnalysis::GetConfidence(void) michael@0: { michael@0: //if we didn't receive any character in our consideration range, or the michael@0: // number of frequent characters is below the minimum threshold, return michael@0: // negative answer michael@0: if (mTotalChars <= 0 || mFreqChars <= mDataThreshold) michael@0: return SURE_NO; michael@0: michael@0: if (mTotalChars != mFreqChars) { michael@0: float r = mFreqChars / ((mTotalChars - mFreqChars) * mTypicalDistributionRatio); michael@0: michael@0: if (r < SURE_YES) michael@0: return r; michael@0: } michael@0: //normalize confidence, (we don't want to be 100% sure) michael@0: return SURE_YES; michael@0: } michael@0: michael@0: EUCTWDistributionAnalysis::EUCTWDistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = EUCTWCharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(EUCTWCharToFreqOrder); michael@0: mTypicalDistributionRatio = EUCTW_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: michael@0: EUCKRDistributionAnalysis::EUCKRDistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = EUCKRCharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(EUCKRCharToFreqOrder); michael@0: mTypicalDistributionRatio = EUCKR_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: michael@0: GB2312DistributionAnalysis::GB2312DistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = GB2312CharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(GB2312CharToFreqOrder); michael@0: mTypicalDistributionRatio = GB2312_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: michael@0: Big5DistributionAnalysis::Big5DistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = Big5CharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(Big5CharToFreqOrder); michael@0: mTypicalDistributionRatio = BIG5_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: michael@0: SJISDistributionAnalysis::SJISDistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = JISCharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(JISCharToFreqOrder); michael@0: mTypicalDistributionRatio = JIS_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: michael@0: EUCJPDistributionAnalysis::EUCJPDistributionAnalysis() michael@0: { michael@0: mCharToFreqOrder = JISCharToFreqOrder; michael@0: mTableSize = mozilla::ArrayLength(JISCharToFreqOrder); michael@0: mTypicalDistributionRatio = JIS_TYPICAL_DISTRIBUTION_RATIO; michael@0: } michael@0: