JpCntx.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* -*- Mode: C++; tab-width: 2; 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 Communicator client code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  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 __JPCNTX_H__
  38. #define __JPCNTX_H__
  39. #define NUM_OF_CATEGORY 6
  40. #include "nscore.h"
  41. #define ENOUGH_REL_THRESHOLD 100
  42. #define MAX_REL_THRESHOLD 1000
  43. //hiragana frequency category table
  44. extern const PRUint8 jp2CharContext[83][83];
  45. class JapaneseContextAnalysis
  46. {
  47. public:
  48. JapaneseContextAnalysis() {Reset(PR_FALSE);}
  49. void HandleData(const char* aBuf, PRUint32 aLen);
  50. void HandleOneChar(const char* aStr, PRUint32 aCharLen)
  51. {
  52. PRInt32 order;
  53. //if we received enough data, stop here
  54. if (mTotalRel > MAX_REL_THRESHOLD) mDone = PR_TRUE;
  55. if (mDone) return;
  56. //Only 2-bytes characters are of our interest
  57. order = (aCharLen == 2) ? GetOrder(aStr) : -1;
  58. if (order != -1 && mLastCharOrder != -1)
  59. {
  60. mTotalRel++;
  61. //count this sequence to its category counter
  62. mRelSample[jp2CharContext[mLastCharOrder][order]]++;
  63. }
  64. mLastCharOrder = order;
  65. }
  66. float GetConfidence(void);
  67. void Reset(PRBool aIsPreferredLanguage);
  68. void SetOpion(){}
  69. PRBool GotEnoughData() {return mTotalRel > ENOUGH_REL_THRESHOLD;}
  70. protected:
  71. virtual PRInt32 GetOrder(const char* str, PRUint32 *charLen) = 0;
  72. virtual PRInt32 GetOrder(const char* str) = 0;
  73. //category counters, each integer counts sequences in its category
  74. PRUint32 mRelSample[NUM_OF_CATEGORY];
  75. //total sequence received
  76. PRUint32 mTotalRel;
  77. //Number of sequences needed to trigger detection
  78. PRUint32 mDataThreshold;
  79. //The order of previous char
  80. PRInt32 mLastCharOrder;
  81. //if last byte in current buffer is not the last byte of a character, we
  82. //need to know how many byte to skip in next buffer.
  83. PRUint32 mNeedToSkipCharNum;
  84. //If this flag is set to PR_TRUE, detection is done and conclusion has been made
  85. PRBool mDone;
  86. };
  87. class SJISContextAnalysis : public JapaneseContextAnalysis
  88. {
  89. //SJISContextAnalysis(){};
  90. protected:
  91. PRInt32 GetOrder(const char* str, PRUint32 *charLen);
  92. PRInt32 GetOrder(const char* str)
  93. {
  94. //We only interested in Hiragana, so first byte is '\202'
  95. if (*str == '\202' &&
  96. (unsigned char)*(str+1) >= (unsigned char)0x9f &&
  97. (unsigned char)*(str+1) <= (unsigned char)0xf1)
  98. return (unsigned char)*(str+1) - (unsigned char)0x9f;
  99. return -1;
  100. }
  101. };
  102. class EUCJPContextAnalysis : public JapaneseContextAnalysis
  103. {
  104. protected:
  105. PRInt32 GetOrder(const char* str, PRUint32 *charLen);
  106. PRInt32 GetOrder(const char* str)
  107. //We only interested in Hiragana, so first byte is '\244'
  108. {
  109. if (*str == '\244' &&
  110. (unsigned char)*(str+1) >= (unsigned char)0xa1 &&
  111. (unsigned char)*(str+1) <= (unsigned char)0xf3)
  112. return (unsigned char)*(str+1) - (unsigned char)0xa1;
  113. return -1;
  114. }
  115. };
  116. #endif /* __JPCNTX_H__ */