nsHebrewProber.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "nsHebrewProber.h"
  38. #include <stdio.h>
  39. // windows-1255 / ISO-8859-8 code points of interest
  40. #define FINAL_KAF ('\xea')
  41. #define NORMAL_KAF ('\xeb')
  42. #define FINAL_MEM ('\xed')
  43. #define NORMAL_MEM ('\xee')
  44. #define FINAL_NUN ('\xef')
  45. #define NORMAL_NUN ('\xf0')
  46. #define FINAL_PE ('\xf3')
  47. #define NORMAL_PE ('\xf4')
  48. #define FINAL_TSADI ('\xf5')
  49. #define NORMAL_TSADI ('\xf6')
  50. // Minimum Visual vs Logical final letter score difference.
  51. // If the difference is below this, don't rely solely on the final letter score distance.
  52. #define MIN_FINAL_CHAR_DISTANCE (5)
  53. // Minimum Visual vs Logical model score difference.
  54. // If the difference is below this, don't rely at all on the model score distance.
  55. #define MIN_MODEL_DISTANCE (0.01)
  56. #define VISUAL_HEBREW_NAME ("ISO-8859-8")
  57. #define LOGICAL_HEBREW_NAME ("WINDOWS-1255")
  58. PRBool nsHebrewProber::isFinal(char c)
  59. {
  60. return ((c == FINAL_KAF) || (c == FINAL_MEM) || (c == FINAL_NUN) || (c == FINAL_PE) || (c == FINAL_TSADI));
  61. }
  62. PRBool nsHebrewProber::isNonFinal(char c)
  63. {
  64. return ((c == NORMAL_KAF) || (c == NORMAL_MEM) || (c == NORMAL_NUN) || (c == NORMAL_PE));
  65. // The normal Tsadi is not a good Non-Final letter due to words like
  66. // 'lechotet' (to chat) containing an apostrophe after the tsadi. This
  67. // apostrophe is converted to a space in FilterWithoutEnglishLetters causing
  68. // the Non-Final tsadi to appear at an end of a word even though this is not
  69. // the case in the original text.
  70. // The letters Pe and Kaf rarely display a related behavior of not being a
  71. // good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak' for
  72. // example legally end with a Non-Final Pe or Kaf. However, the benefit of
  73. // these letters as Non-Final letters outweighs the damage since these words
  74. // are quite rare.
  75. }
  76. /** HandleData
  77. * Final letter analysis for logical-visual decision.
  78. * Look for evidence that the received buffer is either logical Hebrew or
  79. * visual Hebrew.
  80. * The following cases are checked:
  81. * 1) A word longer than 1 letter, ending with a final letter. This is an
  82. * indication that the text is laid out "naturally" since the final letter
  83. * really appears at the end. +1 for logical score.
  84. * 2) A word longer than 1 letter, ending with a Non-Final letter. In normal
  85. * Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi, should not end with
  86. * the Non-Final form of that letter. Exceptions to this rule are mentioned
  87. * above in isNonFinal(). This is an indication that the text is laid out
  88. * backwards. +1 for visual score
  89. * 3) A word longer than 1 letter, starting with a final letter. Final letters
  90. * should not appear at the beginning of a word. This is an indication that
  91. * the text is laid out backwards. +1 for visual score.
  92. *
  93. * The visual score and logical score are accumulated throughout the text and
  94. * are finally checked against each other in GetCharSetName().
  95. * No checking for final letters in the middle of words is done since that case
  96. * is not an indication for either Logical or Visual text.
  97. *
  98. * The input buffer should not contain any white spaces that are not (' ')
  99. * or any low-ascii punctuation marks.
  100. */
  101. nsProbingState nsHebrewProber::HandleData(const char* aBuf, PRUint32 aLen)
  102. {
  103. // Both model probers say it's not them. No reason to continue.
  104. if (GetState() == eNotMe)
  105. return eNotMe;
  106. const char *curPtr, *endPtr = aBuf+aLen;
  107. char cur;
  108. for (curPtr = (char*)aBuf; curPtr < endPtr; ++curPtr)
  109. {
  110. cur = *curPtr;
  111. if (cur == ' ') // We stand on a space - a word just ended
  112. {
  113. if (mBeforePrev != ' ') // *(curPtr-2) was not a space so prev is not a 1 letter word
  114. {
  115. if (isFinal(mPrev)) // case (1) [-2:not space][-1:final letter][cur:space]
  116. ++mFinalCharLogicalScore;
  117. else if (isNonFinal(mPrev)) // case (2) [-2:not space][-1:Non-Final letter][cur:space]
  118. ++mFinalCharVisualScore;
  119. }
  120. }
  121. else // Not standing on a space
  122. {
  123. if ((mBeforePrev == ' ') && (isFinal(mPrev)) && (cur != ' ')) // case (3) [-2:space][-1:final letter][cur:not space]
  124. ++mFinalCharVisualScore;
  125. }
  126. mBeforePrev = mPrev;
  127. mPrev = cur;
  128. }
  129. // Forever detecting, till the end or until both model probers return eNotMe (handled above).
  130. return eDetecting;
  131. }
  132. // Make the decision: is it Logical or Visual?
  133. const char* nsHebrewProber::GetCharSetName()
  134. {
  135. // If the final letter score distance is dominant enough, rely on it.
  136. PRInt32 finalsub = mFinalCharLogicalScore - mFinalCharVisualScore;
  137. if (finalsub >= MIN_FINAL_CHAR_DISTANCE)
  138. return LOGICAL_HEBREW_NAME;
  139. if (finalsub <= -(MIN_FINAL_CHAR_DISTANCE))
  140. return VISUAL_HEBREW_NAME;
  141. // It's not dominant enough, try to rely on the model scores instead.
  142. float modelsub = mLogicalProb->GetConfidence() - mVisualProb->GetConfidence();
  143. if (modelsub > MIN_MODEL_DISTANCE)
  144. return LOGICAL_HEBREW_NAME;
  145. if (modelsub < -(MIN_MODEL_DISTANCE))
  146. return VISUAL_HEBREW_NAME;
  147. // Still no good, back to final letter distance, maybe it'll save the day.
  148. if (finalsub < 0)
  149. return VISUAL_HEBREW_NAME;
  150. // (finalsub > 0 - Logical) or (don't know what to do) default to Logical.
  151. return LOGICAL_HEBREW_NAME;
  152. }
  153. void nsHebrewProber::Reset(void)
  154. {
  155. mFinalCharLogicalScore = 0;
  156. mFinalCharVisualScore = 0;
  157. // mPrev and mBeforePrev are initialized to space in order to simulate a word
  158. // delimiter at the beginning of the data
  159. mPrev = ' ';
  160. mBeforePrev = ' ';
  161. }
  162. nsProbingState nsHebrewProber::GetState(void)
  163. {
  164. // Remain active as long as any of the model probers are active.
  165. if ((mLogicalProb->GetState() == eNotMe) && (mVisualProb->GetState() == eNotMe))
  166. return eNotMe;
  167. return eDetecting;
  168. }
  169. #ifdef DEBUG_chardet
  170. void nsHebrewProber::DumpStatus()
  171. {
  172. printf(" HEB: %d - %d [Logical-Visual score]\r\n", mFinalCharLogicalScore, mFinalCharVisualScore);
  173. }
  174. #endif