CharsetProber.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.IO;
  39. namespace UniversalDetector.Core
  40. {
  41. public enum ProbingState
  42. {
  43. Detecting = 0, // no sure answer yet, but caller can ask for confidence
  44. FoundIt = 1, // positive answer
  45. NotMe = 2 // negative answer
  46. };
  47. public abstract class CharsetProber
  48. {
  49. protected const float SHORTCUT_THRESHOLD = 0.95F;
  50. protected ProbingState state;
  51. // ASCII codes
  52. private const byte SPACE = 0x20;
  53. private const byte CAPITAL_A = 0x41;
  54. private const byte CAPITAL_Z = 0x5A;
  55. private const byte SMALL_A = 0x61;
  56. private const byte SMALL_Z = 0x7A;
  57. private const byte LESS_THAN = 0x3C;
  58. private const byte GREATER_THAN = 0x3E;
  59. /// <summary>
  60. /// Feed data to the prober
  61. /// </summary>
  62. /// <param name="buf">a buffer</param>
  63. /// <param name="offset">offset into buffer</param>
  64. /// <param name="len">number of bytes available into buffer</param>
  65. /// <returns>
  66. /// A <see cref="ProbingState"/>
  67. /// </returns>
  68. public abstract ProbingState HandleData(byte[] buf, int offset, int len);
  69. /// <summary>
  70. /// Reset prober state
  71. /// </summary>
  72. public abstract void Reset();
  73. public abstract string GetCharsetName();
  74. public abstract float GetConfidence();
  75. public virtual ProbingState GetState()
  76. {
  77. return state;
  78. }
  79. public virtual void SetOption()
  80. {
  81. }
  82. public virtual void DumpStatus()
  83. {
  84. }
  85. //
  86. // Helper functions used in the Latin1 and Group probers
  87. //
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <returns>filtered buffer</returns>
  92. protected static byte[] FilterWithoutEnglishLetters(byte[] buf, int offset, int len)
  93. {
  94. byte[] result = null;
  95. using (var ms = new MemoryStream(buf.Length))
  96. {
  97. bool meetMSB = false;
  98. int max = offset + len;
  99. int prev = offset;
  100. int cur = offset;
  101. while (cur < max)
  102. {
  103. byte b = buf[cur];
  104. if ((b & 0x80) != 0)
  105. {
  106. meetMSB = true;
  107. }
  108. else if (b < CAPITAL_A || (b > CAPITAL_Z && b < SMALL_A)
  109. || b > SMALL_Z)
  110. {
  111. if (meetMSB && cur > prev)
  112. {
  113. ms.Write(buf, prev, cur - prev);
  114. ms.WriteByte(SPACE);
  115. meetMSB = false;
  116. }
  117. prev = cur + 1;
  118. }
  119. cur++;
  120. }
  121. if (meetMSB && cur > prev)
  122. ms.Write(buf, prev, cur - prev);
  123. ms.SetLength(ms.Position);
  124. result = ms.ToArray();
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. /// Do filtering to reduce load to probers (Remove ASCII symbols,
  130. /// collapse spaces). This filter applies to all scripts which contain
  131. /// both English characters and upper ASCII characters.
  132. /// </summary>
  133. /// <returns>a filtered copy of the input buffer</returns>
  134. protected static byte[] FilterWithEnglishLetters(byte[] buf, int offset, int len)
  135. {
  136. byte[] result = null;
  137. using (var ms = new MemoryStream(buf.Length))
  138. {
  139. bool inTag = false;
  140. int max = offset + len;
  141. int prev = offset;
  142. int cur = offset;
  143. while (cur < max)
  144. {
  145. byte b = buf[cur];
  146. if (b == GREATER_THAN)
  147. inTag = false;
  148. else if (b == LESS_THAN)
  149. inTag = true;
  150. // it's ascii, but it's not a letter
  151. if ((b & 0x80) == 0 && (b < CAPITAL_A || b > SMALL_Z
  152. || (b > CAPITAL_Z && b < SMALL_A)))
  153. {
  154. if (cur > prev && !inTag)
  155. {
  156. ms.Write(buf, prev, cur - prev);
  157. ms.WriteByte(SPACE);
  158. }
  159. prev = cur + 1;
  160. }
  161. cur++;
  162. }
  163. // If the current segment contains more than just a symbol
  164. // and it is not inside a tag then keep it.
  165. if (!inTag && cur > prev)
  166. ms.Write(buf, prev, cur - prev);
  167. ms.SetLength(ms.Position);
  168. result = ms.ToArray();
  169. }
  170. return result;
  171. }
  172. }
  173. }