1 // Copyright 2013 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // Author: dsites@google.com (Dick Sites)
19 #ifndef I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_
20 #define I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_
24 #include "../public/compact_lang_det.h" // For CLDHints, ResultChunkVector
25 #include "integral_types.h"
26 #include "lang_script.h"
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;
40 // Public use flags, debug output controls, defined in compact_lang_det.h
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.
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
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.
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
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.
71 Short -- DEPRICATED, unused
73 Hint -- EXPERIMENTAL flag for compact_lang_det_test.cc to indicate a language
74 hint supplied in parameter plus_one.
76 UseWords -- In additon to scoring quad/uni/nil-grams, score complete words
80 Tentative decision logic:
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.
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
96 // Scan interchange-valid UTF-8 bytes and detect most likely language,
97 // or set of languages.
100 // Skip over big stretches of HTML tags
101 // Able to return ranges of different languages
102 // Relatively small tables and relatively fast processing
108 const Language* perscript_lang;
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;
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);
133 // For HTML documents, tags are skipped, along with <script> ... </script>
134 // and <style> ... </style> sequences, and entities are expanded.
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.
141 // Inputs: text and text_length
142 // is_plain_text if true says to NOT parse/skip HTML tags nor entities
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
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
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
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).
163 Language DetectLanguageSummaryV2(
167 const CLDHints* cld_hints,
168 bool allow_extended_lang,
173 double* normalized_score3,
174 ResultChunkVector* resultchunkvector,
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);
185 } // End namespace CLD2
187 #endif // I18N_ENCODINGS_COMPACT_LANG_DET_COMPACT_LANG_DET_IMPL_H_