Blame src/JpCntx.h

Packit 67f6e7
/* -*- Mode: C++; tab-width: 2; 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 Communicator client code.
Packit 67f6e7
 *
Packit 67f6e7
 * The Initial Developer of the Original Code is
Packit 67f6e7
 * Netscape Communications Corporation.
Packit 67f6e7
 * Portions created by the Initial Developer are Copyright (C) 1998
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 __JPCNTX_H__
Packit 67f6e7
#define __JPCNTX_H__
Packit 67f6e7
Packit 67f6e7
#define NUM_OF_CATEGORY 6
Packit 67f6e7
Packit 67f6e7
#include "nscore.h" 
Packit 67f6e7
Packit 67f6e7
#define ENOUGH_REL_THRESHOLD  100
Packit 67f6e7
#define MAX_REL_THRESHOLD     1000
Packit 67f6e7
Packit 67f6e7
//hiragana frequency category table
Packit 67f6e7
extern char jp2CharContext[83][83];
Packit 67f6e7
Packit 67f6e7
class JapaneseContextAnalysis
Packit 67f6e7
{
Packit 67f6e7
public:
Packit 67f6e7
  JapaneseContextAnalysis() {Reset();};
Packit 67f6e7
Packit 67f6e7
  void HandleData(const char* aBuf, PRUint32 aLen);
Packit 67f6e7
Packit 67f6e7
  void HandleOneChar(const char* aStr, PRUint32 aCharLen)
Packit 67f6e7
  {
Packit 67f6e7
    PRInt32 order;
Packit 67f6e7
Packit 67f6e7
    //if we received enough data, stop here   
Packit 67f6e7
    if (mTotalRel > MAX_REL_THRESHOLD)   mDone = PR_TRUE;
Packit 67f6e7
    if (mDone)       return;
Packit 67f6e7
     
Packit 67f6e7
    //Only 2-bytes characters are of our interest
Packit 67f6e7
    order = (aCharLen == 2) ? GetOrder(aStr) : -1;
Packit 67f6e7
    if (order != -1 && mLastCharOrder != -1)
Packit 67f6e7
    {
Packit 67f6e7
      mTotalRel++;
Packit 67f6e7
      //count this sequence to its category counter
Packit 67f6e7
      mRelSample[jp2CharContext[mLastCharOrder][order]]++;
Packit 67f6e7
    }
Packit 67f6e7
    mLastCharOrder = order;
Packit 67f6e7
  };
Packit 67f6e7
Packit 67f6e7
  float GetConfidence();
Packit 67f6e7
  void      Reset(void);
Packit 67f6e7
  void      SetOpion(){};
Packit 67f6e7
  PRBool GotEnoughData() {return mTotalRel > ENOUGH_REL_THRESHOLD;};
Packit 67f6e7
Packit 67f6e7
protected:
Packit 67f6e7
  virtual PRInt32 GetOrder(const char* str, PRUint32 *charLen) = 0;
Packit 67f6e7
  virtual PRInt32 GetOrder(const char* str) = 0;
Packit 67f6e7
Packit 67f6e7
  //category counters, each interger counts sequence in its category
Packit 67f6e7
  PRUint32 mRelSample[NUM_OF_CATEGORY];
Packit 67f6e7
Packit 67f6e7
  //total sequence received
Packit 67f6e7
  PRUint32 mTotalRel;
Packit 67f6e7
  
Packit 67f6e7
  //The order of previous char
Packit 67f6e7
  PRInt32  mLastCharOrder;
Packit 67f6e7
Packit 67f6e7
  //if last byte in current buffer is not the last byte of a character, we
Packit 67f6e7
  //need to know how many byte to skip in next buffer.
Packit 67f6e7
  PRUint32 mNeedToSkipCharNum;
Packit 67f6e7
Packit 67f6e7
  //If this flag is set to PR_TRUE, detection is done and conclusion has been made
Packit 67f6e7
  PRBool   mDone;
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
Packit 67f6e7
class SJISContextAnalysis : public JapaneseContextAnalysis
Packit 67f6e7
{
Packit 67f6e7
  //SJISContextAnalysis(){};
Packit 67f6e7
protected:
Packit 67f6e7
  PRInt32 GetOrder(const char* str, PRUint32 *charLen);
Packit 67f6e7
Packit 67f6e7
  PRInt32 GetOrder(const char* str)
Packit 67f6e7
  {
Packit 67f6e7
    //We only interested in Hiragana, so first byte is '\202'
Packit 67f6e7
    if (*str == '\202' && 
Packit 67f6e7
          (unsigned char)*(str+1) >= (unsigned char)0x9f && 
Packit 67f6e7
          (unsigned char)*(str+1) <= (unsigned char)0xf1)
Packit 67f6e7
      return (unsigned char)*(str+1) - (unsigned char)0x9f;
Packit 67f6e7
    return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
class EUCJPContextAnalysis : public JapaneseContextAnalysis
Packit 67f6e7
{
Packit 67f6e7
protected:
Packit 67f6e7
  PRInt32 GetOrder(const char* str, PRUint32 *charLen);
Packit 67f6e7
  PRInt32 GetOrder(const char* str)
Packit 67f6e7
    //We only interested in Hiragana, so first byte is '\244'
Packit 67f6e7
  {
Packit 67f6e7
    if (*str == '\244' &&
Packit 67f6e7
          (unsigned char)*(str+1) >= (unsigned char)0xa1 &&
Packit 67f6e7
          (unsigned char)*(str+1) <= (unsigned char)0xf3)
Packit 67f6e7
      return (unsigned char)*(str+1) - (unsigned char)0xa1;
Packit 67f6e7
    return -1;
Packit 67f6e7
  };
Packit 67f6e7
};
Packit 67f6e7
Packit 67f6e7
#endif /* __JPCNTX_H__ */
Packit 67f6e7