-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompact_lang_det_impl.cc
2092 lines (1822 loc) · 78.4 KB
/
compact_lang_det_impl.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: [email protected] (Dick Sites)
// Updated 2014.01 for dual table lookup
//
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include "cldutil.h"
#include "debug.h"
#include "integral_types.h"
#include "lang_script.h"
#include "utf8statetable.h"
#ifdef CLD2_DYNAMIC_MODE
#include "cld2_dynamic_data.h"
#include "cld2_dynamic_data_loader.h"
#endif
#include "cld2tablesummary.h"
#include "compact_lang_det_impl.h"
#include "compact_lang_det_hint_code.h"
#include "getonescriptspan.h"
#include "tote.h"
namespace CLD2 {
using namespace std;
// Linker supplies the right tables, From files
// cld_generated_cjk_uni_prop_80.cc cld2_generated_cjk_compatible.cc
// cld_generated_cjk_delta_bi_32.cc generated_distinct_bi_0.cc
// cld2_generated_quad*.cc cld2_generated_deltaocta*.cc
// cld2_generated_distinctocta*.cc
// cld_generated_score_quad_octa_1024_256.cc
// 2014.01 Now implementing quadgram dual lookup tables, to allow main table
// sizes that are 1/3/5 times a power of two, instead of just powers of two.
// Gives more flexibility of total footprint for CLD2.
extern const int kLanguageToPLangSize;
extern const int kCloseSetSize;
extern const UTF8PropObj cld_generated_CjkUni_obj;
extern const CLD2TableSummary kCjkCompat_obj;
extern const CLD2TableSummary kCjkDeltaBi_obj;
extern const CLD2TableSummary kDistinctBiTable_obj;
extern const CLD2TableSummary kQuad_obj;
extern const CLD2TableSummary kQuad_obj2; // Dual lookup tables
extern const CLD2TableSummary kDeltaOcta_obj;
extern const CLD2TableSummary kDistinctOcta_obj;
extern const short kAvgDeltaOctaScore[];
#ifdef CLD2_DYNAMIC_MODE
// CLD2_DYNAMIC_MODE is defined:
// Data will be read from an mmap opened at runtime.
// Convenience for nulling things out completely at any point.
static ScoringTables NULL_TABLES = {
NULL, //&cld_generated_CjkUni_obj,
NULL, //&kCjkCompat_obj,
NULL, //&kCjkDeltaBi_obj,
NULL, //&kDistinctBiTable_obj,
NULL, //&kQuad_obj,
NULL, //&kQuad_obj2,
NULL, //&kDeltaOcta_obj,
NULL, //&kDistinctOcta_obj,
NULL, //kAvgDeltaOctaScore,
};
static ScoringTables kScoringtables = NULL_TABLES; // copy constructed
static bool dynamicDataLoaded = false;
static bool dataSourceIsFile = false;
static ScoringTables* dynamicTables = NULL;
static void* mmapAddress = NULL;
static uint32_t mmapLength = 0;
bool isDataLoaded() { return dynamicDataLoaded; }
bool isDataDynamic() { return true; } // Because CLD2_DYNAMIC_MODE is defined
void loadDataFromFile(const char* fileName) {
if (isDataLoaded()) {
unloadData();
}
ScoringTables* result = CLD2DynamicDataLoader::loadDataFile(fileName, &mmapAddress, &mmapLength);
if (result == NULL) {
fprintf(stderr, "WARNING: Dynamic data loading failed.\n");
return;
}
dynamicTables = result;
kScoringtables = *dynamicTables;
dataSourceIsFile = true;
dynamicDataLoaded = true;
};
void loadDataFromRawAddress(const void* rawAddress, const uint32_t length) {
if (isDataLoaded()) {
unloadData();
}
ScoringTables* result = CLD2DynamicDataLoader::loadDataRaw(rawAddress, length);
if (result == NULL) {
fprintf(stderr, "WARNING: Dynamic data loading failed.\n");
return;
}
dynamicTables = result;
kScoringtables = *dynamicTables;
dataSourceIsFile = false;
dynamicDataLoaded = true;
}
void unloadData() {
if (!dynamicDataLoaded) return;
if (dataSourceIsFile) {
CLD2DynamicDataLoader::unloadDataFile(&dynamicTables, &mmapAddress, &mmapLength);
} else {
CLD2DynamicDataLoader::unloadDataRaw(&dynamicTables);
}
dynamicDataLoaded = false;
dataSourceIsFile = false; // vacuous
kScoringtables = NULL_TABLES; // Housekeeping: null all pointers
}
#else // !CLD2_DYNAMIC_MODE
// This initializes kScoringtables.quadgram_obj etc.
static const ScoringTables kScoringtables = {
&cld_generated_CjkUni_obj,
&kCjkCompat_obj,
&kCjkDeltaBi_obj,
&kDistinctBiTable_obj,
&kQuad_obj,
&kQuad_obj2, // Dual lookup tables
&kDeltaOcta_obj,
&kDistinctOcta_obj,
kAvgDeltaOctaScore,
};
// Method implementations below are provided so that callers aren't *forced*
// to depend upon the CLD2_DYNAMIC_MODE flag, but can use runtime checks
// instead. For more information, refer to CLD2 issue 16:
// https://code.google.com/p/cld2/issues/detail?id=16
bool isDataLoaded() { return true; } // Data is statically linked
bool isDataDynamic() { return false; } // Because CLD2_DYNAMIC_MODE is not defined
void loadDataFromFile(const char* fileName) {
// This is a bug in the calling code.
fprintf(stderr, "WARNING: Dynamic mode not active, loadDataFromFile has no effect!\n");
}
void loadDataFromRawAddress(const void* rawAddress, const uint32_t length) {
// This is a bug in the calling code.
fprintf(stderr, "WARNING: Dynamic mode not active, loadDataFromRawAddress has no effect!\n");
}
void unloadData() {
// This is a bug in the calling code.
fprintf(stderr, "WARNING: Dynamic mode not active, unloadData has no effect!\n");
}
#endif // #ifdef CLD2_DYNAMIC_MODE
static const bool FLAGS_cld_no_minimum_bytes = false;
static const bool FLAGS_cld_forcewords = true;
static const bool FLAGS_cld_showme = false;
static const bool FLAGS_cld_echotext = true;
static const int32 FLAGS_cld_textlimit = 160;
static const int32 FLAGS_cld_smoothwidth = 20;
static const bool FLAGS_cld_2011_hints = true;
static const int32 FLAGS_cld_max_lang_tag_scan_kb = 8;
static const bool FLAGS_dbgscore = false;
static const int kLangHintInitial = 12; // Boost language by N initially
static const int kLangHintBoost = 12; // Boost language by N/16 per quadgram
static const int kShortSpanThresh = 32; // Bytes
static const int kMaxSecondChanceLen = 1024; // Look at first 1K of short spans
static const int kCheapSqueezeTestThresh = 4096; // Only look for squeezing
// after this many text bytes
static const int kCheapSqueezeTestLen = 256; // Bytes to test to trigger sqz
static const int kSpacesTriggerPercent = 25; // Trigger sqz if >=25% spaces
static const int kPredictTriggerPercent = 67; // Trigger sqz if >=67% predicted
static const int kChunksizeDefault = 48; // Squeeze 48-byte chunks
static const int kSpacesThreshPercent = 25; // Squeeze if >=25% spaces
static const int kPredictThreshPercent = 40; // Squeeze if >=40% predicted
static const int kMaxSpaceScan = 32; // Bytes
static const int kGoodLang1Percent = 70;
static const int kGoodLang1and2Percent = 93;
static const int kShortTextThresh = 256; // Bytes
static const int kMinChunkSizeQuads = 4; // Chunk is at least four quads
static const int kMaxChunkSizeQuads = 1024; // Chunk is at most 1K quads
static const int kDefaultWordSpan = 256; // Scan at least this many initial
// bytes with word scoring
static const int kReallyBigWordSpan = 9999999; // Forces word scoring all text
static const int kMinReliableSeq = 50; // Record in seq if >= 50% reliable
static const int kPredictionTableSize = 4096; // Must be exactly 4096 for
// cheap compressor
static const int kNonEnBoilerplateMinPercent = 17; // <this => no second
static const int kNonFIGSBoilerplateMinPercent = 20; // <this => no second
static const int kGoodFirstMinPercent = 26; // <this => UNK
static const int kGoodFirstReliableMinPercent = 51; // <this => unreli
static const int kIgnoreMaxPercent = 20; // >this => unreli
static const int kKeepMinPercent = 2; // <this => unreli
// Statistically closest language, based on quadgram table
// Those that are far from other languges map to UNKNOWN_LANGUAGE
// Subscripted by Language
//
// From lang_correlation.txt and hand-edits
// sed 's/^\([^ ]*\) \([^ ]*\) coef=0\.\(..\).*$/
// (\3 >= kMinCorrPercent) ? \2 : UNKNOWN_LANGUAGE,
// \/\/ \1/' lang_correlation.txt >/tmp/closest_lang_decl.txt
//
static const int kMinCorrPercent = 24; // Pick off how close you want
// 24 catches PERSIAN <== ARABIC
// but not SPANISH <== PORTUGESE
static Language Unknown = UNKNOWN_LANGUAGE;
// Suspect idea
// Subscripted by Language
static const Language kClosestAltLanguage[] = {
(28 >= kMinCorrPercent) ? SCOTS : UNKNOWN_LANGUAGE, // ENGLISH
(36 >= kMinCorrPercent) ? NORWEGIAN : UNKNOWN_LANGUAGE, // DANISH
(31 >= kMinCorrPercent) ? AFRIKAANS : UNKNOWN_LANGUAGE, // DUTCH
(15 >= kMinCorrPercent) ? ESTONIAN : UNKNOWN_LANGUAGE, // FINNISH
(11 >= kMinCorrPercent) ? OCCITAN : UNKNOWN_LANGUAGE, // FRENCH
(17 >= kMinCorrPercent) ? LUXEMBOURGISH : UNKNOWN_LANGUAGE, // GERMAN
(27 >= kMinCorrPercent) ? YIDDISH : UNKNOWN_LANGUAGE, // HEBREW
(16 >= kMinCorrPercent) ? CORSICAN : UNKNOWN_LANGUAGE, // ITALIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // Japanese
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // Korean
(41 >= kMinCorrPercent) ? NORWEGIAN_N : UNKNOWN_LANGUAGE, // NORWEGIAN
( 5 >= kMinCorrPercent) ? SLOVAK : UNKNOWN_LANGUAGE, // POLISH
(23 >= kMinCorrPercent) ? SPANISH : UNKNOWN_LANGUAGE, // PORTUGUESE
(33 >= kMinCorrPercent) ? BULGARIAN : UNKNOWN_LANGUAGE, // RUSSIAN
(28 >= kMinCorrPercent) ? GALICIAN : UNKNOWN_LANGUAGE, // SPANISH
(17 >= kMinCorrPercent) ? NORWEGIAN : UNKNOWN_LANGUAGE, // SWEDISH
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // Chinese
(42 >= kMinCorrPercent) ? SLOVAK : UNKNOWN_LANGUAGE, // CZECH
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // GREEK
(35 >= kMinCorrPercent) ? FAROESE : UNKNOWN_LANGUAGE, // ICELANDIC
( 7 >= kMinCorrPercent) ? LITHUANIAN : UNKNOWN_LANGUAGE, // LATVIAN
( 7 >= kMinCorrPercent) ? LATVIAN : UNKNOWN_LANGUAGE, // LITHUANIAN
( 4 >= kMinCorrPercent) ? LATIN : UNKNOWN_LANGUAGE, // ROMANIAN
( 4 >= kMinCorrPercent) ? SLOVAK : UNKNOWN_LANGUAGE, // HUNGARIAN
(15 >= kMinCorrPercent) ? FINNISH : UNKNOWN_LANGUAGE, // ESTONIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // Ignore
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // Unknown
(33 >= kMinCorrPercent) ? RUSSIAN : UNKNOWN_LANGUAGE, // BULGARIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CROATIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // SERBIAN
(24 >= kMinCorrPercent) ? SCOTS_GAELIC : UNKNOWN_LANGUAGE, // IRISH
(28 >= kMinCorrPercent) ? SPANISH : UNKNOWN_LANGUAGE, // GALICIAN
( 8 >= kMinCorrPercent) ? INDONESIAN : UNKNOWN_LANGUAGE, // TAGALOG
(29 >= kMinCorrPercent) ? AZERBAIJANI : UNKNOWN_LANGUAGE, // TURKISH
(28 >= kMinCorrPercent) ? RUSSIAN : UNKNOWN_LANGUAGE, // UKRAINIAN
(37 >= kMinCorrPercent) ? MARATHI : UNKNOWN_LANGUAGE, // HINDI
(29 >= kMinCorrPercent) ? BULGARIAN : UNKNOWN_LANGUAGE, // MACEDONIAN
(14 >= kMinCorrPercent) ? ASSAMESE : UNKNOWN_LANGUAGE, // BENGALI
(46 >= kMinCorrPercent) ? MALAY : UNKNOWN_LANGUAGE, // INDONESIAN
( 9 >= kMinCorrPercent) ? INTERLINGUA : UNKNOWN_LANGUAGE, // LATIN
(46 >= kMinCorrPercent) ? INDONESIAN : UNKNOWN_LANGUAGE, // MALAY
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // MALAYALAM
( 4 >= kMinCorrPercent) ? BRETON : UNKNOWN_LANGUAGE, // WELSH
( 8 >= kMinCorrPercent) ? HINDI : UNKNOWN_LANGUAGE, // NEPALI
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // TELUGU
( 3 >= kMinCorrPercent) ? ESPERANTO : UNKNOWN_LANGUAGE, // ALBANIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // TAMIL
(22 >= kMinCorrPercent) ? UKRAINIAN : UNKNOWN_LANGUAGE, // BELARUSIAN
(15 >= kMinCorrPercent) ? SUNDANESE : UNKNOWN_LANGUAGE, // JAVANESE
(19 >= kMinCorrPercent) ? CATALAN : UNKNOWN_LANGUAGE, // OCCITAN
(27 >= kMinCorrPercent) ? PERSIAN : UNKNOWN_LANGUAGE, // URDU
(36 >= kMinCorrPercent) ? HINDI : UNKNOWN_LANGUAGE, // BIHARI
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // GUJARATI
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // THAI
(24 >= kMinCorrPercent) ? PERSIAN : UNKNOWN_LANGUAGE, // ARABIC
(19 >= kMinCorrPercent) ? OCCITAN : UNKNOWN_LANGUAGE, // CATALAN
( 4 >= kMinCorrPercent) ? LATIN : UNKNOWN_LANGUAGE, // ESPERANTO
( 3 >= kMinCorrPercent) ? GERMAN : UNKNOWN_LANGUAGE, // BASQUE
( 9 >= kMinCorrPercent) ? LATIN : UNKNOWN_LANGUAGE, // INTERLINGUA
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // KANNADA
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // PUNJABI
(24 >= kMinCorrPercent) ? IRISH : UNKNOWN_LANGUAGE, // SCOTS_GAELIC
( 7 >= kMinCorrPercent) ? KINYARWANDA : UNKNOWN_LANGUAGE, // SWAHILI
(28 >= kMinCorrPercent) ? SERBIAN : UNKNOWN_LANGUAGE, // SLOVENIAN
(37 >= kMinCorrPercent) ? HINDI : UNKNOWN_LANGUAGE, // MARATHI
( 3 >= kMinCorrPercent) ? ITALIAN : UNKNOWN_LANGUAGE, // MALTESE
( 1 >= kMinCorrPercent) ? YORUBA : UNKNOWN_LANGUAGE, // VIETNAMESE
(15 >= kMinCorrPercent) ? DUTCH : UNKNOWN_LANGUAGE, // FRISIAN
(42 >= kMinCorrPercent) ? CZECH : UNKNOWN_LANGUAGE, // SLOVAK
// Original ( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // ChineseT
(24 >= kMinCorrPercent) ? CHINESE : UNKNOWN_LANGUAGE, // ChineseT
(35 >= kMinCorrPercent) ? ICELANDIC : UNKNOWN_LANGUAGE, // FAROESE
(15 >= kMinCorrPercent) ? JAVANESE : UNKNOWN_LANGUAGE, // SUNDANESE
(17 >= kMinCorrPercent) ? TAJIK : UNKNOWN_LANGUAGE, // UZBEK
( 7 >= kMinCorrPercent) ? TIGRINYA : UNKNOWN_LANGUAGE, // AMHARIC
(29 >= kMinCorrPercent) ? TURKISH : UNKNOWN_LANGUAGE, // AZERBAIJANI
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // GEORGIAN
( 7 >= kMinCorrPercent) ? AMHARIC : UNKNOWN_LANGUAGE, // TIGRINYA
(27 >= kMinCorrPercent) ? URDU : UNKNOWN_LANGUAGE, // PERSIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // BOSNIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // SINHALESE
(41 >= kMinCorrPercent) ? NORWEGIAN : UNKNOWN_LANGUAGE, // NORWEGIAN_N
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // PORTUGUESE_P
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // PORTUGUESE_B
(37 >= kMinCorrPercent) ? ZULU : UNKNOWN_LANGUAGE, // XHOSA
(37 >= kMinCorrPercent) ? XHOSA : UNKNOWN_LANGUAGE, // ZULU
( 2 >= kMinCorrPercent) ? SPANISH : UNKNOWN_LANGUAGE, // GUARANI
(29 >= kMinCorrPercent) ? TSWANA : UNKNOWN_LANGUAGE, // SESOTHO
( 7 >= kMinCorrPercent) ? TURKISH : UNKNOWN_LANGUAGE, // TURKMEN
( 8 >= kMinCorrPercent) ? KAZAKH : UNKNOWN_LANGUAGE, // KYRGYZ
( 5 >= kMinCorrPercent) ? FRENCH : UNKNOWN_LANGUAGE, // BRETON
( 3 >= kMinCorrPercent) ? GANDA : UNKNOWN_LANGUAGE, // TWI
(27 >= kMinCorrPercent) ? HEBREW : UNKNOWN_LANGUAGE, // YIDDISH
(28 >= kMinCorrPercent) ? SLOVENIAN : UNKNOWN_LANGUAGE, // SERBO_CROATIAN
(12 >= kMinCorrPercent) ? OROMO : UNKNOWN_LANGUAGE, // SOMALI
( 9 >= kMinCorrPercent) ? UZBEK : UNKNOWN_LANGUAGE, // UIGHUR
(15 >= kMinCorrPercent) ? PERSIAN : UNKNOWN_LANGUAGE, // KURDISH
( 6 >= kMinCorrPercent) ? KYRGYZ : UNKNOWN_LANGUAGE, // MONGOLIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // ARMENIAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // LAOTHIAN
( 8 >= kMinCorrPercent) ? URDU : UNKNOWN_LANGUAGE, // SINDHI
(10 >= kMinCorrPercent) ? ITALIAN : UNKNOWN_LANGUAGE, // RHAETO_ROMANCE
(31 >= kMinCorrPercent) ? DUTCH : UNKNOWN_LANGUAGE, // AFRIKAANS
(17 >= kMinCorrPercent) ? GERMAN : UNKNOWN_LANGUAGE, // LUXEMBOURGISH
( 2 >= kMinCorrPercent) ? SCOTS : UNKNOWN_LANGUAGE, // BURMESE
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // KHMER
(45 >= kMinCorrPercent) ? DZONGKHA : UNKNOWN_LANGUAGE, // TIBETAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // DHIVEHI
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CHEROKEE
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // SYRIAC
( 8 >= kMinCorrPercent) ? DUTCH : UNKNOWN_LANGUAGE, // LIMBU
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // ORIYA
(14 >= kMinCorrPercent) ? BENGALI : UNKNOWN_LANGUAGE, // ASSAMESE
(16 >= kMinCorrPercent) ? ITALIAN : UNKNOWN_LANGUAGE, // CORSICAN
( 5 >= kMinCorrPercent) ? INTERLINGUA : UNKNOWN_LANGUAGE, // INTERLINGUE
( 8 >= kMinCorrPercent) ? KYRGYZ : UNKNOWN_LANGUAGE, // KAZAKH
( 4 >= kMinCorrPercent) ? SWAHILI : UNKNOWN_LANGUAGE, // LINGALA
(11 >= kMinCorrPercent) ? RUSSIAN : UNKNOWN_LANGUAGE, // MOLDAVIAN
(19 >= kMinCorrPercent) ? PERSIAN : UNKNOWN_LANGUAGE, // PASHTO
( 5 >= kMinCorrPercent) ? AYMARA : UNKNOWN_LANGUAGE, // QUECHUA
( 5 >= kMinCorrPercent) ? KINYARWANDA : UNKNOWN_LANGUAGE, // SHONA
(17 >= kMinCorrPercent) ? UZBEK : UNKNOWN_LANGUAGE, // TAJIK
(13 >= kMinCorrPercent) ? BASHKIR : UNKNOWN_LANGUAGE, // TATAR
(11 >= kMinCorrPercent) ? SAMOAN : UNKNOWN_LANGUAGE, // TONGA
( 2 >= kMinCorrPercent) ? TWI : UNKNOWN_LANGUAGE, // YORUBA
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CREOLES_AND_PIDGINS_ENGLISH_BASED
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CREOLES_AND_PIDGINS_FRENCH_BASED
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CREOLES_AND_PIDGINS_PORTUGUESE_BASED
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // CREOLES_AND_PIDGINS_OTHER
( 6 >= kMinCorrPercent) ? TONGA : UNKNOWN_LANGUAGE, // MAORI
( 3 >= kMinCorrPercent) ? OROMO : UNKNOWN_LANGUAGE, // WOLOF
( 1 >= kMinCorrPercent) ? MONGOLIAN : UNKNOWN_LANGUAGE, // ABKHAZIAN
( 8 >= kMinCorrPercent) ? SOMALI : UNKNOWN_LANGUAGE, // AFAR
( 5 >= kMinCorrPercent) ? QUECHUA : UNKNOWN_LANGUAGE, // AYMARA
(13 >= kMinCorrPercent) ? TATAR : UNKNOWN_LANGUAGE, // BASHKIR
( 3 >= kMinCorrPercent) ? ENGLISH : UNKNOWN_LANGUAGE, // BISLAMA
(45 >= kMinCorrPercent) ? TIBETAN : UNKNOWN_LANGUAGE, // DZONGKHA
( 4 >= kMinCorrPercent) ? TONGA : UNKNOWN_LANGUAGE, // FIJIAN
( 7 >= kMinCorrPercent) ? INUPIAK : UNKNOWN_LANGUAGE, // GREENLANDIC
( 3 >= kMinCorrPercent) ? AFAR : UNKNOWN_LANGUAGE, // HAUSA
( 3 >= kMinCorrPercent) ? OCCITAN : UNKNOWN_LANGUAGE, // HAITIAN_CREOLE
( 7 >= kMinCorrPercent) ? GREENLANDIC : UNKNOWN_LANGUAGE, // INUPIAK
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // INUKTITUT
( 4 >= kMinCorrPercent) ? HINDI : UNKNOWN_LANGUAGE, // KASHMIRI
(30 >= kMinCorrPercent) ? RUNDI : UNKNOWN_LANGUAGE, // KINYARWANDA
( 2 >= kMinCorrPercent) ? TAGALOG : UNKNOWN_LANGUAGE, // MALAGASY
(17 >= kMinCorrPercent) ? GERMAN : UNKNOWN_LANGUAGE, // NAURU
(12 >= kMinCorrPercent) ? SOMALI : UNKNOWN_LANGUAGE, // OROMO
(30 >= kMinCorrPercent) ? KINYARWANDA : UNKNOWN_LANGUAGE, // RUNDI
(11 >= kMinCorrPercent) ? TONGA : UNKNOWN_LANGUAGE, // SAMOAN
( 1 >= kMinCorrPercent) ? LINGALA : UNKNOWN_LANGUAGE, // SANGO
(32 >= kMinCorrPercent) ? MARATHI : UNKNOWN_LANGUAGE, // SANSKRIT
(16 >= kMinCorrPercent) ? ZULU : UNKNOWN_LANGUAGE, // SISWANT
( 5 >= kMinCorrPercent) ? SISWANT : UNKNOWN_LANGUAGE, // TSONGA
(29 >= kMinCorrPercent) ? SESOTHO : UNKNOWN_LANGUAGE, // TSWANA
( 2 >= kMinCorrPercent) ? ESTONIAN : UNKNOWN_LANGUAGE, // VOLAPUK
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // ZHUANG
( 1 >= kMinCorrPercent) ? MALAY : UNKNOWN_LANGUAGE, // KHASI
(28 >= kMinCorrPercent) ? ENGLISH : UNKNOWN_LANGUAGE, // SCOTS
(15 >= kMinCorrPercent) ? KINYARWANDA : UNKNOWN_LANGUAGE, // GANDA
( 7 >= kMinCorrPercent) ? ENGLISH : UNKNOWN_LANGUAGE, // MANX
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // MONTENEGRIN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // AKAN
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // IGBO
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // MAURITIAN_CREOLE
( 0 >= kMinCorrPercent) ? Unknown : UNKNOWN_LANGUAGE, // HAWAIIAN
};
// COMPILE_ASSERT(arraysize(kClosestAltLanguage) == NUM_LANGUAGES,
// kClosestAltLanguage_has_incorrect_size);
inline bool FlagFinish(int flags) {return (flags & kCLDFlagFinish) != 0;}
inline bool FlagSqueeze(int flags) {return (flags & kCLDFlagSqueeze) != 0;}
inline bool FlagRepeats(int flags) {return (flags & kCLDFlagRepeats) != 0;}
inline bool FlagTop40(int flags) {return (flags & kCLDFlagTop40) != 0;}
inline bool FlagShort(int flags) {return (flags & kCLDFlagShort) != 0;}
inline bool FlagHint(int flags) {return (flags & kCLDFlagHint) != 0;}
inline bool FlagUseWords(int flags) {return (flags & kCLDFlagUseWords) != 0;}
// Defines Top40 packed languages
// Google top 40 languages
//
// Tier 0/1 Language enum list (16)
// ENGLISH, /*no en_GB,*/ FRENCH, ITALIAN, GERMAN, SPANISH, // E - FIGS
// DUTCH, CHINESE, CHINESE_T, JAPANESE, KOREAN,
// PORTUGUESE, RUSSIAN, POLISH, TURKISH, THAI,
// ARABIC,
//
// Tier 2 Language enum list (22)
// SWEDISH, FINNISH, DANISH, /*no pt-PT,*/ ROMANIAN, HUNGARIAN,
// HEBREW, INDONESIAN, CZECH, GREEK, NORWEGIAN,
// VIETNAMESE, BULGARIAN, CROATIAN, LITHUANIAN, SLOVAK,
// TAGALOG, SLOVENIAN, SERBIAN, CATALAN, LATVIAN,
// UKRAINIAN, HINDI,
//
// use SERBO_CROATIAN instead of BOSNIAN, SERBIAN, CROATIAN, MONTENEGRIN(21)
//
// Include IgnoreMe (TG_UNKNOWN_LANGUAGE, 25+1) as a top 40
void DemoteNotTop40(Tote* chunk_tote, uint16 psplus_one) {
// REVISIT
}
void PrintText(FILE* f, Language cur_lang, const string& temp) {
if (temp.size() == 0) {return;}
fprintf(f, "PrintText[%s]%s<br>\n", LanguageName(cur_lang), temp.c_str());
}
//------------------------------------------------------------------------------
// For --cld_html debugging output. Not thread safe
//------------------------------------------------------------------------------
static Language prior_lang = UNKNOWN_LANGUAGE;
static bool prior_unreliable = false;
//------------------------------------------------------------------------------
// End For --cld_html debugging output
//------------------------------------------------------------------------------
// Backscan to word boundary, returning how many bytes n to go back
// so that src - n is non-space ans src - n - 1 is space.
// If not found in kMaxSpaceScan bytes, return 0..3 to a clean UTF-8 boundary
int BackscanToSpace(const char* src, int limit) {
int n = 0;
limit = minint(limit, kMaxSpaceScan);
while (n < limit) {
if (src[-n - 1] == ' ') {return n;} // We are at _X
++n;
}
n = 0;
while (n < limit) {
if ((src[-n] & 0xc0) != 0x80) {return n;} // We are at char begin
++n;
}
return 0;
}
// Forwardscan to word boundary, returning how many bytes n to go forward
// so that src + n is non-space ans src + n - 1 is space.
// If not found in kMaxSpaceScan bytes, return 0..3 to a clean UTF-8 boundary
int ForwardscanToSpace(const char* src, int limit) {
int n = 0;
limit = minint(limit, kMaxSpaceScan);
while (n < limit) {
if (src[n] == ' ') {return n + 1;} // We are at _X
++n;
}
n = 0;
while (n < limit) {
if ((src[n] & 0xc0) != 0x80) {return n;} // We are at char begin
++n;
}
return 0;
}
// This uses a cheap predictor to get a measure of compression, and
// hence a measure of repetitiveness. It works on complete UTF-8 characters
// instead of bytes, because three-byte UTF-8 Indic, etc. text compress highly
// all the time when done with a byte-based count. Sigh.
//
// To allow running prediction across multiple chunks, caller passes in current
// 12-bit hash value and int[4096] prediction table. Caller inits these to 0.
//
// Returns the number of *bytes* correctly predicted, increments by 1..4 for
// each correctly-predicted character.
//
// NOTE: Overruns by up to three bytes. Not a problem with valid UTF-8 text
//
// TODO(dsites) make this use just one byte per UTF-8 char and incr by charlen
int CountPredictedBytes(const char* isrc, int src_len, int* hash, int* tbl) {
int p_count = 0;
const uint8* src = reinterpret_cast<const uint8*>(isrc);
const uint8* srclimit = src + src_len;
int local_hash = *hash;
while (src < srclimit) {
int c = src[0];
int incr = 1;
// Pick up one char and length
if (c < 0xc0) {
// One-byte or continuation byte: 00xxxxxx 01xxxxxx 10xxxxxx
// Do nothing more
} else if ((c & 0xe0) == 0xc0) {
// Two-byte
c = (c << 8) | src[1];
incr = 2;
} else if ((c & 0xf0) == 0xe0) {
// Three-byte
c = (c << 16) | (src[1] << 8) | src[2];
incr = 3;
} else {
// Four-byte
c = (c << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
incr = 4;
}
src += incr;
int p = tbl[local_hash]; // Prediction
tbl[local_hash] = c; // Update prediction
if (c == p) {
p_count += incr; // Count bytes of good predictions
}
local_hash = ((local_hash << 4) ^ c) & 0xfff;
}
*hash = local_hash;
return p_count;
}
// Counts number of spaces; a little faster than one-at-a-time
// Doesn't count odd bytes at end
int CountSpaces4(const char* src, int src_len) {
int s_count = 0;
for (int i = 0; i < (src_len & ~3); i += 4) {
s_count += (src[i] == ' ');
s_count += (src[i+1] == ' ');
s_count += (src[i+2] == ' ');
s_count += (src[i+3] == ' ');
}
return s_count;
}
// Remove words of text that have more than half their letters predicted
// correctly by our cheap predictor, moving the remaining words in-place
// to the front of the input buffer.
//
// To allow running prediction across multiple chunks, caller passes in current
// 12-bit hash value and int[4096] prediction table. Caller inits these to 0.
//
// Return the new, possibly-shorter length
//
// Result Buffer ALWAYS has leading space and trailing space space space NUL,
// if input does
//
int CheapRepWordsInplace(char* isrc, int src_len, int* hash, int* tbl) {
const uint8* src = reinterpret_cast<const uint8*>(isrc);
const uint8* srclimit = src + src_len;
char* dst = isrc;
int local_hash = *hash;
char* word_dst = dst; // Start of next word
int good_predict_bytes = 0;
int word_length_bytes = 0;
while (src < srclimit) {
int c = src[0];
int incr = 1;
*dst++ = c;
if (c == ' ') {
if ((good_predict_bytes * 2) > word_length_bytes) {
// Word is well-predicted: backup to start of this word
dst = word_dst;
if (FLAGS_cld_showme) {
// Mark the deletion point with period
// Don't repeat multiple periods
// Cannot mark with more bytes or may overwrite unseen input
if ((isrc < (dst - 2)) && (dst[-2] != '.')) {
*dst++ = '.';
*dst++ = ' ';
}
}
}
word_dst = dst; // Start of next word
good_predict_bytes = 0;
word_length_bytes = 0;
}
// Pick up one char and length
if (c < 0xc0) {
// One-byte or continuation byte: 00xxxxxx 01xxxxxx 10xxxxxx
// Do nothing more
} else if ((c & 0xe0) == 0xc0) {
// Two-byte
*dst++ = src[1];
c = (c << 8) | src[1];
incr = 2;
} else if ((c & 0xf0) == 0xe0) {
// Three-byte
*dst++ = src[1];
*dst++ = src[2];
c = (c << 16) | (src[1] << 8) | src[2];
incr = 3;
} else {
// Four-byte
*dst++ = src[1];
*dst++ = src[2];
*dst++ = src[3];
c = (c << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
incr = 4;
}
src += incr;
word_length_bytes += incr;
int p = tbl[local_hash]; // Prediction
tbl[local_hash] = c; // Update prediction
if (c == p) {
good_predict_bytes += incr; // Count good predictions
}
local_hash = ((local_hash << 4) ^ c) & 0xfff;
}
*hash = local_hash;
if ((dst - isrc) < (src_len - 3)) {
// Pad and make last char clean UTF-8 by putting following spaces
dst[0] = ' ';
dst[1] = ' ';
dst[2] = ' ';
dst[3] = '\0';
} else if ((dst - isrc) < src_len) {
// Make last char clean UTF-8 by putting following space off the end
dst[0] = ' ';
}
return static_cast<int>(dst - isrc);
}
// This alternate form overwrites redundant words, thus avoiding corrupting the
// backmap for generate a vector of original-text ranges.
int CheapRepWordsInplaceOverwrite(char* isrc, int src_len, int* hash, int* tbl) {
const uint8* src = reinterpret_cast<const uint8*>(isrc);
const uint8* srclimit = src + src_len;
char* dst = isrc;
int local_hash = *hash;
char* word_dst = dst; // Start of next word
int good_predict_bytes = 0;
int word_length_bytes = 0;
while (src < srclimit) {
int c = src[0];
int incr = 1;
*dst++ = c;
if (c == ' ') {
if ((good_predict_bytes * 2) > word_length_bytes) {
// Word [word_dst..dst-1) is well-predicted: overwrite
for (char* p = word_dst; p < dst - 1; ++p) {*p = '.';}
}
word_dst = dst; // Start of next word
good_predict_bytes = 0;
word_length_bytes = 0;
}
// Pick up one char and length
if (c < 0xc0) {
// One-byte or continuation byte: 00xxxxxx 01xxxxxx 10xxxxxx
// Do nothing more
} else if ((c & 0xe0) == 0xc0) {
// Two-byte
*dst++ = src[1];
c = (c << 8) | src[1];
incr = 2;
} else if ((c & 0xf0) == 0xe0) {
// Three-byte
*dst++ = src[1];
*dst++ = src[2];
c = (c << 16) | (src[1] << 8) | src[2];
incr = 3;
} else {
// Four-byte
*dst++ = src[1];
*dst++ = src[2];
*dst++ = src[3];
c = (c << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
incr = 4;
}
src += incr;
word_length_bytes += incr;
int p = tbl[local_hash]; // Prediction
tbl[local_hash] = c; // Update prediction
if (c == p) {
good_predict_bytes += incr; // Count good predictions
}
local_hash = ((local_hash << 4) ^ c) & 0xfff;
}
*hash = local_hash;
if ((dst - isrc) < (src_len - 3)) {
// Pad and make last char clean UTF-8 by putting following spaces
dst[0] = ' ';
dst[1] = ' ';
dst[2] = ' ';
dst[3] = '\0';
} else if ((dst - isrc) < src_len) {
// Make last char clean UTF-8 by putting following space off the end
dst[0] = ' ';
}
return static_cast<int>(dst - isrc);
}
// Remove portions of text that have a high density of spaces, or that are
// overly repetitive, squeezing the remaining text in-place to the front of the
// input buffer.
//
// Squeezing looks at density of space/prediced chars in fixed-size chunks,
// specified by chunksize. A chunksize <= 0 uses the default size of 48 bytes.
//
// Return the new, possibly-shorter length
//
// Result Buffer ALWAYS has leading space and trailing space space space NUL,
// if input does
//
int CheapSqueezeInplace(char* isrc,
int src_len,
int ichunksize) {
char* src = isrc;
char* dst = src;
char* srclimit = src + src_len;
bool skipping = false;
int hash = 0;
// Allocate local prediction table.
int* predict_tbl = new int[kPredictionTableSize];
memset(predict_tbl, 0, kPredictionTableSize * sizeof(predict_tbl[0]));
int chunksize = ichunksize;
if (chunksize == 0) {chunksize = kChunksizeDefault;}
int space_thresh = (chunksize * kSpacesThreshPercent) / 100;
int predict_thresh = (chunksize * kPredictThreshPercent) / 100;
while (src < srclimit) {
int remaining_bytes = srclimit - src;
int len = minint(chunksize, remaining_bytes);
// Make len land us on a UTF-8 character boundary.
// Ah. Also fixes mispredict because we could get out of phase
// Loop always terminates at trailing space in buffer
while ((src[len] & 0xc0) == 0x80) {++len;} // Move past continuation bytes
int space_n = CountSpaces4(src, len);
int predb_n = CountPredictedBytes(src, len, &hash, predict_tbl);
if ((space_n >= space_thresh) || (predb_n >= predict_thresh)) {
// Skip the text
if (!skipping) {
// Keeping-to-skipping transition; do it at a space
int n = BackscanToSpace(dst, static_cast<int>(dst - isrc));
dst -= n;
if (dst == isrc) {
// Force a leading space if the first chunk is deleted
*dst++ = ' ';
}
if (FLAGS_cld_showme) {
// Mark the deletion point with black square U+25A0
*dst++ = static_cast<unsigned char>(0xe2);
*dst++ = static_cast<unsigned char>(0x96);
*dst++ = static_cast<unsigned char>(0xa0);
*dst++ = ' ';
}
skipping = true;
}
} else {
// Keep the text
if (skipping) {
// Skipping-to-keeping transition; do it at a space
int n = ForwardscanToSpace(src, len);
src += n;
remaining_bytes -= n; // Shrink remaining length
len -= n;
skipping = false;
}
// "len" can be negative in some cases
if (len > 0) {
memmove(dst, src, len);
dst += len;
}
}
src += len;
}
if ((dst - isrc) < (src_len - 3)) {
// Pad and make last char clean UTF-8 by putting following spaces
dst[0] = ' ';
dst[1] = ' ';
dst[2] = ' ';
dst[3] = '\0';
} else if ((dst - isrc) < src_len) {
// Make last char clean UTF-8 by putting following space off the end
dst[0] = ' ';
}
// Deallocate local prediction table
delete[] predict_tbl;
return static_cast<int>(dst - isrc);
}
// This alternate form overwrites redundant words, thus avoiding corrupting the
// backmap for generate a vector of original-text ranges.
int CheapSqueezeInplaceOverwrite(char* isrc,
int src_len,
int ichunksize) {
char* src = isrc;
char* dst = src;
char* srclimit = src + src_len;
bool skipping = false;
int hash = 0;
// Allocate local prediction table.
int* predict_tbl = new int[kPredictionTableSize];
memset(predict_tbl, 0, kPredictionTableSize * sizeof(predict_tbl[0]));
int chunksize = ichunksize;
if (chunksize == 0) {chunksize = kChunksizeDefault;}
int space_thresh = (chunksize * kSpacesThreshPercent) / 100;
int predict_thresh = (chunksize * kPredictThreshPercent) / 100;
// Always keep first byte (space)
++src;
++dst;
while (src < srclimit) {
int remaining_bytes = srclimit - src;
int len = minint(chunksize, remaining_bytes);
// Make len land us on a UTF-8 character boundary.
// Ah. Also fixes mispredict because we could get out of phase
// Loop always terminates at trailing space in buffer
while ((src[len] & 0xc0) == 0x80) {++len;} // Move past continuation bytes
int space_n = CountSpaces4(src, len);
int predb_n = CountPredictedBytes(src, len, &hash, predict_tbl);
if ((space_n >= space_thresh) || (predb_n >= predict_thresh)) {
// Overwrite the text [dst-n..dst)
if (!skipping) {
// Keeping-to-skipping transition; do it at a space
int n = BackscanToSpace(dst, static_cast<int>(dst - isrc));
// Text [word_dst..dst) is well-predicted: overwrite
for (char* p = dst - n; p < dst; ++p) {*p = '.';}
skipping = true;
}
// Overwrite the text [dst..dst+len)
for (char* p = dst; p < dst + len; ++p) {*p = '.';}
dst[len - 1] = ' '; // Space at end so we can see what is happening
} else {
// Keep the text
if (skipping) {
// Skipping-to-keeping transition; do it at a space
int n = ForwardscanToSpace(src, len);
// Text [dst..dst+n) is well-predicted: overwrite
for (char* p = dst; p < dst + n - 1; ++p) {*p = '.';}
skipping = false;
}
}
dst += len;
src += len;
}
if ((dst - isrc) < (src_len - 3)) {
// Pad and make last char clean UTF-8 by putting following spaces
dst[0] = ' ';
dst[1] = ' ';
dst[2] = ' ';
dst[3] = '\0';
} else if ((dst - isrc) < src_len) {
// Make last char clean UTF-8 by putting following space off the end
dst[0] = ' ';
}
// Deallocate local prediction table
delete[] predict_tbl;
return static_cast<int>(dst - isrc);
}
// Timing 2.8GHz P4 (dsites 2008.03.20) with 170KB input
// About 90 MB/sec, with or without memcpy, chunksize 48 or 4096
// Just CountSpaces is about 340 MB/sec
// Byte-only CountPredictedBytes is about 150 MB/sec
// Byte-only CountPredictedBytes, conditional tbl[] = is about 85! MB/sec
// Byte-only CountPredictedBytes is about 180 MB/sec, byte tbl, byte/int c
// Unjammed byte-only both = 170 MB/sec
// Jammed byte-only both = 120 MB/sec
// Back to original w/slight updates, 110 MB/sec
//
bool CheapSqueezeTriggerTest(const char* src, int src_len, int testsize) {
// Don't trigger at all on short text
if (src_len < testsize) {return false;}
int space_thresh = (testsize * kSpacesTriggerPercent) / 100;
int predict_thresh = (testsize * kPredictTriggerPercent) / 100;
int hash = 0;
// Allocate local prediction table.
int* predict_tbl = new int[kPredictionTableSize];
memset(predict_tbl, 0, kPredictionTableSize * sizeof(predict_tbl[0]));
bool retval = false;
if ((CountSpaces4(src, testsize) >= space_thresh) ||
(CountPredictedBytes(src, testsize, &hash, predict_tbl) >=
predict_thresh)) {
retval = true;
}
// Deallocate local prediction table
delete[] predict_tbl;
return retval;
}
// Delete any extended languages from doc_tote
void RemoveExtendedLanguages(DocTote* doc_tote) {
// Now a nop
}
static const int kMinReliableKeepPercent = 41; // Remove lang if reli < this
// For Tier3 languages, require a minimum number of bytes to be first-place lang
static const int kGoodFirstT3MinBytes = 24; // <this => no first
// Move bytes for unreliable langs to another lang or UNKNOWN
// doc_tote is sorted, so cannot Add
//
// If both CHINESE and CHINESET are present and unreliable, do not delete both;
// merge both into CHINESE.
//
//dsites 2009.03.19
// we also want to remove Tier3 languages as the first lang if there is very
// little text like ej1 ej2 ej3 ej4
// maybe fold this back in earlier
//
void RemoveUnreliableLanguages(DocTote* doc_tote,
bool FLAGS_cld2_html, bool FLAGS_cld2_quiet) {
// Prepass to merge some low-reliablility languages
// TODO: this shouldn't really reach in to the internal structure of doc_tote
int total_bytes = 0;
for (int sub = 0; sub < doc_tote->MaxSize(); ++sub) {
int plang = doc_tote->Key(sub);
if (plang == DocTote::kUnusedKey) {continue;} // Empty slot
Language lang = static_cast<Language>(plang);
int bytes = doc_tote->Value(sub);
int reli = doc_tote->Reliability(sub);
if (bytes == 0) {continue;} // Zero bytes
total_bytes += bytes;
// Reliable percent = stored reliable score over stored bytecount
int reliable_percent = reli / bytes;
if (reliable_percent >= kMinReliableKeepPercent) {continue;} // Keeper