nsLatin1Prober.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "nsLatin1Prober.h"
  39. #include "prmem.h"
  40. #include <stdio.h>
  41. #define UDF 0 // undefined
  42. #define OTH 1 //other
  43. #define ASC 2 // ascii capital letter
  44. #define ASS 3 // ascii small letter
  45. #define ACV 4 // accent capital vowel
  46. #define ACO 5 // accent capital other
  47. #define ASV 6 // accent small vowel
  48. #define ASO 7 // accent small other
  49. #define CLASS_NUM 8 // total classes
  50. static const unsigned char Latin1_CharToClass[] =
  51. {
  52. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 00 - 07
  53. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 08 - 0F
  54. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 10 - 17
  55. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 18 - 1F
  56. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 20 - 27
  57. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 28 - 2F
  58. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 30 - 37
  59. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 38 - 3F
  60. OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 40 - 47
  61. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 48 - 4F
  62. ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, // 50 - 57
  63. ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, // 58 - 5F
  64. OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 60 - 67
  65. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 68 - 6F
  66. ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, // 70 - 77
  67. ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, // 78 - 7F
  68. OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, // 80 - 87
  69. OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, // 88 - 8F
  70. UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // 90 - 97
  71. OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, // 98 - 9F
  72. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A0 - A7
  73. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // A8 - AF
  74. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B0 - B7
  75. OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, // B8 - BF
  76. ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, // C0 - C7
  77. ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, // C8 - CF
  78. ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, // D0 - D7
  79. ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, // D8 - DF
  80. ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, // E0 - E7
  81. ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, // E8 - EF
  82. ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, // F0 - F7
  83. ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO, // F8 - FF
  84. };
  85. /* 0 : illegal
  86. 1 : very unlikely
  87. 2 : normal
  88. 3 : very likely
  89. */
  90. static const unsigned char Latin1ClassModel[] =
  91. {
  92. /* UDF OTH ASC ASS ACV ACO ASV ASO */
  93. /*UDF*/ 0, 0, 0, 0, 0, 0, 0, 0,
  94. /*OTH*/ 0, 3, 3, 3, 3, 3, 3, 3,
  95. /*ASC*/ 0, 3, 3, 3, 3, 3, 3, 3,
  96. /*ASS*/ 0, 3, 3, 3, 1, 1, 3, 3,
  97. /*ACV*/ 0, 3, 3, 3, 1, 2, 1, 2,
  98. /*ACO*/ 0, 3, 3, 3, 3, 3, 3, 3,
  99. /*ASV*/ 0, 3, 1, 3, 1, 1, 1, 3,
  100. /*ASO*/ 0, 3, 1, 3, 1, 1, 3, 3,
  101. };
  102. void nsLatin1Prober::Reset(void)
  103. {
  104. mState = eDetecting;
  105. mLastCharClass = OTH;
  106. for (int i = 0; i < FREQ_CAT_NUM; i++)
  107. mFreqCounter[i] = 0;
  108. }
  109. nsProbingState nsLatin1Prober::HandleData(const char* aBuf, PRUint32 aLen)
  110. {
  111. char *newBuf1 = 0;
  112. PRUint32 newLen1 = 0;
  113. if (!FilterWithEnglishLetters(aBuf, aLen, &newBuf1, newLen1)) {
  114. newBuf1 = (char*)aBuf;
  115. newLen1 = aLen;
  116. }
  117. unsigned char charClass;
  118. unsigned char freq;
  119. for (PRUint32 i = 0; i < newLen1; i++)
  120. {
  121. charClass = Latin1_CharToClass[(unsigned char)newBuf1[i]];
  122. freq = Latin1ClassModel[mLastCharClass*CLASS_NUM + charClass];
  123. if (freq == 0) {
  124. mState = eNotMe;
  125. break;
  126. }
  127. mFreqCounter[freq]++;
  128. mLastCharClass = charClass;
  129. }
  130. if (newBuf1 != aBuf)
  131. PR_FREEIF(newBuf1);
  132. return mState;
  133. }
  134. float nsLatin1Prober::GetConfidence(void)
  135. {
  136. if (mState == eNotMe)
  137. return 0.01f;
  138. float confidence;
  139. PRUint32 total = 0;
  140. for (PRInt32 i = 0; i < FREQ_CAT_NUM; i++)
  141. total += mFreqCounter[i];
  142. if(!total)
  143. confidence = 0.0f;
  144. else
  145. {
  146. confidence = mFreqCounter[3]*1.0f / total;
  147. confidence -= mFreqCounter[1]*20.0f/total;
  148. }
  149. if (confidence < 0.0f)
  150. confidence = 0.0f;
  151. // lower the confidence of latin1 so that other more accurate detector
  152. // can take priority.
  153. confidence *= 0.50f;
  154. return confidence;
  155. }
  156. #ifdef DEBUG_chardet
  157. void nsLatin1Prober::DumpStatus()
  158. {
  159. printf(" Latin1Prober: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName());
  160. }
  161. #endif