MBCSGroupProber.cs 6.2 KB

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