~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/juniversalchardet/src/org/mozilla/universalchardet/prober/Latin1Prober.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is Mozilla Universal charset detector code.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Netscape Communications Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2001
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *          Shy Shalom <shooshX@gmail.com>
 
23
 *          Kohei Taketa <k-tak@void.in> (Java port)
 
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
 
 
39
package org.mozilla.universalchardet.prober;
 
40
 
 
41
import java.nio.ByteBuffer;
 
42
import org.mozilla.universalchardet.Constants;
 
43
 
 
44
 
 
45
public class Latin1Prober extends CharsetProber
 
46
{
 
47
    ////////////////////////////////////////////////////////////////
 
48
    // constants
 
49
    ////////////////////////////////////////////////////////////////
 
50
    public static final byte UDF = 0;
 
51
    public static final byte OTH = 1;
 
52
    public static final byte ASC = 2;
 
53
    public static final byte ASS = 3;
 
54
    public static final byte ACV = 4;
 
55
    public static final byte ACO = 5;
 
56
    public static final byte ASV = 6;
 
57
    public static final byte ASO = 7;
 
58
    public static final int CLASS_NUM = 8;
 
59
    public static final int FREQ_CAT_NUM = 4;
 
60
    
 
61
 
 
62
    ////////////////////////////////////////////////////////////////
 
63
    // fields
 
64
    ////////////////////////////////////////////////////////////////
 
65
    private ProbingState    state;
 
66
    private byte            lastCharClass;
 
67
    private int[]           freqCounter;
 
68
    
 
69
 
 
70
    ////////////////////////////////////////////////////////////////
 
71
    // methods
 
72
    ////////////////////////////////////////////////////////////////
 
73
    public Latin1Prober()
 
74
    {
 
75
        super();
 
76
        
 
77
        this.freqCounter = new int[FREQ_CAT_NUM];
 
78
        
 
79
        reset();
 
80
    }
 
81
 
 
82
    @Override
 
83
    public String getCharSetName()
 
84
    {
 
85
        return Constants.CHARSET_WINDOWS_1252;
 
86
    }
 
87
 
 
88
    @Override
 
89
    public float getConfidence()
 
90
    {
 
91
        if (this.state == ProbingState.NOT_ME) {
 
92
            return 0.01f;
 
93
        }
 
94
        
 
95
        float confidence;
 
96
        int total = 0;
 
97
        for (int i=0; i<this.freqCounter.length; ++i) {
 
98
            total += this.freqCounter[i];
 
99
        }
 
100
        
 
101
        if (total <= 0) {
 
102
            confidence = 0.0f;
 
103
        } else {
 
104
            confidence = this.freqCounter[3] * 1.0f / total;
 
105
            confidence -= this.freqCounter[1] * 20.0f / total;
 
106
        }
 
107
        
 
108
        if (confidence < 0.0f) {
 
109
            confidence = 0.0f;
 
110
        }
 
111
        
 
112
        // lower the confidence of latin1 so that other more accurate detector 
 
113
        // can take priority.
 
114
        confidence *= 0.50f;
 
115
        
 
116
        return confidence;
 
117
    }
 
118
 
 
119
    @Override
 
120
    public ProbingState getState()
 
121
    {
 
122
        return this.state;
 
123
    }
 
124
 
 
125
    @Override
 
126
    public ProbingState handleData(byte[] buf, int offset, int length)
 
127
    {
 
128
        ByteBuffer newBufTmp = filterWithEnglishLetters(buf, offset, length);
 
129
 
 
130
        byte charClass;
 
131
        byte freq;
 
132
        
 
133
        byte[] newBuf = newBufTmp.array();
 
134
        int newBufLen = newBufTmp.position();
 
135
 
 
136
        for (int i=0; i<newBufLen; ++i) {
 
137
            int c = newBuf[i] & 0xFF;
 
138
            charClass = latin1CharToClass[c];
 
139
            freq = latin1ClassModel[this.lastCharClass * CLASS_NUM + charClass];
 
140
            if (freq == 0) {
 
141
                this.state = ProbingState.NOT_ME;
 
142
                break;
 
143
            }
 
144
            ++this.freqCounter[freq];
 
145
            this.lastCharClass = charClass;
 
146
        }
 
147
 
 
148
        return this.state;
 
149
    }
 
150
 
 
151
    @Override
 
152
    public void reset()
 
153
    {
 
154
        this.state = ProbingState.DETECTING;
 
155
        this.lastCharClass = OTH;
 
156
        for (int i=0; i<this.freqCounter.length; ++i) {
 
157
            this.freqCounter[i] = 0;
 
158
        }
 
159
    }
 
160
 
 
161
    @Override
 
162
    public void setOption()
 
163
    {}
 
164
 
 
165
    
 
166
    ////////////////////////////////////////////////////////////////
 
167
    // constants continued
 
168
    ////////////////////////////////////////////////////////////////
 
169
    private static final byte[] latin1CharToClass = new byte[] {
 
170
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 00 - 07
 
171
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 08 - 0F
 
172
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 10 - 17
 
173
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 18 - 1F
 
174
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 20 - 27
 
175
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 28 - 2F
 
176
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 30 - 37
 
177
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 38 - 3F
 
178
          OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC,   // 40 - 47
 
179
          ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,   // 48 - 4F
 
180
          ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,   // 50 - 57
 
181
          ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH,   // 58 - 5F
 
182
          OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS,   // 60 - 67
 
183
          ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS,   // 68 - 6F
 
184
          ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS,   // 70 - 77
 
185
          ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH,   // 78 - 7F
 
186
          OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH,   // 80 - 87
 
187
          OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF,   // 88 - 8F
 
188
          UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // 90 - 97
 
189
          OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO,   // 98 - 9F
 
190
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // A0 - A7
 
191
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // A8 - AF
 
192
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // B0 - B7
 
193
          OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH,   // B8 - BF
 
194
          ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO,   // C0 - C7
 
195
          ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV,   // C8 - CF
 
196
          ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH,   // D0 - D7
 
197
          ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO,   // D8 - DF
 
198
          ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO,   // E0 - E7
 
199
          ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV,   // E8 - EF
 
200
          ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH,   // F0 - F7
 
201
          ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO,   // F8 - FF
 
202
    };
 
203
    
 
204
    private static final byte[] latin1ClassModel = new byte[] {
 
205
        /*      UDF OTH ASC ASS ACV ACO ASV ASO  */
 
206
        /*UDF*/  0,  0,  0,  0,  0,  0,  0,  0,
 
207
        /*OTH*/  0,  3,  3,  3,  3,  3,  3,  3,
 
208
        /*ASC*/  0,  3,  3,  3,  3,  3,  3,  3, 
 
209
        /*ASS*/  0,  3,  3,  3,  1,  1,  3,  3,
 
210
        /*ACV*/  0,  3,  3,  3,  1,  2,  1,  2,
 
211
        /*ACO*/  0,  3,  3,  3,  3,  3,  3,  3, 
 
212
        /*ASV*/  0,  3,  1,  3,  1,  1,  1,  3, 
 
213
        /*ASO*/  0,  3,  1,  3,  1,  1,  3,  3,
 
214
    };
 
215
}