MBCSGroupProber.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * Rudi Pettazzi <rudi.pettazzi@gmail.com> (C# 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. namespace UniversalDetector.Core
  39. {
  40. /// <summary>
  41. /// Multi-byte charsets probers
  42. /// </summary>
  43. public class MBCSGroupProber : CharsetProber
  44. {
  45. private const int PROBERS_NUM = 7;
  46. private readonly static string[] ProberName =
  47. { "UTF8", "SJIS", "EUCJP", "GB18030", "EUCKR", "Big5", "EUCTW" };
  48. private CharsetProber[] probers = new CharsetProber[PROBERS_NUM];
  49. private bool[] isActive = new bool[PROBERS_NUM];
  50. private int bestGuess;
  51. private int activeNum;
  52. public MBCSGroupProber()
  53. {
  54. probers[0] = new UTF8Prober();
  55. probers[1] = new SJISProber();
  56. probers[2] = new EUCJPProber();
  57. probers[3] = new GB18030Prober();
  58. probers[4] = new EUCKRProber();
  59. probers[5] = new Big5Prober();
  60. probers[6] = new EUCTWProber();
  61. Reset();
  62. }
  63. public override string GetCharsetName()
  64. {
  65. if (bestGuess == -1)
  66. {
  67. GetConfidence();
  68. if (bestGuess == -1)
  69. bestGuess = 0;
  70. }
  71. return probers[bestGuess].GetCharsetName();
  72. }
  73. public override void Reset()
  74. {
  75. activeNum = 0;
  76. for (int i = 0; i < probers.Length; i++)
  77. {
  78. if (probers[i] != null)
  79. {
  80. probers[i].Reset();
  81. isActive[i] = true;
  82. ++activeNum;
  83. }
  84. else
  85. {
  86. isActive[i] = false;
  87. }
  88. }
  89. bestGuess = -1;
  90. state = ProbingState.Detecting;
  91. }
  92. public override ProbingState HandleData(byte[] buf, int offset, int len)
  93. {
  94. // do filtering to reduce load to probers
  95. byte[] highbyteBuf = new byte[len];
  96. int hptr = 0;
  97. //assume previous is not ascii, it will do no harm except add some noise
  98. bool keepNext = true;
  99. int max = offset + len;
  100. for (int i = offset; i < max; i++)
  101. {
  102. if ((buf[i] & 0x80) != 0)
  103. {
  104. highbyteBuf[hptr++] = buf[i];
  105. keepNext = true;
  106. }
  107. else
  108. {
  109. //if previous is highbyte, keep this even it is a ASCII
  110. if (keepNext)
  111. {
  112. highbyteBuf[hptr++] = buf[i];
  113. keepNext = false;
  114. }
  115. }
  116. }
  117. var st = ProbingState.NotMe;
  118. for (int i = 0; i < probers.Length; i++)
  119. {
  120. if (!isActive[i])
  121. continue;
  122. st = probers[i].HandleData(highbyteBuf, 0, hptr);
  123. if (st == ProbingState.FoundIt)
  124. {
  125. bestGuess = i;
  126. state = ProbingState.FoundIt;
  127. break;
  128. }
  129. else if (st == ProbingState.NotMe)
  130. {
  131. isActive[i] = false;
  132. activeNum--;
  133. if (activeNum <= 0)
  134. {
  135. state = ProbingState.NotMe;
  136. break;
  137. }
  138. }
  139. }
  140. return state;
  141. }
  142. public override float GetConfidence()
  143. {
  144. float bestConf = 0.0f;
  145. float cf = 0.0f;
  146. if (state == ProbingState.FoundIt)
  147. {
  148. return 0.99f;
  149. }
  150. else if (state == ProbingState.NotMe)
  151. {
  152. return 0.01f;
  153. }
  154. else
  155. {
  156. for (int i = 0; i < PROBERS_NUM; i++)
  157. {
  158. if (!isActive[i])
  159. continue;
  160. cf = probers[i].GetConfidence();
  161. if (bestConf < cf)
  162. {
  163. bestConf = cf;
  164. bestGuess = i;
  165. }
  166. }
  167. }
  168. return bestConf;
  169. }
  170. public override void DumpStatus()
  171. {
  172. float cf;
  173. GetConfidence();
  174. for (int i = 0; i < PROBERS_NUM; i++)
  175. {
  176. if (!isActive[i])
  177. {
  178. //Console.WriteLine(" MBCS inactive: {0} (confidence is too low).", ProberName[i]);
  179. }
  180. else
  181. {
  182. cf = probers[i].GetConfidence();
  183. //Console.WriteLine(" MBCS {0}: [{1}]", cf, ProberName[i]);
  184. }
  185. }
  186. }
  187. }
  188. }