Blame src/nsHebrewProber.h

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
#ifndef nsHebrewProber_h__
Packit 67f6e7
#define nsHebrewProber_h__
Packit 67f6e7
Packit 67f6e7
#include "nsSBCharSetProber.h"
Packit 67f6e7
Packit 67f6e7
// This prober doesn't actually recognize a language or a charset.
Packit 67f6e7
// It is a helper prober for the use of the Hebrew model probers
Packit 67f6e7
class nsHebrewProber: public nsCharSetProber
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  nsHebrewProber(void) :mLogicalProb(0), mVisualProb(0) { Reset(); }
Packit 67f6e7
Packit 67f6e7
  virtual ~nsHebrewProber(void) {}
Packit 67f6e7
  virtual nsProbingState HandleData(const char* aBuf, PRUint32 aLen);
Packit 67f6e7
  virtual const char* GetCharSetName();
Packit 67f6e7
  virtual void Reset(void);
Packit 67f6e7
Packit 67f6e7
  virtual nsProbingState GetState(void);
Packit 67f6e7
Packit 67f6e7
  virtual float     GetConfidence(void) { return (float)0.0; }
Packit 67f6e7
  virtual void      SetOpion() {};
Packit 67f6e7
Packit 67f6e7
  void SetModelProbers(nsCharSetProber *logicalPrb, nsCharSetProber *visualPrb) 
Packit 67f6e7
  { mLogicalProb = logicalPrb; mVisualProb = visualPrb; }
Packit 67f6e7
Packit 67f6e7
#ifdef DEBUG_chardet
Packit 67f6e7
  virtual void  DumpStatus();
Packit 67f6e7
#endif
Packit 67f6e7
Packit 67f6e7
protected:
Packit 67f6e7
  static PRBool isFinal(char c);
Packit 67f6e7
  static PRBool isNonFinal(char c);
Packit 67f6e7
Packit 67f6e7
  PRInt32 mFinalCharLogicalScore, mFinalCharVisualScore;
Packit 67f6e7
Packit 67f6e7
  // The two last characters seen in the previous buffer.
Packit 67f6e7
  char mPrev, mBeforePrev;
Packit 67f6e7
Packit 67f6e7
  // These probers are owned by the group prober.
Packit 67f6e7
  nsCharSetProber *mLogicalProb, *mVisualProb;
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
/**
Packit 67f6e7
 * ** General ideas of the Hebrew charset recognition **
Packit 67f6e7
 *
Packit 67f6e7
 * Four main charsets exist in Hebrew:
Packit 67f6e7
 * "ISO-8859-8" - Visual Hebrew
Packit 67f6e7
 * "windows-1255" - Logical Hebrew 
Packit 67f6e7
 * "ISO-8859-8-I" - Logical Hebrew
Packit 67f6e7
 * "x-mac-hebrew" - ?? Logical Hebrew ??
Packit 67f6e7
 *
Packit 67f6e7
 * Both "ISO" charsets use a completely identical set of code points, whereas
Packit 67f6e7
 * "windows-1255" and "x-mac-hebrew" are two different proper supersets of 
Packit 67f6e7
 * these code points. windows-1255 defines additional characters in the range
Packit 67f6e7
 * 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific 
Packit 67f6e7
 * diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.
Packit 67f6e7
 * x-mac-hebrew defines similar additional code points but with a different 
Packit 67f6e7
 * mapping.
Packit 67f6e7
 *
Packit 67f6e7
 * As far as an average Hebrew text with no diacritics is concerned, all four 
Packit 67f6e7
 * charsets are identical with respect to code points. Meaning that for the 
Packit 67f6e7
 * main Hebrew alphabet, all four map the same values to all 27 Hebrew letters 
Packit 67f6e7
 * (including final letters).
Packit 67f6e7
 *
Packit 67f6e7
 * The dominant difference between these charsets is their directionality.
Packit 67f6e7
 * "Visual" directionality means that the text is ordered as if the renderer is
Packit 67f6e7
 * not aware of a BIDI rendering algorithm. The renderer sees the text and 
Packit 67f6e7
 * draws it from left to right. The text itself when ordered naturally is read 
Packit 67f6e7
 * backwards. A buffer of Visual Hebrew generally looks like so:
Packit 67f6e7
 * "[last word of first line spelled backwards] [whole line ordered backwards
Packit 67f6e7
 * and spelled backwards] [first word of first line spelled backwards] 
Packit 67f6e7
 * [end of line] [last word of second line] ... etc' "
Packit 67f6e7
 * adding punctuation marks, numbers and English text to visual text is
Packit 67f6e7
 * naturally also "visual" and from left to right.
Packit 67f6e7
 * 
Packit 67f6e7
 * "Logical" directionality means the text is ordered "naturally" according to
Packit 67f6e7
 * the order it is read. It is the responsibility of the renderer to display 
Packit 67f6e7
 * the text from right to left. A BIDI algorithm is used to place general 
Packit 67f6e7
 * punctuation marks, numbers and English text in the text.
Packit 67f6e7
 *
Packit 67f6e7
 * Texts in x-mac-hebrew are almost impossible to find on the Internet. From 
Packit 67f6e7
 * what little evidence I could find, it seems that its general directionality
Packit 67f6e7
 * is Logical.
Packit 67f6e7
 *
Packit 67f6e7
 * To sum up all of the above, the Hebrew probing mechanism knows about two
Packit 67f6e7
 * charsets:
Packit 67f6e7
 * Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are
Packit 67f6e7
 *    backwards while line order is natural. For charset recognition purposes
Packit 67f6e7
 *    the line order is unimportant (In fact, for this implementation, even 
Packit 67f6e7
 *    word order is unimportant).
Packit 67f6e7
 * Logical Hebrew - "windows-1255" - normal, naturally ordered text.
Packit 67f6e7
 *
Packit 67f6e7
 * "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be 
Packit 67f6e7
 *    specifically identified.
Packit 67f6e7
 * "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew
Packit 67f6e7
 *    that contain special punctuation marks or diacritics is displayed with
Packit 67f6e7
 *    some unconverted characters showing as question marks. This problem might
Packit 67f6e7
 *    be corrected using another model prober for x-mac-hebrew. Due to the fact
Packit 67f6e7
 *    that x-mac-hebrew texts are so rare, writing another model prober isn't 
Packit 67f6e7
 *    worth the effort and performance hit.
Packit 67f6e7
 *
Packit 67f6e7
 * *** The Prober ***
Packit 67f6e7
 *
Packit 67f6e7
 * The prober is divided between two nsSBCharSetProbers and an nsHebrewProber,
Packit 67f6e7
 * all of which are managed, created, fed data, inquired and deleted by the
Packit 67f6e7
 * nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in
Packit 67f6e7
 * fact some kind of Hebrew, Logical or Visual. The final decision about which
Packit 67f6e7
 * one is it is made by the nsHebrewProber by combining final-letter scores
Packit 67f6e7
 * with the scores of the two nsSBCharSetProbers to produce a final answer.
Packit 67f6e7
 *
Packit 67f6e7
 * The nsSBCSGroupProber is responsible for stripping the original text of HTML
Packit 67f6e7
 * tags, English characters, numbers, low-ASCII punctuation characters, spaces
Packit 67f6e7
 * and new lines. It reduces any sequence of such characters to a single space.
Packit 67f6e7
 * The buffer fed to each prober in the SBCS group prober is pure text in
Packit 67f6e7
 * high-ASCII.
Packit 67f6e7
 * The two nsSBCharSetProbers (model probers) share the same language model:
Packit 67f6e7
 * Win1255Model.
Packit 67f6e7
 * The first nsSBCharSetProber uses the model normally as any other
Packit 67f6e7
 * nsSBCharSetProber does, to recognize windows-1255, upon which this model was
Packit 67f6e7
 * built. The second nsSBCharSetProber is told to make the pair-of-letter
Packit 67f6e7
 * lookup in the language model backwards. This in practice exactly simulates
Packit 67f6e7
 * a visual Hebrew model using the windows-1255 logical Hebrew model.
Packit 67f6e7
 *
Packit 67f6e7
 * The nsHebrewProber is not using any language model. All it does is look for
Packit 67f6e7
 * final-letter evidence suggesting the text is either logical Hebrew or visual
Packit 67f6e7
 * Hebrew. Disjointed from the model probers, the results of the nsHebrewProber
Packit 67f6e7
 * alone are meaningless. nsHebrewProber always returns 0.00 as confidence
Packit 67f6e7
 * since it never identifies a charset by itself. Instead, the pointer to the
Packit 67f6e7
 * nsHebrewProber is passed to the model probers as a helper "Name Prober".
Packit 67f6e7
 * When the Group prober receives a positive identification from any prober,
Packit 67f6e7
 * it asks for the name of the charset identified. If the prober queried is a
Packit 67f6e7
 * Hebrew model prober, the model prober forwards the call to the
Packit 67f6e7
 * nsHebrewProber to make the final decision. In the nsHebrewProber, the
Packit 67f6e7
 * decision is made according to the final-letters scores maintained and Both
Packit 67f6e7
 * model probers scores. The answer is returned in the form of the name of the
Packit 67f6e7
 * charset identified, either "windows-1255" or "ISO-8859-8".
Packit 67f6e7
 *
Packit 67f6e7
 */
Packit 67f6e7
#endif /* nsHebrewProber_h__ */