File: cld2_do_score.cc

package info (click to toggle)
cld2 0.0.0-git20150806-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 120,756 kB
  • ctags: 2,721
  • sloc: cpp: 864,716; sh: 255; ansic: 207; makefile: 17
file content (277 lines) | stat: -rw-r--r-- 8,688 bytes parent folder | download | duplicates (6)
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
// 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.

// Little program to read lines of sample text, calculate score per 1024 bytes
// per language-script4 combination
// Possible input file /export/hda3/cld/pre2010/b0_samp_prune_20100722.utf8

#include <stdio.h>
#include <string.h>
#include <string>

#include "compact_lang_det_impl.h"
#include "lang_script.h"

using namespace std;
using namespace CLD2;

double bytes[NUM_LANGUAGES][4];
double scores[NUM_LANGUAGES][4];


// Return score per 1024 bytes for top language
Language ScoreOneLine(const char* buffer, int buffer_length,
                    int* bytes, double* score_per_1kb) {
  bool is_plain_text = true;
  const CLDHints* cld_hints = NULL;
  bool allow_extended_lang = true;
  int flags = 0;
  Language plus_one = UNKNOWN_LANGUAGE;
  Language language3[3];
  int percent3[3];
  double normalized_score3[3];
  ResultChunkVector* resultchunkvector = NULL;
  int text_bytes;
  bool is_reliable;
  Language summary_lang;

  summary_lang = DetectLanguageSummaryV2(
                        buffer,
                        buffer_length,
                        is_plain_text,
                        cld_hints,
                        allow_extended_lang,
                        flags,
                        plus_one,
                        language3,
                        percent3,
                        normalized_score3,
                        resultchunkvector,
                        &text_bytes,
                        &is_reliable);
  *bytes = text_bytes;
  *score_per_1kb = normalized_score3[0];
  return language3[0];
}

#define LF 0x0a
#define CR 0x0d
const int kMaxBuffer = 5 * 1024;

bool ReadLine(FILE* infile, char* buffer, size_t maxlen) {
  char* p = fgets(buffer, maxlen, infile);
  if (p == NULL) {
    return false;
  }
  int len = strlen(buffer);

  // trim CR LF
  if (buffer[len-1] == LF) {buffer[--len] = '\0';}
  if (buffer[len-1] == CR) {buffer[--len] = '\0';}
  return true;
}

bool IsComment(const char* buffer) {
  int len = strlen(buffer);
  if (len == 0) {return true;}
  if (buffer[0] == '#') {return true;}
  if (buffer[0] == ' ') {return true;}    // Any leading space is comment
  return false;
}

// Skips over xxxxx_ where _ is one or more spaces/tabs
// Returns string::npos if no more fields
int SkipOneField(const string& src, int pos) {
  if (pos == string::npos) {return pos;}

  int lpos = pos;
  lpos = src.find_first_of(" \t", lpos);
  if (lpos == string::npos) {return lpos;}
  lpos = src.find_first_not_of(" \t", lpos);
  if (lpos == string::npos) {return lpos;}
  return lpos;
}

// Return language and script from parsed line or defaults
void GetLangScript(const string& src,
                   Language default_lang, ULScript default_lscript,
                   Language* target_lang, ULScript* target_lscript,
                   string* tld) {
  *target_lang = default_lang;
  *target_lscript = default_lscript;
  *tld = "";
  int pos = 0;
  int pos2 = 0;
  if (src.substr(0,7) == "SAMPLE ") {
    // SAMPLE ll-Ssss
    pos = SkipOneField(src, pos);
  } else if (src.substr(0,5) == "SAMP ") {
    // SAMP ll-Ssss /tld2.tld/
    pos = SkipOneField(src, pos);
    pos2 = SkipOneField(src, pos);
  } else if (src.substr(0,5) == "Samp ") {
    // Samp ll-Ssss /tld2.tld/
    pos = SkipOneField(src, pos);
    pos2 = SkipOneField(src, pos);
  }
  if (pos == 0) {return;}
  if (pos == string::npos) {return;}

  // Pos is at the first letter of language-script combination
  int end = src.find_first_of(" \t", pos);    // find end of lang-script
  if (end == string::npos) {return;}
  *target_lang = GetLanguageFromName(src.substr(pos, end - pos).c_str());
  *target_lscript = GetULScriptFromName(src.substr(pos, end - pos).c_str());

  // Pos2 is 0 or at the first letter of the tld string
  if (pos2 == 0) {return;}
  if (pos2 == string::npos) {return;}
  end = src.find_first_of(" \t", pos2);
  if (end == string::npos) {return;}
  *tld = src.substr(pos2, end - pos2);
}

// Return position of start of text
int GetTextBeginPos(const string& src) {
  int pos = 0;
  if (src.size() < 8) {return pos;}

  if (src.substr(0,7) == "SAMPLE ") {
    // Skip SAMPLE ll-Ssss
    pos = SkipOneField(src, pos);
    pos = SkipOneField(src, pos);
  } else if (src.substr(0,5) == "SAMP ") {
    // Skip SAMP ll-Ssss /tld2.tld/
    pos = SkipOneField(src, pos);
    pos = SkipOneField(src, pos);
    pos = SkipOneField(src, pos);
  } else if (src.substr(0,5) == "Samp ") {
    // Skip Samp ll-Ssss /tld2.tld/
    pos = SkipOneField(src, pos);
    pos = SkipOneField(src, pos);
    pos = SkipOneField(src, pos);
  }
  return pos;
}

// Avoid zdiv
inline double Divisor(double x) {
   return (x > 0.0 ? x : 1.0);
}

void Flush(Language cur_lang, ULScript ulscript,
           double total_score_cur_lang,
           double total_bytes_cur_lang, double total_bad_bytes_cur_lang) {
  if (cur_lang == UNKNOWN_LANGUAGE) {return;}

  bytes[cur_lang][LScript4(ulscript)] += total_bytes_cur_lang;
  scores[cur_lang][LScript4(ulscript)] += total_score_cur_lang;

  double score = total_score_cur_lang * 1024.0 / Divisor(total_bytes_cur_lang);
  double percent_bad = 100.0 * total_bad_bytes_cur_lang /
     Divisor(total_bytes_cur_lang + total_bad_bytes_cur_lang);
  fprintf(stdout, "%s-%s    %7.0f %6.1f, %2.0f%% bad SUMMARY\n\n",
          LanguageCode(cur_lang),
          ULScriptCode(ulscript),
          total_bytes_cur_lang,
          score,
          percent_bad);
}

int BytesPer1KB(int i, int j) {
   int bytes_per_1kb = ((scores[i][j] * 1024.0) / Divisor(bytes[i][j])) + 0.5;
   return bytes_per_1kb;
}

int main(int argc, char *argv[]) {
  Language cur_lang = UNKNOWN_LANGUAGE;
  ULScript cur_ulscript = ULScript_Common;
  double total_score_cur_lang = 0.0;
  double total_bytes_cur_lang = 0.0;
  double total_bad_bytes_cur_lang = 0.0;
  memset(bytes, 0, sizeof(bytes));
  memset(scores, 0, sizeof(bytes));

  char buffer[kMaxBuffer];
  int buffer_length;
  const char* filename = NULL;
  FILE* infile = stdin;
  for (int i = 1; i < argc; ++i) {
     if (argv[i][0] != '-') {
        filename = argv[i];
     }
  }

  if (filename != NULL) {
     infile = fopen(filename, "r");
     if (infile == NULL) {
        fprintf(stderr, "%s did not open\n", filename);
        return 0;
     }
  }

  while (ReadLine(infile, buffer, kMaxBuffer)) {
    if (IsComment(buffer)) {continue;}

    buffer_length = strlen(buffer);
    int bytes;
    double score_per_1kb;
    Language toplang;
    Language target_lang;
    ULScript target_ulscript;

    string src(buffer, buffer_length);
    string tld("");
    int pos = GetTextBeginPos(src);
    GetLangScript(src, UNKNOWN_LANGUAGE, ULScript_Common,
                   &target_lang, &target_ulscript, &tld);
    if ((cur_lang != target_lang) || (cur_ulscript != target_ulscript)) {
      Flush(cur_lang, cur_ulscript, total_score_cur_lang,
            total_bytes_cur_lang, total_bad_bytes_cur_lang);
      cur_lang = target_lang;
      cur_ulscript = target_ulscript;
      total_score_cur_lang = 0.0;
      total_bytes_cur_lang = 0.0;
      total_bad_bytes_cur_lang = 0.0;
    }

    toplang = ScoreOneLine(&src[pos], src.size() - pos, &bytes, &score_per_1kb);

    fprintf(stdout, "%s%c %d %4.1f %s\n",
            LanguageCode(toplang),
            (toplang == target_lang) ? ' ' : '*',
            bytes, score_per_1kb, buffer);
    // Only count when detected lang matches the claimed target lang
    if (toplang == target_lang) {
      total_bytes_cur_lang += bytes;
      total_score_cur_lang += (score_per_1kb * bytes) / 1024.0;
    } else {
      total_bad_bytes_cur_lang += bytes;
    }
  }
  Flush(cur_lang, cur_ulscript, total_score_cur_lang,
        total_bytes_cur_lang, total_bad_bytes_cur_lang);

  for (int i = 0; i < NUM_LANGUAGES; ++i) {
     Language ilang = static_cast<Language>(i);
     fprintf(stdout, "  {%4d, %4d, %4d, %4d},  // %d %s %s\n",
             BytesPer1KB(i, 0), BytesPer1KB(i, 1),
             BytesPer1KB(i, 2), BytesPer1KB(i, 3),
             i, LanguageName(ilang), LanguageCode(ilang));
  }

  if (infile != stdin) {
     fclose(infile);
  }
}