nsHebrewProber.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla Universal charset detector code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Shy Shalom <shooshX@gmail.com>
  19. * Portions created by the Initial Developer are Copyright (C) 2005
  20. * the Initial Developer: All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsHebrewProber_h__
  38. #define nsHebrewProber_h__
  39. #include "nsSBCharSetProber.h"
  40. // This prober doesn't actually recognize a language or a charset.
  41. // It is a helper prober for the use of the Hebrew model probers
  42. class nsHebrewProber: public nsCharSetProber
  43. {
  44. public:
  45. nsHebrewProber(void) :mLogicalProb(0), mVisualProb(0) { Reset(); }
  46. virtual ~nsHebrewProber(void) {}
  47. virtual nsProbingState HandleData(const char* aBuf, PRUint32 aLen);
  48. virtual const char* GetCharSetName();
  49. virtual void Reset(void);
  50. virtual nsProbingState GetState(void);
  51. virtual float GetConfidence(void) { return (float)0.0; }
  52. virtual void SetOpion() {}
  53. void SetModelProbers(nsCharSetProber *logicalPrb, nsCharSetProber *visualPrb)
  54. { mLogicalProb = logicalPrb; mVisualProb = visualPrb; }
  55. #ifdef DEBUG_chardet
  56. virtual void DumpStatus();
  57. #endif
  58. protected:
  59. static PRBool isFinal(char c);
  60. static PRBool isNonFinal(char c);
  61. PRInt32 mFinalCharLogicalScore, mFinalCharVisualScore;
  62. // The two last characters seen in the previous buffer.
  63. char mPrev, mBeforePrev;
  64. // These probers are owned by the group prober.
  65. nsCharSetProber *mLogicalProb, *mVisualProb;
  66. };
  67. /**
  68. * ** General ideas of the Hebrew charset recognition **
  69. *
  70. * Four main charsets exist in Hebrew:
  71. * "ISO-8859-8" - Visual Hebrew
  72. * "windows-1255" - Logical Hebrew
  73. * "ISO-8859-8-I" - Logical Hebrew
  74. * "x-mac-hebrew" - ?? Logical Hebrew ??
  75. *
  76. * Both "ISO" charsets use a completely identical set of code points, whereas
  77. * "windows-1255" and "x-mac-hebrew" are two different proper supersets of
  78. * these code points. windows-1255 defines additional characters in the range
  79. * 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific
  80. * diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.
  81. * x-mac-hebrew defines similar additional code points but with a different
  82. * mapping.
  83. *
  84. * As far as an average Hebrew text with no diacritics is concerned, all four
  85. * charsets are identical with respect to code points. Meaning that for the
  86. * main Hebrew alphabet, all four map the same values to all 27 Hebrew letters
  87. * (including final letters).
  88. *
  89. * The dominant difference between these charsets is their directionality.
  90. * "Visual" directionality means that the text is ordered as if the renderer is
  91. * not aware of a BIDI rendering algorithm. The renderer sees the text and
  92. * draws it from left to right. The text itself when ordered naturally is read
  93. * backwards. A buffer of Visual Hebrew generally looks like so:
  94. * "[last word of first line spelled backwards] [whole line ordered backwards
  95. * and spelled backwards] [first word of first line spelled backwards]
  96. * [end of line] [last word of second line] ... etc' "
  97. * adding punctuation marks, numbers and English text to visual text is
  98. * naturally also "visual" and from left to right.
  99. *
  100. * "Logical" directionality means the text is ordered "naturally" according to
  101. * the order it is read. It is the responsibility of the renderer to display
  102. * the text from right to left. A BIDI algorithm is used to place general
  103. * punctuation marks, numbers and English text in the text.
  104. *
  105. * Texts in x-mac-hebrew are almost impossible to find on the Internet. From
  106. * what little evidence I could find, it seems that its general directionality
  107. * is Logical.
  108. *
  109. * To sum up all of the above, the Hebrew probing mechanism knows about two
  110. * charsets:
  111. * Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are
  112. * backwards while line order is natural. For charset recognition purposes
  113. * the line order is unimportant (In fact, for this implementation, even
  114. * word order is unimportant).
  115. * Logical Hebrew - "windows-1255" - normal, naturally ordered text.
  116. *
  117. * "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be
  118. * specifically identified.
  119. * "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew
  120. * that contain special punctuation marks or diacritics is displayed with
  121. * some unconverted characters showing as question marks. This problem might
  122. * be corrected using another model prober for x-mac-hebrew. Due to the fact
  123. * that x-mac-hebrew texts are so rare, writing another model prober isn't
  124. * worth the effort and performance hit.
  125. *
  126. * *** The Prober ***
  127. *
  128. * The prober is divided between two nsSBCharSetProbers and an nsHebrewProber,
  129. * all of which are managed, created, fed data, inquired and deleted by the
  130. * nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in
  131. * fact some kind of Hebrew, Logical or Visual. The final decision about which
  132. * one is it is made by the nsHebrewProber by combining final-letter scores
  133. * with the scores of the two nsSBCharSetProbers to produce a final answer.
  134. *
  135. * The nsSBCSGroupProber is responsible for stripping the original text of HTML
  136. * tags, English characters, numbers, low-ASCII punctuation characters, spaces
  137. * and new lines. It reduces any sequence of such characters to a single space.
  138. * The buffer fed to each prober in the SBCS group prober is pure text in
  139. * high-ASCII.
  140. * The two nsSBCharSetProbers (model probers) share the same language model:
  141. * Win1255Model.
  142. * The first nsSBCharSetProber uses the model normally as any other
  143. * nsSBCharSetProber does, to recognize windows-1255, upon which this model was
  144. * built. The second nsSBCharSetProber is told to make the pair-of-letter
  145. * lookup in the language model backwards. This in practice exactly simulates
  146. * a visual Hebrew model using the windows-1255 logical Hebrew model.
  147. *
  148. * The nsHebrewProber is not using any language model. All it does is look for
  149. * final-letter evidence suggesting the text is either logical Hebrew or visual
  150. * Hebrew. Disjointed from the model probers, the results of the nsHebrewProber
  151. * alone are meaningless. nsHebrewProber always returns 0.00 as confidence
  152. * since it never identifies a charset by itself. Instead, the pointer to the
  153. * nsHebrewProber is passed to the model probers as a helper "Name Prober".
  154. * When the Group prober receives a positive identification from any prober,
  155. * it asks for the name of the charset identified. If the prober queried is a
  156. * Hebrew model prober, the model prober forwards the call to the
  157. * nsHebrewProber to make the final decision. In the nsHebrewProber, the
  158. * decision is made according to the final-letters scores maintained and Both
  159. * model probers scores. The answer is returned in the form of the name of the
  160. * charset identified, either "windows-1255" or "ISO-8859-8".
  161. *
  162. */
  163. #endif /* nsHebrewProber_h__ */