Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / cld_2 / src / internal / compact_lang_det_impl.h
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //
16 // Author: dsites@google.com (Dick Sites)
17 //
18
19 #ifndef I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_
20 #define I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_
21
22 #include <vector>
23
24 #include "../public/compact_lang_det.h"   // For CLDHints, ResultChunkVector
25 #include "integral_types.h"
26 #include "lang_script.h"
27
28 namespace CLD2 {
29
30 // Internal use flags
31 static const int kCLDFlagFinish = 1;
32 static const int kCLDFlagSqueeze = 2;
33 static const int kCLDFlagRepeats = 4;
34 static const int kCLDFlagTop40 = 8;
35 static const int kCLDFlagShort = 16;
36 static const int kCLDFlagHint = 32;
37 static const int kCLDFlagUseWords = 64;
38 static const int kCLDFlagUNUSED = 128;
39
40 // Public use flags, debug output controls, defined in compact_lang_det.h
41 // 0x0100 and above
42
43 /***
44
45 Flag meanings:
46
47 Flags are used in the context of a recursive call from Detect to itself,
48 trying to deal in a more restrictive way with input that was not reliably
49 identified in the top-level call.
50
51 Finish -- Do not further recurse; return whatever result ensues, even if it is
52           unreliable. Typically set in any recursive call to take a second try
53           on unreliable text.
54
55 Squeeze -- For each text run, do an inplace cheapsqueeze to remove chunks of
56           highly repetitive text and chunks of text with too many 1- and
57           2-letter words. This avoids scoring repetitive or useless non-text
58           crap in large files such bogus JPEGs within an HTML file.
59
60 Repeats -- When scoring a text run, do a cheap prediction of each character
61           and do not score a unigram/quadgram if the last character of same is
62           correctly predicted. This is a slower, finer-grained form of
63           cheapsqueeze, typically used when the first pass got unreliable
64           results.
65
66 Top40 -- Restrict the set of scored languages to the Google "Top 40", which is
67           actually 38 languages. This gets rid of about 110 languages that
68           represent about 0.7% of the web. Typically used when the first pass
69           got unreliable results.
70
71 Short -- DEPRICATED, unused
72
73 Hint -- EXPERIMENTAL flag for compact_lang_det_test.cc to indicate a language
74           hint supplied in parameter plus_one.
75
76 UseWords -- In additon to scoring quad/uni/nil-grams, score complete words
77
78
79
80 Tentative decision logic:
81
82 In the middle of first pass -- After 4KB of text, look at the front 256 bytes
83           of every full 4KB buffer. If it compresses very well (say 3:1) or has
84           lots of spaces (say 1 of every 4 bytes), assume that the input is
85           large and contains lots of bogus non-text. Recurse, passing the
86           Squeeze flag to strip out chunks of this non-text.
87
88 At the end of the first pass --
89           If the top language is reliable and >= 70% of the document, return.
90           Else if the top language is reliable and top+2nd >= say 94%, return.
91           Else, either the top language is not reliable or there is a lot of
92           other crap.
93 ***/
94
95
96   // Scan interchange-valid UTF-8 bytes and detect most likely language,
97   // or set of languages.
98   //
99   // Design goals:
100   //   Skip over big stretches of HTML tags
101   //   Able to return ranges of different languages
102   //   Relatively small tables and relatively fast processing
103   //   Thread safe
104   //
105
106   typedef struct {
107     int perscript_count;
108     const Language* perscript_lang;
109   } PerScriptPair;
110
111   typedef struct {
112     // Constants for hashing 4-7 byte quadgram to 32 bits
113     const int kQuadHashB4Shift;
114     const int kQuadHashB4bShift;
115     const int kQuadHashB5Shift;
116     const int kQuadHashB5bShift;
117     // Constants for hashing 32 bits to kQuadKeyTable subscript/key
118     const int kHashvalToSubShift;
119     const uint32 kHashvalToSubMask;
120     const int kHashvalToKeyShift;
121     const uint32 kHashvalToKeyMask;
122     const int kHashvalAssociativity;
123     // Pointers to the actual tables
124     const PerScriptPair* kPerScriptPair;
125     const uint16* kQuadKeyTable;
126     const uint32* kQuadValueTable;
127   } LangDetObj;
128
129   // Returns the length in bytes of the prefix of src that is all
130   //  interchange valid UTF-8
131   int SpanInterchangeValid(const char* src, int byte_length);
132
133   // For HTML documents, tags are skipped, along with <script> ... </script>
134   // and <style> ... </style> sequences, and entities are expanded.
135   //
136   // We distinguish between bytes of the raw input buffer and bytes of non-tag
137   // text letters. Since tags can be over 50% of the bytes of an HTML Page,
138   // and are nearly all seven-bit ASCII English, we prefer to distinguish
139   // language mixture fractions based on just the non-tag text.
140   //
141   // Inputs: text and text_length
142   //  is_plain_text if true says to NOT parse/skip HTML tags nor entities
143   // Outputs:
144   //  language3 is an array of the top 3 languages or UNKNOWN_LANGUAGE
145   //  percent3 is an array of the text percentages 0..100 of the top 3 languages
146   //  normalized_score3 is an array of internal scores, normalized to the
147   //    average score for each language over a body of training text. A
148   //    normalized score significantly away from 1.0 indicates very skewed text
149   //    or gibberish.
150   //
151   //  text_bytes is the amount of non-tag/letters-only text found
152   //  is_reliable set true if the returned Language is at least 2**30 times more
153   //  probable then the second-best Language
154   //
155   // Return value: the most likely Language for the majority of the input text
156   //  Length 0 input and text with no reliable letter sequences returns
157   //  UNKNOWN_LANGUAGE
158   //
159   // Subsetting: For fast detection over large documents, these routines will
160   // only scan up to a fixed limit (currently 160KB of non-tag letters).
161   //
162
163   Language DetectLanguageSummaryV2(
164                         const char* buffer,
165                         int buffer_length,
166                         bool is_plain_text,
167                         const CLDHints* cld_hints,
168                         bool allow_extended_lang,
169                         int flags,
170                         Language plus_one,
171                         Language* language3,
172                         int* percent3,
173                         double* normalized_score3,
174                         ResultChunkVector* resultchunkvector,
175                         int* text_bytes,
176                         bool* is_reliable);
177
178   // For unit testing:
179   // Remove portions of text that have a high density of spaces, or that are
180   // overly repetitive, squeezing the remaining text in-place to the front
181   // of the input buffer.
182   // Return the new, possibly-shorter length
183   int CheapSqueezeInplace(char* isrc, int srclen, int ichunksize);
184
185 }       // End namespace CLD2
186
187 #endif  // I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_