SBCSGroupProber.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public class SBCSGroupProber : CharsetProber
  41. {
  42. private const int PROBERS_NUM = 13;
  43. private CharsetProber[] probers = new CharsetProber[PROBERS_NUM];
  44. private bool[] isActive = new bool[PROBERS_NUM];
  45. private int bestGuess;
  46. private int activeNum;
  47. public SBCSGroupProber()
  48. {
  49. probers[0] = new SingleByteCharSetProber(new Win1251Model());
  50. probers[1] = new SingleByteCharSetProber(new Koi8rModel());
  51. probers[2] = new SingleByteCharSetProber(new Latin5Model());
  52. probers[3] = new SingleByteCharSetProber(new MacCyrillicModel());
  53. probers[4] = new SingleByteCharSetProber(new Ibm866Model());
  54. probers[5] = new SingleByteCharSetProber(new Ibm855Model());
  55. probers[6] = new SingleByteCharSetProber(new Latin7Model());
  56. probers[7] = new SingleByteCharSetProber(new Win1253Model());
  57. probers[8] = new SingleByteCharSetProber(new Latin5BulgarianModel());
  58. probers[9] = new SingleByteCharSetProber(new Win1251BulgarianModel());
  59. var hebprober = new HebrewProber();
  60. probers[10] = hebprober;
  61. // Logical
  62. probers[11] = new SingleByteCharSetProber(new Win1255Model(), false, hebprober);
  63. // Visual
  64. probers[12] = new SingleByteCharSetProber(new Win1255Model(), true, hebprober);
  65. hebprober.SetModelProbers(probers[11], probers[12]);
  66. // disable latin2 before latin1 is available, otherwise all latin1
  67. // will be detected as latin2 because of their similarity.
  68. //probers[13] = new SingleByteCharSetProber(new Latin2HungarianModel());
  69. //probers[14] = new SingleByteCharSetProber(new Win1250HungarianModel());
  70. Reset();
  71. }
  72. public override ProbingState HandleData(byte[] buf, int offset, int len)
  73. {
  74. var st = ProbingState.NotMe;
  75. //apply filter to original buffer, and we got new buffer back
  76. //depend on what script it is, we will feed them the new buffer
  77. //we got after applying proper filter
  78. //this is done without any consideration to KeepEnglishLetters
  79. //of each prober since as of now, there are no probers here which
  80. //recognize languages with English characters.
  81. byte[] newBuf = FilterWithoutEnglishLetters(buf, offset, len);
  82. if (newBuf.Length == 0)
  83. return state; // Nothing to see here, move on.
  84. for (int i = 0; i < PROBERS_NUM; i++)
  85. {
  86. if (!isActive[i])
  87. continue;
  88. st = probers[i].HandleData(newBuf, 0, newBuf.Length);
  89. if (st == ProbingState.FoundIt)
  90. {
  91. bestGuess = i;
  92. state = ProbingState.FoundIt;
  93. break;
  94. }
  95. else if (st == ProbingState.NotMe)
  96. {
  97. isActive[i] = false;
  98. activeNum--;
  99. if (activeNum <= 0)
  100. {
  101. state = ProbingState.NotMe;
  102. break;
  103. }
  104. }
  105. }
  106. return state;
  107. }
  108. public override float GetConfidence()
  109. {
  110. float bestConf = 0.0f, cf;
  111. switch (state)
  112. {
  113. case ProbingState.FoundIt:
  114. return 0.99f; //sure yes
  115. case ProbingState.NotMe:
  116. return 0.01f; //sure no
  117. default:
  118. for (int i = 0; i < PROBERS_NUM; i++)
  119. {
  120. if (!isActive[i])
  121. continue;
  122. cf = probers[i].GetConfidence();
  123. if (bestConf < cf)
  124. {
  125. bestConf = cf;
  126. bestGuess = i;
  127. }
  128. }
  129. break;
  130. }
  131. return bestConf;
  132. }
  133. public override void DumpStatus()
  134. {
  135. float cf = GetConfidence();
  136. // Console.WriteLine(" SBCS Group Prober --------begin status");
  137. for (int i = 0; i < PROBERS_NUM; i++)
  138. {
  139. if (isActive[i])
  140. probers[i].DumpStatus();
  141. //else
  142. //Console.WriteLine(" inactive: [{0}] (i.e. confidence is too low).", probers[i].GetCharsetName());
  143. }
  144. //Console.WriteLine(" SBCS Group found best match [{0}] confidence {1}.", probers[bestGuess].GetCharsetName(), cf);
  145. }
  146. public override void Reset()
  147. {
  148. int activeNum = 0;
  149. for (int i = 0; i < PROBERS_NUM; i++)
  150. {
  151. if (probers[i] != null)
  152. {
  153. probers[i].Reset();
  154. isActive[i] = true;
  155. activeNum++;
  156. }
  157. else
  158. {
  159. isActive[i] = false;
  160. }
  161. }
  162. bestGuess = -1;
  163. state = ProbingState.Detecting;
  164. }
  165. public override string GetCharsetName()
  166. {
  167. //if we have no answer yet
  168. if (bestGuess == -1)
  169. {
  170. GetConfidence();
  171. //no charset seems positive
  172. if (bestGuess == -1)
  173. bestGuess = 0;
  174. }
  175. return probers[bestGuess].GetCharsetName();
  176. }
  177. }
  178. }