|
Packit |
67f6e7 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
Packit |
67f6e7 |
/* ***** BEGIN LICENSE BLOCK *****
|
|
Packit |
67f6e7 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* The contents of this file are subject to the Mozilla Public License Version
|
|
Packit |
67f6e7 |
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
Packit |
67f6e7 |
* the License. You may obtain a copy of the License at
|
|
Packit |
67f6e7 |
* http://www.mozilla.org/MPL/
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
Packit |
67f6e7 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
Packit |
67f6e7 |
* for the specific language governing rights and limitations under the
|
|
Packit |
67f6e7 |
* License.
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* The Original Code is Mozilla Universal charset detector code.
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* The Initial Developer of the Original Code is
|
|
Packit |
67f6e7 |
* Netscape Communications Corporation.
|
|
Packit |
67f6e7 |
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
Packit |
67f6e7 |
* the Initial Developer. All Rights Reserved.
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* Contributor(s):
|
|
Packit |
67f6e7 |
* Shy Shalom <shooshX@gmail.com>
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* Alternatively, the contents of this file may be used under the terms of
|
|
Packit |
67f6e7 |
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
Packit |
67f6e7 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
Packit |
67f6e7 |
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
Packit |
67f6e7 |
* of those above. If you wish to allow use of your version of this file only
|
|
Packit |
67f6e7 |
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
Packit |
67f6e7 |
* use your version of this file under the terms of the MPL, indicate your
|
|
Packit |
67f6e7 |
* decision by deleting the provisions above and replace them with the notice
|
|
Packit |
67f6e7 |
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
Packit |
67f6e7 |
* the provisions above, a recipient may use your version of this file under
|
|
Packit |
67f6e7 |
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
Packit |
67f6e7 |
*
|
|
Packit |
67f6e7 |
* ***** END LICENSE BLOCK ***** */
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
#include "nsLatin1Prober.h"
|
|
Packit |
67f6e7 |
#include "prmem.h"
|
|
Packit |
67f6e7 |
#include <stdio.h>
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
#define UDF 0 // undefined
|
|
Packit |
67f6e7 |
#define OTH 1 //other
|
|
Packit |
67f6e7 |
#define ASC 2 // ascii capital letter
|
|
Packit |
67f6e7 |
#define ASS 3 // ascii small letter
|
|
Packit |
67f6e7 |
#define ACV 4 // accent capital vowel
|
|
Packit |
67f6e7 |
#define ACO 5 // accent capital other
|
|
Packit |
67f6e7 |
#define ASV 6 // accent small vowel
|
|
Packit |
67f6e7 |
#define ASO 7 // accent small other
|
|
Packit |
67f6e7 |
#define CLASS_NUM 8 // total classes
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
static unsigned char Latin1_CharToClass[] =
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 00 - 07
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 08 - 0F
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 10 - 17
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 18 - 1F
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 20 - 27
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 28 - 2F
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 30 - 37
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 38 - 3F
|
|
Packit |
67f6e7 |
OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 40 - 47
|
|
Packit |
67f6e7 |
ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 48 - 4F
|
|
Packit |
67f6e7 |
ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 50 - 57
|
|
Packit |
67f6e7 |
ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, // 58 - 5F
|
|
Packit |
67f6e7 |
OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 60 - 67
|
|
Packit |
67f6e7 |
ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 68 - 6F
|
|
Packit |
67f6e7 |
ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 70 - 77
|
|
Packit |
67f6e7 |
ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, // 78 - 7F
|
|
Packit |
67f6e7 |
OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, // 80 - 87
|
|
Packit |
67f6e7 |
OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, // 88 - 8F
|
|
Packit |
67f6e7 |
UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 90 - 97
|
|
Packit |
67f6e7 |
OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, // 98 - 9F
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A0 - A7
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A8 - AF
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B0 - B7
|
|
Packit |
67f6e7 |
OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B8 - BF
|
|
Packit |
67f6e7 |
ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, // C0 - C7
|
|
Packit |
67f6e7 |
ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, // C8 - CF
|
|
Packit |
67f6e7 |
ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, // D0 - D7
|
|
Packit |
67f6e7 |
ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, // D8 - DF
|
|
Packit |
67f6e7 |
ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, // E0 - E7
|
|
Packit |
67f6e7 |
ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, // E8 - EF
|
|
Packit |
67f6e7 |
ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, // F0 - F7
|
|
Packit |
67f6e7 |
ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO, // F8 - FF
|
|
Packit |
67f6e7 |
};
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
/* 0 : illegal
|
|
Packit |
67f6e7 |
1 : very unlikely
|
|
Packit |
67f6e7 |
2 : normal
|
|
Packit |
67f6e7 |
3 : very likely
|
|
Packit |
67f6e7 |
*/
|
|
Packit |
67f6e7 |
static unsigned char Latin1ClassModel[] =
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
/* UDF OTH ASC ASS ACV ACO ASV ASO */
|
|
Packit |
67f6e7 |
/*UDF*/ 0, 0, 0, 0, 0, 0, 0, 0,
|
|
Packit |
67f6e7 |
/*OTH*/ 0, 3, 3, 3, 3, 3, 3, 3,
|
|
Packit |
67f6e7 |
/*ASC*/ 0, 3, 3, 3, 3, 3, 3, 3,
|
|
Packit |
67f6e7 |
/*ASS*/ 0, 3, 3, 3, 1, 1, 3, 3,
|
|
Packit |
67f6e7 |
/*ACV*/ 0, 3, 3, 3, 1, 2, 1, 2,
|
|
Packit |
67f6e7 |
/*ACO*/ 0, 3, 3, 3, 3, 3, 3, 3,
|
|
Packit |
67f6e7 |
/*ASV*/ 0, 3, 1, 3, 1, 1, 1, 3,
|
|
Packit |
67f6e7 |
/*ASO*/ 0, 3, 1, 3, 1, 1, 3, 3,
|
|
Packit |
67f6e7 |
};
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
void nsLatin1Prober::Reset(void)
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
mState = eDetecting;
|
|
Packit |
67f6e7 |
mLastCharClass = OTH;
|
|
Packit |
67f6e7 |
for (int i = 0; i < FREQ_CAT_NUM; i++)
|
|
Packit |
67f6e7 |
mFreqCounter[i] = 0;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
nsProbingState nsLatin1Prober::HandleData(const char* aBuf, PRUint32 aLen)
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
char *newBuf1 = 0;
|
|
Packit |
67f6e7 |
PRUint32 newLen1 = 0;
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
if (!FilterWithEnglishLetters(aBuf, aLen, &newBuf1, newLen1)) {
|
|
Packit |
67f6e7 |
newBuf1 = (char*)aBuf;
|
|
Packit |
67f6e7 |
newLen1 = aLen;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
unsigned char charClass;
|
|
Packit |
67f6e7 |
unsigned char freq;
|
|
Packit |
67f6e7 |
for (PRUint32 i = 0; i < newLen1; i++)
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
charClass = Latin1_CharToClass[(unsigned char)newBuf1[i]];
|
|
Packit |
67f6e7 |
freq = Latin1ClassModel[mLastCharClass*CLASS_NUM + charClass];
|
|
Packit |
67f6e7 |
if (freq == 0) {
|
|
Packit |
67f6e7 |
mState = eNotMe;
|
|
Packit |
67f6e7 |
break;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
mFreqCounter[freq]++;
|
|
Packit |
67f6e7 |
mLastCharClass = charClass;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
if (newBuf1 != aBuf)
|
|
Packit |
67f6e7 |
PR_FREEIF(newBuf1);
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
return mState;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
float nsLatin1Prober::GetConfidence(void)
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
if (mState == eNotMe)
|
|
Packit |
67f6e7 |
return 0.01f;
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
float confidence;
|
|
Packit |
67f6e7 |
PRUint32 total = 0;
|
|
Packit |
67f6e7 |
for (PRInt32 i = 0; i < FREQ_CAT_NUM; i++)
|
|
Packit |
67f6e7 |
total += mFreqCounter[i];
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
if(!total)
|
|
Packit |
67f6e7 |
confidence = 0.0f;
|
|
Packit |
67f6e7 |
else
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
confidence = mFreqCounter[3]*1.0f / total;
|
|
Packit |
67f6e7 |
confidence -= mFreqCounter[1]*20.0f/total;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
if (confidence < 0.0f)
|
|
Packit |
67f6e7 |
confidence = 0.0f;
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
// lower the confidence of latin1 so that other more accurate detector
|
|
Packit |
67f6e7 |
// can take priority.
|
|
Packit |
67f6e7 |
confidence *= 0.50f;
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
return confidence;
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
#ifdef DEBUG_chardet
|
|
Packit |
67f6e7 |
void nsLatin1Prober::DumpStatus()
|
|
Packit |
67f6e7 |
{
|
|
Packit |
67f6e7 |
printf(" Latin1Prober: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName());
|
|
Packit |
67f6e7 |
}
|
|
Packit |
67f6e7 |
#endif
|
|
Packit |
67f6e7 |
|
|
Packit |
67f6e7 |
|