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