| 1 |
|
| /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
|
| /* ***** BEGIN LICENSE BLOCK ***** |
| 3 |
|
| * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 |
|
| * |
| 5 |
|
| * The contents of this file are subject to the Mozilla Public License Version |
| 6 |
|
| * 1.1 (the "License"); you may not use this file except in compliance with |
| 7 |
|
| * the License. You may obtain a copy of the License at |
| 8 |
|
| * http://www.mozilla.org/MPL/ |
| 9 |
|
| * |
| 10 |
|
| * Software distributed under the License is distributed on an "AS IS" basis, |
| 11 |
|
| * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 |
|
| * for the specific language governing rights and limitations under the |
| 13 |
|
| * License. |
| 14 |
|
| * |
| 15 |
|
| * The Original Code is Mozilla Universal charset detector code. |
| 16 |
|
| * |
| 17 |
|
| * The Initial Developer of the Original Code is |
| 18 |
|
| * Netscape Communications Corporation. |
| 19 |
|
| * Portions created by the Initial Developer are Copyright (C) 2001 |
| 20 |
|
| * the Initial Developer. All Rights Reserved. |
| 21 |
|
| * |
| 22 |
|
| * Contributor(s): |
| 23 |
|
| * Shy Shalom <shooshX@gmail.com> |
| 24 |
|
| * |
| 25 |
|
| * Alternatively, the contents of this file may be used under the terms of |
| 26 |
|
| * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 |
|
| * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 |
|
| * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 |
|
| * of those above. If you wish to allow use of your version of this file only |
| 30 |
|
| * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 |
|
| * use your version of this file under the terms of the MPL, indicate your |
| 32 |
|
| * decision by deleting the provisions above and replace them with the notice |
| 33 |
|
| * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 |
|
| * the provisions above, a recipient may use your version of this file under |
| 35 |
|
| * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 |
|
| * |
| 37 |
|
| * ***** END LICENSE BLOCK ***** */ |
| 38 |
|
| #include <stdio.h> |
| 39 |
|
| #include "nsSBCharSetProber.h" |
| 40 |
|
| |
| 41 |
48 |
| nsProbingState nsSingleByteCharSetProber::HandleData(const char* aBuf, PRUint32 aLen) |
| 42 |
|
| { |
| 43 |
|
| unsigned char order; |
| 44 |
|
| |
| 45 |
600 |
▶ | for (PRUint32 i = 0; i < aLen; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (PRUint32 i = 0; i < aLen; i++) |
552 |
48 |
600 |
|
| 46 |
|
| { |
| 47 |
552 |
| order = mModel->charToOrderMap[(unsigned char)aBuf[i]]; |
| 48 |
|
| |
| 49 |
552 |
▶ | if (order < SYMBOL_CAT_ORDER) |
Branch: 1 branch
| Branch | True | False | Total |
| if (order < SYMBOL_CAT_ORDER) |
516 |
36 |
552 |
|
| 50 |
516 |
| mTotalChar++; |
| 51 |
552 |
▶ | if (order < SAMPLE_SIZE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (order < SAMPLE_SIZE) |
154 |
398 |
552 |
|
| 52 |
|
| { |
| 53 |
154 |
| mFreqChar++; |
| 54 |
|
| |
| 55 |
154 |
▶ | if (mLastOrder < SAMPLE_SIZE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mLastOrder < SAMPLE_SIZE) |
32 |
122 |
154 |
|
| 56 |
|
| { |
| 57 |
32 |
| mTotalSeqs++; |
| 58 |
32 |
▶ | if (!mReversed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mReversed) |
18 |
14 |
32 |
|
| 59 |
18 |
| ++(mSeqCounters[mModel->precedenceMatrix[mLastOrder*SAMPLE_SIZE+order]]); |
| 60 |
|
| else // reverse the order of the letters in the lookup |
| 61 |
14 |
| ++(mSeqCounters[mModel->precedenceMatrix[order*SAMPLE_SIZE+mLastOrder]]); |
| 62 |
|
| } |
| 63 |
|
| } |
| 64 |
552 |
| mLastOrder = order; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
48 |
▶ | if (mState == eDetecting) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mState == eDetecting) |
48 |
0 |
48 |
|
| 68 |
48 |
▶ | if (mTotalSeqs > SB_ENOUGH_REL_THRESHOLD) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mTotalSeqs > SB_ENOUGH_REL_THRESHOLD) |
0 |
48 |
48 |
|
| 69 |
|
| { |
| 70 |
0 |
| float cf = GetConfidence(); |
| 71 |
0 |
▶ | if (cf > POSITIVE_SHORTCUT_THRESHOLD) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cf > POSITIVE_SHORTCUT_THRESHOLD) |
0 |
0 |
0 |
|
| 72 |
0 |
| mState = eFoundIt; |
| 73 |
0 |
▶ | else if (cf < NEGATIVE_SHORTCUT_THRESHOLD) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (cf < NEGATIVE_SHORTCUT_THRESHOLD) |
0 |
0 |
0 |
|
| 74 |
0 |
| mState = eNotMe; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
48 |
| return mState; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
96 |
| void nsSingleByteCharSetProber::Reset(void) |
| 81 |
|
| { |
| 82 |
96 |
| mState = eDetecting; |
| 83 |
96 |
| mLastOrder = 255; |
| 84 |
480 |
▶ | for (PRUint32 i = 0; i < NUMBER_OF_SEQ_CAT; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (PRUint32 i = 0; i < NUMBER_OF_SEQ_CAT; i++) |
384 |
96 |
480 |
|
| 85 |
384 |
| mSeqCounters[i] = 0; |
| 86 |
96 |
| mTotalSeqs = 0; |
| 87 |
96 |
| mTotalChar = 0; |
| 88 |
96 |
| mFreqChar = 0; |
| 89 |
96 |
| } |
| 90 |
|
| |
| 91 |
|
| //#define NEGATIVE_APPROACH 1 |
| 92 |
|
| |
| 93 |
48 |
| float nsSingleByteCharSetProber::GetConfidence(void) |
| 94 |
|
| { |
| 95 |
|
| #ifdef NEGATIVE_APPROACH |
| 96 |
|
| if (mTotalSeqs > 0) |
| 97 |
|
| if (mTotalSeqs > mSeqCounters[NEGATIVE_CAT]*10 ) |
| 98 |
|
| return ((float)(mTotalSeqs - mSeqCounters[NEGATIVE_CAT]*10))/mTotalSeqs * mFreqChar / mTotalChar; |
| 99 |
|
| return (float)0.01; |
| 100 |
|
| #else //POSITIVE_APPROACH |
| 101 |
|
| float r; |
| 102 |
|
| |
| 103 |
48 |
▶ | if (mTotalSeqs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mTotalSeqs > 0) { |
6 |
42 |
48 |
|
| 104 |
6 |
| r = ((float)1.0) * mSeqCounters[POSITIVE_CAT] / mTotalSeqs / mModel->mTypicalPositiveRatio; |
| 105 |
6 |
| r = r*mFreqChar/mTotalChar; |
| 106 |
6 |
▶ | if (r >= (float)1.00) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r >= (float)1.00) |
0 |
6 |
6 |
|
| 107 |
0 |
| r = (float)0.99; |
| 108 |
6 |
| return r; |
| 109 |
|
| } |
| 110 |
42 |
| return (float)0.01; |
| 111 |
|
| #endif |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
0 |
| const char* nsSingleByteCharSetProber::GetCharSetName() |
| 115 |
|
| { |
| 116 |
0 |
▶ | if (!mNameProber) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mNameProber) |
0 |
0 |
0 |
|
| 117 |
0 |
| return mModel->charsetName; |
| 118 |
0 |
| return mNameProber->GetCharSetName(); |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| #ifdef DEBUG_chardet |
| 122 |
|
| void nsSingleByteCharSetProber::DumpStatus() |
| 123 |
|
| { |
| 124 |
|
| printf(" SBCS: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName()); |
| 125 |
|
| } |
| 126 |
|
| #endif |