Blame src/nsHebrewProber.cpp

Packit 67f6e7
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit 67f6e7
/* ***** BEGIN LICENSE BLOCK *****
Packit 67f6e7
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
Packit 67f6e7
 *
Packit 67f6e7
 * The contents of this file are subject to the Mozilla Public License Version
Packit 67f6e7
 * 1.1 (the "License"); you may not use this file except in compliance with
Packit 67f6e7
 * the License. You may obtain a copy of the License at
Packit 67f6e7
 * http://www.mozilla.org/MPL/
Packit 67f6e7
 *
Packit 67f6e7
 * Software distributed under the License is distributed on an "AS IS" basis,
Packit 67f6e7
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
Packit 67f6e7
 * for the specific language governing rights and limitations under the
Packit 67f6e7
 * License.
Packit 67f6e7
 *
Packit 67f6e7
 * The Original Code is Mozilla Universal charset detector code.
Packit 67f6e7
 *
Packit 67f6e7
 * The Initial Developer of the Original Code is
Packit 67f6e7
 *          Shy Shalom <shooshX@gmail.com>
Packit 67f6e7
 * Portions created by the Initial Developer are Copyright (C) 2005
Packit 67f6e7
 * the Initial Developer. All Rights Reserved.
Packit 67f6e7
 *
Packit 67f6e7
 * Contributor(s):
Packit 67f6e7
 *
Packit 67f6e7
 * Alternatively, the contents of this file may be used under the terms of
Packit 67f6e7
 * either the GNU General Public License Version 2 or later (the "GPL"), or
Packit 67f6e7
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
Packit 67f6e7
 * in which case the provisions of the GPL or the LGPL are applicable instead
Packit 67f6e7
 * of those above. If you wish to allow use of your version of this file only
Packit 67f6e7
 * under the terms of either the GPL or the LGPL, and not to allow others to
Packit 67f6e7
 * use your version of this file under the terms of the MPL, indicate your
Packit 67f6e7
 * decision by deleting the provisions above and replace them with the notice
Packit 67f6e7
 * and other provisions required by the GPL or the LGPL. If you do not delete
Packit 67f6e7
 * the provisions above, a recipient may use your version of this file under
Packit 67f6e7
 * the terms of any one of the MPL, the GPL or the LGPL.
Packit 67f6e7
 *
Packit 67f6e7
 * ***** END LICENSE BLOCK ***** */
Packit 67f6e7
Packit 67f6e7
#include "nsHebrewProber.h"
Packit 67f6e7
#include <stdio.h>
Packit 67f6e7
Packit 67f6e7
// windows-1255 / ISO-8859-8 code points of interest
Packit 67f6e7
#define FINAL_KAF ('\xea')
Packit 67f6e7
#define NORMAL_KAF ('\xeb')
Packit 67f6e7
#define FINAL_MEM ('\xed')
Packit 67f6e7
#define NORMAL_MEM ('\xee')
Packit 67f6e7
#define FINAL_NUN ('\xef')
Packit 67f6e7
#define NORMAL_NUN ('\xf0')
Packit 67f6e7
#define FINAL_PE ('\xf3')
Packit 67f6e7
#define NORMAL_PE ('\xf4')
Packit 67f6e7
#define FINAL_TSADI ('\xf5')
Packit 67f6e7
#define NORMAL_TSADI ('\xf6')
Packit 67f6e7
Packit 67f6e7
// Minimum Visual vs Logical final letter score difference.
Packit 67f6e7
// If the difference is below this, don't rely solely on the final letter score distance.
Packit 67f6e7
#define MIN_FINAL_CHAR_DISTANCE (5)
Packit 67f6e7
Packit 67f6e7
// Minimum Visual vs Logical model score difference.
Packit 67f6e7
// If the difference is below this, don't rely at all on the model score distance.
Packit 67f6e7
#define MIN_MODEL_DISTANCE (0.01)
Packit 67f6e7
Packit 67f6e7
#define VISUAL_HEBREW_NAME ("ISO-8859-8")
Packit 67f6e7
#define LOGICAL_HEBREW_NAME ("windows-1255")
Packit 67f6e7
Packit 67f6e7
PRBool nsHebrewProber::isFinal(char c)
Packit 67f6e7
{
Packit 67f6e7
  return ((c == FINAL_KAF) || (c == FINAL_MEM) || (c == FINAL_NUN) || (c == FINAL_PE) || (c == FINAL_TSADI));
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
PRBool nsHebrewProber::isNonFinal(char c)
Packit 67f6e7
{
Packit 67f6e7
  return ((c == NORMAL_KAF) || (c == NORMAL_MEM) || (c == NORMAL_NUN) || (c == NORMAL_PE));
Packit 67f6e7
  // The normal Tsadi is not a good Non-Final letter due to words like 
Packit 67f6e7
  // 'lechotet' (to chat) containing an apostrophe after the tsadi. This 
Packit 67f6e7
  // apostrophe is converted to a space in FilterWithoutEnglishLetters causing 
Packit 67f6e7
  // the Non-Final tsadi to appear at an end of a word even though this is not 
Packit 67f6e7
  // the case in the original text.
Packit 67f6e7
  // The letters Pe and Kaf rarely display a related behavior of not being a 
Packit 67f6e7
  // good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak' for 
Packit 67f6e7
  // example legally end with a Non-Final Pe or Kaf. However, the benefit of 
Packit 67f6e7
  // these letters as Non-Final letters outweighs the damage since these words 
Packit 67f6e7
  // are quite rare.
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
/** HandleData
Packit 67f6e7
 * Final letter analysis for logical-visual decision.
Packit 67f6e7
 * Look for evidence that the received buffer is either logical Hebrew or 
Packit 67f6e7
 * visual Hebrew.
Packit 67f6e7
 * The following cases are checked:
Packit 67f6e7
 * 1) A word longer than 1 letter, ending with a final letter. This is an 
Packit 67f6e7
 *    indication that the text is laid out "naturally" since the final letter 
Packit 67f6e7
 *    really appears at the end. +1 for logical score.
Packit 67f6e7
 * 2) A word longer than 1 letter, ending with a Non-Final letter. In normal
Packit 67f6e7
 *    Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi, should not end with
Packit 67f6e7
 *    the Non-Final form of that letter. Exceptions to this rule are mentioned
Packit 67f6e7
 *    above in isNonFinal(). This is an indication that the text is laid out
Packit 67f6e7
 *    backwards. +1 for visual score
Packit 67f6e7
 * 3) A word longer than 1 letter, starting with a final letter. Final letters 
Packit 67f6e7
 *    should not appear at the beginning of a word. This is an indication that 
Packit 67f6e7
 *    the text is laid out backwards. +1 for visual score.
Packit 67f6e7
 *
Packit 67f6e7
 * The visual score and logical score are accumulated throughout the text and 
Packit 67f6e7
 * are finally checked against each other in GetCharSetName().
Packit 67f6e7
 * No checking for final letters in the middle of words is done since that case
Packit 67f6e7
 * is not an indication for either Logical or Visual text.
Packit 67f6e7
 *
Packit 67f6e7
 * The input buffer should not contain any white spaces that are not (' ')
Packit 67f6e7
 * or any low-ascii punctuation marks. 
Packit 67f6e7
 */
Packit 67f6e7
nsProbingState nsHebrewProber::HandleData(const char* aBuf, PRUint32 aLen)
Packit 67f6e7
{
Packit 67f6e7
  // Both model probers say it's not them. No reason to continue.
Packit 67f6e7
  if (GetState() == eNotMe)
Packit 67f6e7
    return eNotMe;
Packit 67f6e7
Packit 67f6e7
  const char *curPtr, *endPtr = aBuf+aLen;
Packit 67f6e7
  char cur;
Packit 67f6e7
Packit 67f6e7
  for (curPtr = (char*)aBuf; curPtr < endPtr; ++curPtr)
Packit 67f6e7
  {
Packit 67f6e7
    cur = *curPtr;
Packit 67f6e7
    if (cur == ' ') // We stand on a space - a word just ended
Packit 67f6e7
    {
Packit 67f6e7
      if (mBeforePrev != ' ') // *(curPtr-2) was not a space so prev is not a 1 letter word
Packit 67f6e7
      {
Packit 67f6e7
        if (isFinal(mPrev)) // case (1) [-2:not space][-1:final letter][cur:space]
Packit 67f6e7
          ++mFinalCharLogicalScore;
Packit 67f6e7
        else if (isNonFinal(mPrev)) // case (2) [-2:not space][-1:Non-Final letter][cur:space]
Packit 67f6e7
          ++mFinalCharVisualScore;
Packit 67f6e7
      }
Packit 67f6e7
    }
Packit 67f6e7
    else  // Not standing on a space
Packit 67f6e7
    {
Packit 67f6e7
      if ((mBeforePrev == ' ') && (isFinal(mPrev)) && (cur != ' ')) // case (3) [-2:space][-1:final letter][cur:not space]
Packit 67f6e7
        ++mFinalCharVisualScore;
Packit 67f6e7
    }
Packit 67f6e7
    mBeforePrev = mPrev;
Packit 67f6e7
    mPrev = cur;
Packit 67f6e7
  }
Packit 67f6e7
Packit 67f6e7
  // Forever detecting, till the end or until both model probers return eNotMe (handled above).
Packit 67f6e7
  return eDetecting;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
// Make the decision: is it Logical or Visual?
Packit 67f6e7
const char* nsHebrewProber::GetCharSetName()
Packit 67f6e7
{
Packit 67f6e7
  // If the final letter score distance is dominant enough, rely on it.
Packit 67f6e7
  PRInt32 finalsub = mFinalCharLogicalScore - mFinalCharVisualScore;
Packit 67f6e7
  if (finalsub >= MIN_FINAL_CHAR_DISTANCE) 
Packit 67f6e7
    return LOGICAL_HEBREW_NAME;
Packit 67f6e7
  if (finalsub <= -(MIN_FINAL_CHAR_DISTANCE))
Packit 67f6e7
    return VISUAL_HEBREW_NAME;
Packit 67f6e7
Packit 67f6e7
  // It's not dominant enough, try to rely on the model scores instead.
Packit 67f6e7
  float modelsub = mLogicalProb->GetConfidence() - mVisualProb->GetConfidence();
Packit 67f6e7
  if (modelsub > MIN_MODEL_DISTANCE)
Packit 67f6e7
    return LOGICAL_HEBREW_NAME;
Packit 67f6e7
  if (modelsub < -(MIN_MODEL_DISTANCE))
Packit 67f6e7
    return VISUAL_HEBREW_NAME;
Packit 67f6e7
Packit 67f6e7
  // Still no good, back to final letter distance, maybe it'll save the day.
Packit 67f6e7
  if (finalsub < 0) 
Packit 67f6e7
    return VISUAL_HEBREW_NAME;
Packit 67f6e7
Packit 67f6e7
  // (finalsub > 0 - Logical) or (don't know what to do) default to Logical.
Packit 67f6e7
  return LOGICAL_HEBREW_NAME;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
Packit 67f6e7
void nsHebrewProber::Reset(void)
Packit 67f6e7
{
Packit 67f6e7
  mFinalCharLogicalScore = 0;
Packit 67f6e7
  mFinalCharVisualScore = 0;
Packit 67f6e7
Packit 67f6e7
  // mPrev and mBeforePrev are initialized to space in order to simulate a word 
Packit 67f6e7
  // delimiter at the beginning of the data
Packit 67f6e7
  mPrev = ' ';
Packit 67f6e7
  mBeforePrev = ' ';
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
nsProbingState nsHebrewProber::GetState(void) 
Packit 67f6e7
{
Packit 67f6e7
  // Remain active as long as any of the model probers are active.
Packit 67f6e7
  if ((mLogicalProb->GetState() == eNotMe) && (mVisualProb->GetState() == eNotMe))
Packit 67f6e7
    return eNotMe;
Packit 67f6e7
  return eDetecting;
Packit 67f6e7
}
Packit 67f6e7
Packit 67f6e7
#ifdef DEBUG_chardet
Packit 67f6e7
void  nsHebrewProber::DumpStatus()
Packit 67f6e7
{
Packit 67f6e7
  printf("  HEB: %d - %d [Logical-Visual score]\r\n", mFinalCharLogicalScore, mFinalCharVisualScore);
Packit 67f6e7
}
Packit 67f6e7
#endif