1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14
* The Original Code is Mozilla Universal charset detector code.
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.
22
* Shy Shalom <shooshX@gmail.com>
23
* Kohei Taketa <k-tak@void.in> (Java port)
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.
37
* ***** END LICENSE BLOCK ***** */
39
package org.mozilla.universalchardet.prober;
41
import java.nio.ByteBuffer;
42
import org.mozilla.universalchardet.Constants;
45
public class Latin1Prober extends CharsetProber
47
////////////////////////////////////////////////////////////////
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;
62
////////////////////////////////////////////////////////////////
64
////////////////////////////////////////////////////////////////
65
private ProbingState state;
66
private byte lastCharClass;
67
private int[] freqCounter;
70
////////////////////////////////////////////////////////////////
72
////////////////////////////////////////////////////////////////
77
this.freqCounter = new int[FREQ_CAT_NUM];
83
public String getCharSetName()
85
return Constants.CHARSET_WINDOWS_1252;
89
public float getConfidence()
91
if (this.state == ProbingState.NOT_ME) {
97
for (int i=0; i<this.freqCounter.length; ++i) {
98
total += this.freqCounter[i];
104
confidence = this.freqCounter[3] * 1.0f / total;
105
confidence -= this.freqCounter[1] * 20.0f / total;
108
if (confidence < 0.0f) {
112
// lower the confidence of latin1 so that other more accurate detector
113
// can take priority.
120
public ProbingState getState()
126
public ProbingState handleData(byte[] buf, int offset, int length)
128
ByteBuffer newBufTmp = filterWithEnglishLetters(buf, offset, length);
133
byte[] newBuf = newBufTmp.array();
134
int newBufLen = newBufTmp.position();
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];
141
this.state = ProbingState.NOT_ME;
144
++this.freqCounter[freq];
145
this.lastCharClass = charClass;
154
this.state = ProbingState.DETECTING;
155
this.lastCharClass = OTH;
156
for (int i=0; i<this.freqCounter.length; ++i) {
157
this.freqCounter[i] = 0;
162
public void setOption()
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
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,