summaryrefslogtreecommitdiff
path: root/src/nsLanguageDetector.h
blob: 6ac7ffc8f6d986d3d31027e82279934e4c4d4422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Universal charset detector code. This
 * file was later added by Jehan in 2021 to add language detection.
 *
 * The Initial Developer of the Original Code is Netscape Communications
 * Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001 the
 * Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *          Jehan <zemarmot.net> (2021)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
#ifndef nsLanguageDetector_h__
#define nsLanguageDetector_h__

#include "nscore.h"

#define LANG_SB_ENOUGH_REL_THRESHOLD      2048
#define LANG_POSITIVE_SHORTCUT_THRESHOLD  (float)0.95
#define LANG_NEGATIVE_SHORTCUT_THRESHOLD  (float)0.05

#define LANG_NUMBER_OF_SEQ_CAT 4
#define LANG_POSITIVE_CAT      3
#define LANG_PROBABLE_CAT      2
#define LANG_NEUTRAL_CAT       1
#define LANG_NEGATIVE_CAT      0

typedef struct
{
  const char*          langName;

  /* Table mapping codepoints to character orders. */
  const PRUint32*      charOrderTable;
  int                  charOrderTableSize;

  /* freqCharCount x freqCharCount table of 2-char sequence's frequencies. */
  const PRUint8* const precedenceMatrix;
  /* The count of frequent characters.
   * precedenceMatrix size is the square of this count.
   * charOrderTable can be bigger as it can contain equivalent
   * characters. Yet it maps to this range of orders.
   */
  int                  freqCharCount;
  /* Most languages have 3 or 4 characters which are used more than 40% of the
   * times. We count how many they are and what ratio they are used.
   */
  int                  veryFreqCharCount;
  float                veryFreqRatio;
  /* Most languages will have a whole range of characters which in cumulated
   * total are barely used a few percents of the times. We count how many they
   * are and what ratio they are used.
   */
  int                  lowFreqOrder;
  float                lowFreqRatio;
} LanguageModel;

typedef enum {
  STATE_DETECTING,
  STATE_FOUND,
  STATE_UNLIKELY
} nsDetectState;

class nsLanguageDetector {
public:
  nsLanguageDetector(const LanguageModel *model) : mModel(model) { Reset(); }
  virtual ~nsLanguageDetector() {}

  /* Unlike nsSingleByteCharSetProber, it is charset-unaware and only
   * looks at unicode codepoints.
   */
  virtual const char*   GetLanguage();
  virtual nsDetectState HandleData(const int* codepoints, PRUint32 cpLen);
  virtual void          Reset(void);
  virtual float         GetConfidence(void);

protected:
  const LanguageModel* const mModel;
  nsDetectState              mState;
  /* Order of last character */
  int                        mLastOrder;

  PRUint32 mTotalSeqs;
  PRUint32 mSeqCounters[LANG_NUMBER_OF_SEQ_CAT];

  PRUint32 mTotalChar;
  /*PRUint32 mCtrlChar;*/
  /*PRUint32 mEmoticons;*/
  /*PRUint32 mVariousBetween;*/
  /* Characters that fall in our sampling range */
  PRUint32 mFreqChar;
  /* Most common characters from our sampling range */
  PRUint32 mVeryFreqChar;
  /* Most rare characters from our sampling range */
  PRUint32 mLowFreqChar;
  PRUint32 mOutChar;

private:

  int GetOrderFromCodePoint(int codePoint);
};

extern const LanguageModel ArabicModel;
extern const LanguageModel BelarusianModel;
extern const LanguageModel BulgarianModel;
extern const LanguageModel CroatianModel;
extern const LanguageModel CzechModel;
extern const LanguageModel DanishModel;
extern const LanguageModel EnglishModel;
extern const LanguageModel EsperantoModel;
extern const LanguageModel EstonianModel;
extern const LanguageModel FinnishModel;
extern const LanguageModel FrenchModel;
extern const LanguageModel GermanModel;
extern const LanguageModel GreekModel;
extern const LanguageModel HebrewModel;
extern const LanguageModel HindiModel;
extern const LanguageModel HungarianModel;
extern const LanguageModel IrishModel;
extern const LanguageModel ItalianModel;
extern const LanguageModel LatvianModel;
extern const LanguageModel LithuanianModel;
extern const LanguageModel MalteseModel;
extern const LanguageModel NorwegianModel;
extern const LanguageModel PolishModel;
extern const LanguageModel PortugueseModel;
extern const LanguageModel RomanianModel;
extern const LanguageModel SlovakModel;
extern const LanguageModel SloveneModel;
extern const LanguageModel SpanishModel;
extern const LanguageModel SwedishModel;
extern const LanguageModel ThaiModel;
extern const LanguageModel TurkishModel;
extern const LanguageModel UkrainianModel;
extern const LanguageModel VietnameseModel;

#endif /* nsLanguageDetector_h__ */