1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef ENCODINGS_COMPACT_LANG_DET_CLDUTIL_H_
6 #define ENCODINGS_COMPACT_LANG_DET_CLDUTIL_H_
9 #include "encodings/compact_lang_det/ext_lang_enc.h"
10 #include "encodings/compact_lang_det/tote.h"
11 #include "encodings/compact_lang_det/win/cld_basictypes.h"
12 #include "encodings/compact_lang_det/win/cld_commandlineflags.h"
13 #include "encodings/compact_lang_det/win/cld_utf8statetable.h"
17 // Hash bucket for four-way associative lookup with < 64K buckets
18 // 32 bytes per bucket, 8-byte entries
20 uint32 key
[4]; // hashed word to look up
21 uint32 value
[4]; // packed three lang numbers and probability subscript
22 } SmallWordProbBucket4
;
24 // Hash bucket for fouro-way associative lookup with >= 64K buckets
25 // 24 bytes per bucket, 6-byte entries
27 uint16 key
[4]; // Half of hashed word to look up; other
28 // half is used to pick the bucket
29 uint32 value
[4]; // packed three lang numbers and probability subscript
30 } LargeQuadProbBucket4
;
32 // Hash bucket for four-way associative lookup, indirect probabilities
33 // 16 bytes per bucket, 4-byte entries
35 uint32 keyvalue
[4]; // Upper part of word is hash, lower is indirect prob
36 } IndirectProbBucket4
;
39 // This describes a complete CLD table, consisting of
40 // a main lookup table, an indirect language/probability table, and
42 // The main table key is a quadgram, bigram, or longword hash, with
43 // part of the key used to select a bucket modulo kCLDTableSize,
44 // and the rest matched against the key portion of four entries in a bucket,
45 // defined by kCLDTableKeyMask. The remaining bits of an entry, defined
46 // by ~kCLDTableKeyMask, are usually a subscript in the indirect table.
48 // By using part of the key to select a bucket, those key bits do not need
49 // to be stored in the main table entries, saving space (typically 2 bytes).
51 // By using an indirect table for lang/prob triples, only the subscript needs
52 // to be stored in the main table entires, saving space (typically 2 bytes).
54 // Each entry in the indirect table has three languages and three
55 // corresponding probabilities, packed into four bytes.
57 // The build date constant is included just for version tracking and is not
60 // Different-size tables can be linked in for different production
61 // environments. By going indirect through this struct, the runtime code is
62 // insensitive to the actual sizes.
64 // An empty placeholder table can be described by a table size of 1
65 // bucket, a keymask of 0xffffffff, a degenerate bucket of four no-match
66 // entries, and a degenerate indirect table of one no-languages entry.
69 struct CLDTableSummary
{
70 const IndirectProbBucket4
* kCLDTable
;
71 // Each bucket has four entries, part
72 // key and part indirect subscript
73 const uint32
* kCLDTableInd
; // Each entry is three packed lang/prob
74 const int kCLDTableSize
; // Bucket count
75 const int kCLDTableIndSize
; // Entries count
76 const int kCLDTableKeyMask
; // Mask hash key
77 const int kCLDTableBuildDate
; // yyyymmdd
81 // Keeps per-character 0-12 language probabilities for CTJKVZ-- in that order.
82 // Chinese ChineseT Japanese Korean Vietnamese Zhuang
83 // (2 bytes unused, for alignment padding and future)
88 // Map 8-bit subscript to CTJKVZ probabilities
89 // Target runtime probabilities for CTJK + VZ
90 // Hand-generated to cover a reasonable range of choices
91 static const int kTargetCTJKVZProbsSize
= 242;
92 static const UnigramProbArray kTargetCTJKVZProbs
[kTargetCTJKVZProbsSize
] = {
341 // 1 to skip ASCII space, vowels AEIOU aeiou and UTF-8 continuation bytes 80-BF
342 static const uint8 kSkipSpaceVowelContinue
[256] = {
343 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
344 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
345 0,1,0,0,0,1,0,0, 0,1,0,0,0,0,0,1, 0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,
346 0,1,0,0,0,1,0,0, 0,1,0,0,0,0,0,1, 0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,
348 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
349 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
350 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
351 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
354 // 1 to skip ASCII space, and UTF-8 continuation bytes 80-BF
355 static const uint8 kSkipSpaceContinue
[256] = {
356 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
357 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
358 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
359 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
361 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
362 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
363 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
364 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
368 // If != UNKNOWN, use nilgrams to determine language of this script
369 static const Language kOnlyLanguagePerLScript
[] = {
370 ENGLISH
, // ULScript_Common, [no words should be in this script]
371 UNKNOWN_LANGUAGE
, // ULScript_Latin,
372 //UNKNOWN_LANGUAGE, // ULScript_Greek, Jan 2009: change so we can score quads
373 GREEK
, // ULScript_Greek, Mar 2009: change back; do gibberish separately
374 UNKNOWN_LANGUAGE
, // ULScript_Cyrillic,
375 ARMENIAN
, // ULScript_Armenian,
376 UNKNOWN_LANGUAGE
, // ULScript_Hebrew,
377 UNKNOWN_LANGUAGE
, // ULScript_Arabic,
378 SYRIAC
, // ULScript_Syriac,
379 DHIVEHI
, // ULScript_Thaana,
380 UNKNOWN_LANGUAGE
, // ULScript_Devanagari,
381 UNKNOWN_LANGUAGE
, // ULScript_Bengali,
382 PUNJABI
, // ULScript_Gurmukhi,
383 GUJARATI
, // ULScript_Gujarati,
384 ORIYA
, // ULScript_Oriya,
385 TAMIL
, // ULScript_Tamil,
386 TELUGU
, // ULScript_Telugu,
387 KANNADA
, // ULScript_Kannada,
388 MALAYALAM
, // ULScript_Malayalam,
389 SINHALESE
, // ULScript_Sinhala,
390 THAI
, // ULScript_Thai,
391 LAOTHIAN
, // ULScript_Lao,
392 UNKNOWN_LANGUAGE
, // ULScript_Tibetan,
393 BURMESE
, // ULScript_Myanmar,
394 GEORGIAN
, // ULScript_Georgian,
395 UNKNOWN_LANGUAGE
, // ULScript_HanCJK,
396 UNKNOWN_LANGUAGE
, // ULScript_Ethiopic,
397 CHEROKEE
, // ULScript_Cherokee,
398 INUKTITUT
, // ULScript_Canadian_Aboriginal,
399 X_OGHAM
, // ULScript_Ogham,
400 X_RUNIC
, // ULScript_Runic,
401 KHMER
, // ULScript_Khmer,
402 MONGOLIAN
, // ULScript_Mongolian,
403 X_YI
, // ULScript_Yi,
404 X_OLD_ITALIC
, // ULScript_Old_Italic,
405 X_GOTHIC
, // ULScript_Gothic,
406 X_DESERET
, // ULScript_Deseret,
407 ENGLISH
, // ULScript_Inherited, [no words should be in this script]
408 TAGALOG
, // ULScript_Tagalog,
409 X_HANUNOO
, // ULScript_Hanunoo,
410 X_BUHID
, // ULScript_Buhid,
411 X_TAGBANWA
, // ULScript_Tagbanwa,
412 LIMBU
, // ULScript_Limbu,
413 X_TAI_LE
, // ULScript_Tai_Le,
414 X_LINEAR_B
, // ULScript_Linear_B,
415 X_UGARITIC
, // ULScript_Ugaritic,
416 X_SHAVIAN
, // ULScript_Shavian,
417 X_OSMANYA
, // ULScript_Osmanya,
418 X_CYPRIOT
, // ULScript_Cypriot,
419 X_BUGINESE
, // ULScript_Buginese,
420 X_COPTIC
, // ULScript_Coptic,
421 X_NEW_TAI_LUE
, // ULScript_New_Tai_Lue,
422 X_GLAGOLITIC
, // ULScript_Glagolitic,
423 X_TIFINAGH
, // ULScript_Tifinagh,
424 X_SYLOTI_NAGRI
, // ULScript_Syloti_Nagri,
425 X_OLD_PERSIAN
, // ULScript_Old_Persian,
426 X_KHAROSHTHI
, // ULScript_Kharoshthi,
427 X_BALINESE
, // ULScript_Balinese,
428 X_CUNEIFORM
, // ULScript_Cuneiform,
429 X_PHOENICIAN
, // ULScript_Phoenician,
430 X_PHAGS_PA
, // ULScript_Phags_Pa,
431 X_NKO
, // ULScript_Nko,
434 X_SUDANESE
, // ULScript_Sundanese,
435 X_LEPCHA
, // ULScript_Lepcha,
436 X_OL_CHIKI
, // ULScript_Ol_Chiki,
437 X_VAI
, // ULScript_Vai,
438 X_SAURASHTRA
, // ULScript_Saurashtra,
439 X_KAYAH_LI
, // ULScript_Kayah_Li,
440 X_REJANG
, // ULScript_Rejang,
441 X_LYCIAN
, // ULScript_Lycian,
442 X_CARIAN
, // ULScript_Carian,
443 X_LYDIAN
, // ULScript_Lydian,
444 X_CHAM
, // ULScript_Cham,
447 COMPILE_ASSERT(arraysize(kOnlyLanguagePerLScript
) == ULScript_NUM_SCRIPTS
,
448 kOnlyLanguagePerLScript_has_incorrect_length
);
451 // This is, in a sense, the complement of the table above
452 // If != UNKNOWN, determines a default language of this script
453 static const Language kDefaultLanguagePerLScript
[] = {
454 UNKNOWN_LANGUAGE
, // ULScript_Common, [no words should be in this script]
455 ENGLISH
, // ULScript_Latin,
456 UNKNOWN_LANGUAGE
, // ULScript_Greek,
457 RUSSIAN
, // ULScript_Cyrillic,
458 UNKNOWN_LANGUAGE
, // ULScript_Armenian,
459 HEBREW
, // ULScript_Hebrew,
460 ARABIC
, // ULScript_Arabic,
461 UNKNOWN_LANGUAGE
, // ULScript_Syriac,
462 UNKNOWN_LANGUAGE
, // ULScript_Thaana,
463 HINDI
, // ULScript_Devanagari,
464 BENGALI
, // ULScript_Bengali,
465 UNKNOWN_LANGUAGE
, // ULScript_Gurmukhi,
466 UNKNOWN_LANGUAGE
, // ULScript_Gujarati,
467 UNKNOWN_LANGUAGE
, // ULScript_Oriya,
468 UNKNOWN_LANGUAGE
, // ULScript_Tamil,
469 UNKNOWN_LANGUAGE
, // ULScript_Telugu,
470 UNKNOWN_LANGUAGE
, // ULScript_Kannada,
471 UNKNOWN_LANGUAGE
, // ULScript_Malayalam,
472 UNKNOWN_LANGUAGE
, // ULScript_Sinhala,
473 UNKNOWN_LANGUAGE
, // ULScript_Thai,
474 UNKNOWN_LANGUAGE
, // ULScript_Lao,
475 TIBETAN
, // ULScript_Tibetan,
476 UNKNOWN_LANGUAGE
, // ULScript_Myanmar,
477 UNKNOWN_LANGUAGE
, // ULScript_Georgian,
478 CHINESE
, // ULScript_HanCJK,
479 AMHARIC
, // ULScript_Ethiopic,
480 UNKNOWN_LANGUAGE
, // ULScript_Cherokee,
481 UNKNOWN_LANGUAGE
, // ULScript_Canadian_Aboriginal,
482 UNKNOWN_LANGUAGE
, // ULScript_Ogham,
483 UNKNOWN_LANGUAGE
, // ULScript_Runic,
484 UNKNOWN_LANGUAGE
, // ULScript_Khmer,
485 UNKNOWN_LANGUAGE
, // ULScript_Mongolian,
486 UNKNOWN_LANGUAGE
, // ULScript_Yi,
487 UNKNOWN_LANGUAGE
, // ULScript_Old_Italic,
488 UNKNOWN_LANGUAGE
, // ULScript_Gothic,
489 UNKNOWN_LANGUAGE
, // ULScript_Deseret,
490 UNKNOWN_LANGUAGE
, // ULScript_Inherited, [no words should be in this script]
491 UNKNOWN_LANGUAGE
, // ULScript_Tagalog,
492 UNKNOWN_LANGUAGE
, // ULScript_Hanunoo,
493 UNKNOWN_LANGUAGE
, // ULScript_Buhid,
494 UNKNOWN_LANGUAGE
, // ULScript_Tagbanwa,
495 UNKNOWN_LANGUAGE
, // ULScript_Limbu,
496 UNKNOWN_LANGUAGE
, // ULScript_Tai_Le,
497 UNKNOWN_LANGUAGE
, // ULScript_Linear_B,
498 UNKNOWN_LANGUAGE
, // ULScript_Ugaritic,
499 UNKNOWN_LANGUAGE
, // ULScript_Shavian,
500 UNKNOWN_LANGUAGE
, // ULScript_Osmanya,
501 UNKNOWN_LANGUAGE
, // ULScript_Cypriot,
502 UNKNOWN_LANGUAGE
, // ULScript_Buginese,
503 UNKNOWN_LANGUAGE
, // ULScript_Coptic,
504 UNKNOWN_LANGUAGE
, // ULScript_New_Tai_Lue,
505 UNKNOWN_LANGUAGE
, // ULScript_Glagolitic,
506 UNKNOWN_LANGUAGE
, // ULScript_Tifinagh,
507 UNKNOWN_LANGUAGE
, // ULScript_Syloti_Nagri,
508 UNKNOWN_LANGUAGE
, // ULScript_Old_Persian,
509 UNKNOWN_LANGUAGE
, // ULScript_Kharoshthi,
510 UNKNOWN_LANGUAGE
, // ULScript_Balinese,
511 UNKNOWN_LANGUAGE
, // ULScript_Cuneiform,
512 UNKNOWN_LANGUAGE
, // ULScript_Phoenician,
513 UNKNOWN_LANGUAGE
, // ULScript_Phags_Pa,
514 UNKNOWN_LANGUAGE
, // ULScript_Nko,
517 UNKNOWN_LANGUAGE
, // ULScript_Sundanese,
518 UNKNOWN_LANGUAGE
, // ULScript_Lepcha,
519 UNKNOWN_LANGUAGE
, // ULScript_Ol_Chiki,
520 UNKNOWN_LANGUAGE
, // ULScript_Vai,
521 UNKNOWN_LANGUAGE
, // ULScript_Saurashtra,
522 UNKNOWN_LANGUAGE
, // ULScript_Kayah_Li,
523 UNKNOWN_LANGUAGE
, // ULScript_Rejang,
524 UNKNOWN_LANGUAGE
, // ULScript_Lycian,
525 UNKNOWN_LANGUAGE
, // ULScript_Carian,
526 UNKNOWN_LANGUAGE
, // ULScript_Lydian,
527 UNKNOWN_LANGUAGE
, // ULScript_Cham,
530 COMPILE_ASSERT(arraysize(kDefaultLanguagePerLScript
) == ULScript_NUM_SCRIPTS
,
531 kDefaultLanguagePerLScript_has_incorrect_length
);
534 // True for standalone languages (only lang in a script)
535 // Subscripted by packed language number
536 // If 1, we will use nilgrams to determine language
537 static const uint8 kIsStandaloneLang
[EXT_NUM_LANGUAGES
+ 1] = {
539 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, // GREEK
540 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
541 0,1,0,0,1, 0,1,0,0,0, 0,0,1,1,0, 0,0,0,0,1, // MALAYALAM..KANNADA
542 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,1, // PUNJABI..SINHALESE
543 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,1,1,0, // ARMENIAN..LAOTHIAN
545 0,0,0,0,1, 0,1,1,1,0, 1,0,0,0,0, 0,0,0,0,0, // KHMER..ORIYA
546 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
547 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, // INUKTITUT
549 0,0,0,0,0, // [160..164]
550 // Add new language standalone bit just before here
551 0,0,0,0,0, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,
552 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,
557 // True for ULScript_HanCJK
558 // (Vietnamese and Zhuang also have Latin script quadgrams)
559 // Subscripted by packed language number
560 static const uint8 kIsUnigramLang
[EXT_NUM_LANGUAGES
+ 1] = {
562 0,0,0,0,0, 0,0,0,1,1, 0,0,0,0,0, 0,1,0,0,0, // JAPANESE KOREAN CHINESE
563 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
564 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
565 0,0,0,0,0, 0,1,0,0,1, 0,0,0,0,0, 0,0,0,0,0, // VIETNAMESE CHINESE_T
566 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
568 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
569 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
570 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, // ZHUANG
572 0,0,0,0,0, // [160..164]
573 // Add new language unigram bit just before here
575 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
576 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //
582 // True for ULScript_HanCJK
583 // Subscripted by lscript number
584 static const uint8 kScoreUniPerLScript
[] = {
585 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,
586 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
590 COMPILE_ASSERT(arraysize(kScoreUniPerLScript
) == ULScript_NUM_SCRIPTS
,
591 kScoreUniPerLScript_has_incorrect_length
);
594 // Defines Top40 packed languages
596 // Tier 0/1 Language enum list (16)
597 // ENGLISH, /*no en_GB,*/ FRENCH, ITALIAN, GERMAN, SPANISH, // E - FIGS
598 // DUTCH, CHINESE, CHINESE_T, JAPANESE, KOREAN,
599 // PORTUGUESE, RUSSIAN, POLISH, TURKISH, THAI,
602 // Tier 2 Language enum list (22)
603 // SWEDISH, FINNISH, DANISH, /*no pt-PT,*/ ROMANIAN, HUNGARIAN,
604 // HEBREW, INDONESIAN, CZECH, GREEK, NORWEGIAN,
605 // VIETNAMESE, BULGARIAN, CROATIAN, LITHUANIAN, SLOVAK,
606 // TAGALOG, SLOVENIAN, SERBIAN, CATALAN, LATVIAN,
609 // use SERBO_CROATIAN instead of BOSNIAN, SERBIAN, CROATIAN, MONTENEGRIN(21)
611 // Include IgnoreMe (TG_UNKNOWN_LANGUAGE, 25+1) as a top 40
613 // NOTE: packed, i.e. Language enum + 1
614 static const uint8 kIsPackedTop40
[EXT_NUM_LANGUAGES
+ 1] = {
616 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,0,
617 1,1,1,1,0, 1,0,1,0,0, 0,0,1,1,1, 1,0,0,1,0,
618 0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,1, 1,0,0,0,0,
619 0,0,0,1,0, 0,1,0,1,1, 0,0,0,0,0, 0,0,0,0,0,
620 0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0,
622 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
623 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
624 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
626 0,0,0,0,0, // [160..164]
627 // Add new language top40 bit just before here
629 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
630 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
637 // Table has 234 eight-byte entries. Each entry has a five-byte array and
638 // a three-byte array of log base 2 probabilities in the range 0..11.
639 // The intended use is to express five or three probabilities in a single-byte
640 // subscript, then decode via this table. These probabilities are
641 // intended to go with an array of five or three language numbers.
643 // The corresponding language numbers will have to be sorted by descending
644 // probability, then the actual probability subscript chosen to match the
645 // closest available entry in this table.
647 // Pattern of probability values:
648 // hi 3/4 1/2 1/4 lo hi mid lo
649 // where "3/4" is (hi*3+lo)/4, "1/2" is (hi+lo)/2, and "1/4" is (hi+lo*3)/4 and
650 // mid is one of 3/4 1/2 or 1/4.
651 // There are three groups of 78 (=12*13/2) entries, with hi running 0..11 and
652 // lo running 0..hi. Only the first group is used for five-entry lookups.
653 // The mid value in the first group is 1/2, the second group 3/4, and the
654 // third group 1/4. For three-entry lookups, this allows the mid entry to be
655 // somewhat higher or lower than the midpoint, to allow a better match to the
656 // original probabilities.
657 static const int kLgProbV2TblSize
= 234;
658 static const uint8 kLgProbV2Tbl
[kLgProbV2TblSize
* 8] = {
659 1,1,1,1,1, 1,1,1, // [0]
660 2,2,2,1,1, 2,2,1, // [1]
662 3,3,2,2,1, 3,2,1, // [3]
665 4,3,3,2,1, 4,3,1, // [6]
669 5,4,3,2,1, 5,3,1, // [10]
674 6,5,4,2,1, 6,4,1, // [15]
680 7,6,4,3,1, 7,4,1, // [21]
687 8,6,5,3,1, 8,5,1, // [28]
695 9,7,5,3,1, 9,5,1, // [36]
704 10,8,6,3,1, 10,6,1, // [45]
712 10,10,10,9,9, 10,10,9,
713 10,10,10,10,10, 10,10,10,
714 11,9,6,4,1, 11,6,1, // [55]
721 11,10,10,9,8, 11,10,8,
722 11,11,10,10,9, 11,10,9,
723 11,11,11,10,10, 11,11,10,
724 11,11,11,11,11, 11,11,11,
725 12,9,7,4,1, 12,7,1, // [66]
731 12,11,10,8,7, 12,10,7,
732 12,11,10,9,8, 12,10,8,
733 12,11,11,10,9, 12,11,9,
734 12,12,11,11,10, 12,11,10,
735 12,12,12,11,11, 12,12,11,
736 12,12,12,12,12, 12,12,12,
790 10,10,9,9,8, 10,10,8,
791 10,10,10,9,9, 10,10,9,
792 10,10,10,10,10, 10,10,10,
797 11,10,8,7,5, 11,10,5,
798 11,10,9,7,6, 11,10,6,
799 11,10,9,8,7, 11,10,7,
800 11,10,10,9,8, 11,10,8,
801 11,11,10,10,9, 11,11,9,
802 11,11,11,10,10, 11,11,10,
803 11,11,11,11,11, 11,11,11,
805 12,10,7,5,2, 12,10,2,
806 12,10,8,5,3, 12,10,3,
807 12,10,8,6,4, 12,10,4,
808 12,10,9,7,5, 12,10,5,
809 12,11,9,8,6, 12,11,6,
810 12,11,10,8,7, 12,11,7,
811 12,11,10,9,8, 12,11,8,
812 12,11,11,10,9, 12,11,9,
813 12,12,11,11,10, 12,12,10,
814 12,12,12,11,11, 12,12,11,
815 12,12,12,12,12, 12,12,12,
870 10,10,10,9,9, 10,9,9,
871 10,10,10,10,10, 10,10,10,
879 11,10,10,9,8, 11,9,8,
880 11,11,10,10,9, 11,10,9,
881 11,11,11,10,10, 11,10,10,
882 11,11,11,11,11, 11,11,11,
889 12,11,10,8,7, 12,8,7,
890 12,11,10,9,8, 12,9,8,
891 12,11,11,10,9, 12,10,9,
892 12,12,11,11,10, 12,11,10,
893 12,12,12,11,11, 12,11,11,
894 12,12,12,12,12, 12,12,12,
897 // Backmap a single desired probability into an entry in kLgProbV2Tbl
898 static const uint8 kLgProbV2TblBackmap
[13] = {
900 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66,
904 // Always advances one UTF-8 character
905 static const uint8 kAdvanceOneChar
[256] = {
906 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
907 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
908 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
909 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
911 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
912 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
913 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
914 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4,
917 // Does not advance past space or cr/lf/nul
918 static const uint8 kAdvanceOneCharButSpace
[256] = {
919 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
920 0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
921 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
922 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
924 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
925 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
926 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
927 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4,
930 // Advances *only* on space or ASCII vowel (or illegal byte)
931 static const uint8 kAdvanceOneCharSpaceVowel
[256] = {
932 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
933 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
934 0,1,0,0,0,1,0,0, 0,1,0,0,0,0,0,1, 0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,
935 0,1,0,0,0,1,0,0, 0,1,0,0,0,0,0,1, 0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,
937 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
938 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
939 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
940 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
943 // Advances *only* on space (or illegal byte)
944 static const uint8 kAdvanceOneCharSpace
[256] = {
945 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
946 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
947 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
948 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
950 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
951 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
952 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
953 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
957 //------------------------------------------------------------------------------
959 //------------------------------------------------------------------------------
960 static inline int minint(int a
, int b
) {return (a
< b
) ? a
: b
;}
961 static inline int maxint(int a
, int b
) {return (a
> b
) ? a
: b
;}
963 // Here to make available for debugging
964 int ReliabilityDelta(int value1
, int value2
, int count
);
965 int ReliabilityMainstream(int topscore
, int len
, int mean_score
);
967 // Returns "0" for too small
968 inline const char* MyExtLanguageCode(Language lang
) {
969 return ExtLanguageCode(lang
);
972 // Map script into Latin, Cyrillic, Arabic, Other. Used in keeping track of
973 // amount of training data for language-script combinations
974 inline int LScript4(UnicodeLScript lscript
) {
975 if (lscript
== ULScript_Latin
) {return 0;}
976 if (lscript
== ULScript_Cyrillic
) {return 1;}
977 if (lscript
== ULScript_Arabic
) {return 2;}
982 // Routines to access 3 or 5 log probabilities in a single byte.
984 // Return address of 8-byte entry[i]
985 inline const uint8
* LgProb2TblEntry(int i
) {
986 return &kLgProbV2Tbl
[i
* 8];
989 // Return one of five probabilities in an entry
991 inline uint8
LgProb5(const uint8
* entry
, int j
) {
995 // Return one of three probabilities in an entry
996 inline uint8
LgProb3(const uint8
* entry
, int j
) {
1002 //------------------------------------------------------------------------------
1003 // Hashing groups of 1/2/4/8 letters, perhaps with spaces or underscores
1004 //------------------------------------------------------------------------------
1006 // Pick up 1..12 bytes and hash them via mask/shift/add. NO pre/post
1007 // OVERSHOOTS up to 3 bytes
1008 uint32
BiHashV25(const char* word_ptr
, int bytecount
);
1010 // Pick up 1..12 bytes plus pre/post space and hash them via mask/shift/add
1011 // UNDERSHOOTS 1 byte, OVERSHOOTS up to 3 bytes
1012 uint32
QuadHashV25(const char* word_ptr
, int bytecount
);
1014 // Pick up 1..12 bytes plus pre/post '_' and hash them via mask/shift/add
1015 // OVERSHOOTS up to 3 bytes
1016 uint32
QuadHashV25Underscore(const char* word_ptr
, int bytecount
);
1019 // Pick up 1..24 bytes plus pre/post space and hash them via mask/shift/add
1020 // UNDERSHOOTS 1 byte, OVERSHOOTS up to 3 bytes
1021 // For runtime use of tables V3
1022 uint64
OctaHash40(const char* word_ptr
, int bytecount
);
1024 uint64
OctaHash40underscore(const char* word_ptr
, int bytecount
);
1027 // From 32-bit gram FP, return hash table subscript and remaining key
1028 inline void QuadFPJustHash(uint32 quadhash
,
1031 uint32
* subscr
, uint32
* hashkey
) {
1032 *subscr
= (quadhash
+ (quadhash
>> 12)) & (bucketcount
- 1);
1033 *hashkey
= quadhash
& keymask
;
1036 // Look up 32-bit gram FP in caller-passed table
1037 // Typical size 256K entries (1.5MB)
1039 inline const uint32
QuadHashV3Lookup4(const cld::CLDTableSummary
* gram_obj
,
1042 uint32 subscr
, hashkey
;
1043 const IndirectProbBucket4
* quadtable
= gram_obj
->kCLDTable
;
1044 uint32 keymask
= gram_obj
->kCLDTableKeyMask
;
1045 int bucketcount
= gram_obj
->kCLDTableSize
;
1046 QuadFPJustHash(quadhash
, keymask
, bucketcount
, &subscr
, &hashkey
);
1047 const IndirectProbBucket4
* bucket_ptr
= &quadtable
[subscr
];
1048 // Four-way associative, 4 compares
1049 if (((hashkey
^ bucket_ptr
->keyvalue
[0]) & keymask
) == 0) {
1050 return bucket_ptr
->keyvalue
[0];
1052 if (((hashkey
^ bucket_ptr
->keyvalue
[1]) & keymask
) == 0) {
1053 return bucket_ptr
->keyvalue
[1];
1055 if (((hashkey
^ bucket_ptr
->keyvalue
[2]) & keymask
) == 0) {
1056 return bucket_ptr
->keyvalue
[2];
1058 if (((hashkey
^ bucket_ptr
->keyvalue
[3]) & keymask
) == 0) {
1059 return bucket_ptr
->keyvalue
[3];
1065 // Map 40 bits to subscript, hashkey, expected 18-22 bit subscript (min 16)
1066 // wwwwwwww xxxxxxxx xxxxxxxx yyyyyyyy yyyyyyyy
1067 // + ........ ....wwww wwwwxxxx xxxxxxxx xxxxyyyy
1068 // 00000000 00000000 00000011 11111111 11111111 (18-bit bucketcount-1)
1071 // wwwwxxxx xxxxxxxx xxxx.... ........ (20-bit keymask)
1072 // 12-bit shift in subscript mixes in ~4 letters x 4 bits each
1074 // From 40-bit gram FP, return hash table subscript and remaining key
1075 inline void OctaFPJustHash(uint64 longwordhash
,
1078 uint32
* subscr
, uint32
* hashkey
) {
1079 uint32 temp
= (longwordhash
+ (longwordhash
>> 12)) & (bucketcount
- 1);
1081 temp
= longwordhash
>> 4;
1082 *hashkey
= temp
& keymask
;
1085 // Look up 40-bit gram FP in caller-passed table
1086 // Typical size 256K-4M entries (1-16MB)
1087 // 24-12 bit hashkey packed with 8-20 bit indirect lang/probs
1088 // keymask is 0xfffff000 for 20-bit hashkey and 12-bit indirect
1089 inline const uint32
OctaHashV3Lookup4(const cld::CLDTableSummary
* gram_obj
,
1090 uint64 longwordhash
) {
1091 uint32 subscr
, hashkey
;
1092 const IndirectProbBucket4
* octatable
= gram_obj
->kCLDTable
;
1093 uint32 keymask
= gram_obj
->kCLDTableKeyMask
;
1094 int bucketcount
= gram_obj
->kCLDTableSize
;
1095 OctaFPJustHash(longwordhash
, keymask
, bucketcount
,
1097 const IndirectProbBucket4
* bucket_ptr
= &octatable
[subscr
];
1098 // Four-way associative, 4 compares
1099 if (((hashkey
^ bucket_ptr
->keyvalue
[0]) & keymask
) == 0) {
1100 return bucket_ptr
->keyvalue
[0];
1102 if (((hashkey
^ bucket_ptr
->keyvalue
[1]) & keymask
) == 0) {
1103 return bucket_ptr
->keyvalue
[1];
1105 if (((hashkey
^ bucket_ptr
->keyvalue
[2]) & keymask
) == 0) {
1106 return bucket_ptr
->keyvalue
[2];
1108 if (((hashkey
^ bucket_ptr
->keyvalue
[3]) & keymask
) == 0) {
1109 return bucket_ptr
->keyvalue
[3];
1116 //------------------------------------------------------------------------------
1117 // Scoring single groups of letters
1118 //------------------------------------------------------------------------------
1120 // UNIGRAM score one => tote
1121 // Input: 1-byte entry of subscript into unigram probs, plus
1122 // an accumulator tote.
1123 // Output: running sums in tote updated
1124 void ProcessProbV25UniTote(int propval
, Tote
* tote
);
1126 // BIGRAM, QUADGRAM, OCTAGRAM score one => tote
1127 // Input: 4-byte entry of 3 language numbers and one probability subscript,
1128 // plus an accumulator tote. (language 0 means unused entry)
1129 // Output: running sums in tote updated
1130 void ProcessProbV25Tote(uint32 probs
, Tote
* tote
);
1133 //------------------------------------------------------------------------------
1134 // Routines to accumulate probabilities
1135 //------------------------------------------------------------------------------
1137 // Score up to n=gram_limit unigrams, returning number of bytes consumed
1138 // Caller supplies table, such as compact_lang_det_generated_ctjkvz_b1_obj
1139 int DoUniScoreV3(const UTF8PropObj
* unigram_obj
,
1140 const char* isrc
, int srclen
, int advance_by
,
1141 int* tote_grams
, int gram_limit
, Tote
* chunk_tote
);
1144 // Score all words in isrc, using languages that have bigrams (CJK)
1145 // Caller supplies table, such as &kCjkBiTable_obj or &kGibberishTable_obj
1146 // Return number of bigrams that hit in the hash table
1147 int DoBigramScoreV3(const cld::CLDTableSummary
* bigram_obj
,
1148 const char* isrc
, int srclen
, Tote
* chunk_tote
);
1151 // Score up to n=gram_limit quadgrams, returning number of bytes consumed
1152 // Caller supplies table, such as &kQuadTable_obj or &kGibberishTable_obj
1153 int DoQuadScoreV3(const cld::CLDTableSummary
* quadgram_obj
,
1154 const char* isrc
, int srclen
, int advance_by
,
1155 int* tote_grams
, int gram_limit
, Tote
* chunk_tote
);
1157 // Score all octagrams (words) in isrc, using languages that have quadgrams
1158 // Caller supplies table, such as &kLongWord8Table_obj
1159 // Return number of words that hit in the hash table
1160 int DoOctaScoreV3(const cld::CLDTableSummary
* octagram_obj
,
1161 const char* isrc
, int srclen
, Tote
* chunk_tote
);
1163 //------------------------------------------------------------------------------
1164 // Reliability calculations, for single language and between languages
1165 //------------------------------------------------------------------------------
1167 // Reliability = 0..100
1168 static const int kMinReliable
= 75;
1170 // Calculate ratio of score per 1KB vs. expected score per 1KB
1171 double GetNormalizedScore(Language lang
, UnicodeLScript lscript
,
1172 int bytes
, int score
);
1174 // Calculate reliablity of len bytes of script lscript with chunk_tote
1175 int GetReliability(int len
, UnicodeLScript lscript
, const Tote
* chunk_tote
);
1178 //------------------------------------------------------------------------------
1180 //------------------------------------------------------------------------------
1182 // Make languages packed into uint32 values non-zero
1183 // These routines later could remap so languages not in QuadHash tables are not
1184 // represented, and so that any thrashing in accumulation is eliminated
1185 uint8
inline PackLanguage(Language lang
) {
1186 return static_cast<uint8
>(lang
+ 1);}
1188 Language
inline UnpackLanguage(int ilang
) {
1189 return static_cast<Language
>(ilang
- 1);}
1191 // Useful single-byte tests
1192 bool inline IsUTF8ContinueByte(char c
) {
1193 return static_cast<signed char>(c
) < -64;}
1194 bool inline IsUTF8HighByte(char c
) {
1195 return static_cast<signed char>(c
) < 0;}
1198 // Demote all languages except Top40 and plus_one
1199 // Do this just before sorting
1200 void DemoteNotTop40(Tote
* chunk_tote
, int packed_plus_one
);
1202 } // End namespace cld
1205 #endif // ENCODINGS_COMPACT_LANG_DET_CLDUTIL_H_