2
0

SBCSGroupProber.cs 7.2 KB

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