nsSBCharSetProber.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. nsProbingState nsSingleByteCharSetProber::HandleData(const char* aBuf, PRUint32 aLen)
  41. {
  42. unsigned char order;
  43. for (PRUint32 i = 0; i < aLen; i++)
  44. {
  45. order = mModel->charToOrderMap[(unsigned char)aBuf[i]];
  46. if (order < SYMBOL_CAT_ORDER)
  47. {
  48. mTotalChar++;
  49. }
  50. else if (order == ILL)
  51. {
  52. /* When encountering an illegal codepoint, no need
  53. * to continue analyzing data. */
  54. mState = eNotMe;
  55. break;
  56. }
  57. else if (order == CTR)
  58. {
  59. mCtrlChar++;
  60. }
  61. if (order < mModel->freqCharCount)
  62. {
  63. mFreqChar++;
  64. if (mLastOrder < mModel->freqCharCount)
  65. {
  66. mTotalSeqs++;
  67. if (!mReversed)
  68. ++(mSeqCounters[mModel->precedenceMatrix[mLastOrder*mModel->freqCharCount+order]]);
  69. else // reverse the order of the letters in the lookup
  70. ++(mSeqCounters[mModel->precedenceMatrix[order*mModel->freqCharCount+mLastOrder]]);
  71. }
  72. }
  73. mLastOrder = order;
  74. }
  75. if (mState == eDetecting)
  76. if (mTotalSeqs > SB_ENOUGH_REL_THRESHOLD)
  77. {
  78. float cf = GetConfidence();
  79. if (cf > POSITIVE_SHORTCUT_THRESHOLD)
  80. mState = eFoundIt;
  81. else if (cf < NEGATIVE_SHORTCUT_THRESHOLD)
  82. mState = eNotMe;
  83. }
  84. return mState;
  85. }
  86. void nsSingleByteCharSetProber::Reset(void)
  87. {
  88. mState = eDetecting;
  89. mLastOrder = 255;
  90. for (PRUint32 i = 0; i < NUMBER_OF_SEQ_CAT; i++)
  91. mSeqCounters[i] = 0;
  92. mTotalSeqs = 0;
  93. mTotalChar = 0;
  94. mCtrlChar = 0;
  95. mFreqChar = 0;
  96. }
  97. //#define NEGATIVE_APPROACH 1
  98. float nsSingleByteCharSetProber::GetConfidence(void)
  99. {
  100. #ifdef NEGATIVE_APPROACH
  101. if (mTotalSeqs > 0)
  102. if (mTotalSeqs > mSeqCounters[NEGATIVE_CAT]*10 )
  103. return ((float)(mTotalSeqs - mSeqCounters[NEGATIVE_CAT]*10))/mTotalSeqs * mFreqChar / mTotalChar;
  104. return (float)0.01;
  105. #else //POSITIVE_APPROACH
  106. float r;
  107. if (mTotalSeqs > 0) {
  108. r = ((float)1.0) * mSeqCounters[POSITIVE_CAT] / mTotalSeqs / mModel->mTypicalPositiveRatio;
  109. /* Multiply by a ratio of positive sequences per characters.
  110. * This would help in particular to distinguish close winners.
  111. * Indeed if you add a letter, you'd expect the positive sequence count
  112. * to increase as well. If it doesn't, it may mean that this new codepoint
  113. * may not have been a letter, but instead a symbol (or some other
  114. * character). This could make the difference between very closely related
  115. * charsets used for the same language.
  116. */
  117. r = r * (mSeqCounters[POSITIVE_CAT] + (float) mSeqCounters[PROBABLE_CAT] / 4) / mTotalChar;
  118. /* The more control characters (proportionnaly to the size of the text), the
  119. * less confident we become in the current charset.
  120. */
  121. r = r * (mTotalChar - mCtrlChar) / mTotalChar;
  122. r = r*mFreqChar/mTotalChar;
  123. if (r >= (float)1.00)
  124. r = (float)0.99;
  125. return r;
  126. }
  127. return (float)0.01;
  128. #endif
  129. }
  130. const char* nsSingleByteCharSetProber::GetCharSetName()
  131. {
  132. if (!mNameProber)
  133. return mModel->charsetName;
  134. return mNameProber->GetCharSetName();
  135. }
  136. #ifdef DEBUG_chardet
  137. void nsSingleByteCharSetProber::DumpStatus()
  138. {
  139. printf(" SBCS: %1.3f [%s]\r\n", GetConfidence(), GetCharSetName());
  140. }
  141. #endif