-
Notifications
You must be signed in to change notification settings - Fork 76
/
LanguageDetector.cs
471 lines (386 loc) · 14.8 KB
/
LanguageDetector.cs
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
using UID;
using Mosaik.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO.Compression;
using System.Threading;
namespace Catalyst.Models
{
public class LanguageDetectorModel : StorableObjectData
{
public Dictionary<int, Dictionary<Language, double>> WordLanguageProbabilities { get; set; } = new Dictionary<int, Dictionary<Language, double>>();
public List<Language> Languages { get; set; } = new List<Language>();
}
public class LanguageDetector : StorableObject<LanguageDetector, LanguageDetectorModel>, IProcess
{
private static ObjectPool<List<int>> _listPool = new ObjectPool<List<int>>(() => new List<int>(), Environment.ProcessorCount, l => l.Clear());
public double Alpha { get; set; } = 0.5;
public int Trials { get; set; } = 7;
public int NGramLength { get; set; } = 3;
public int MaxTextLength { get; set; } = 10000;
public double AlphaWidth { get; set; } = 0.05;
public int MaxIterations { get; set; } = 1000;
public double ProbabilityThreshold { get; set; } = 0.1;
public double ConvergenceThreshold { get; set; } = 0.99999;
public int BaseFrequency { get; set; } = 10000;
public LanguageDetector(int version, string tag = "") : base(Language.Any, version, tag, compress: true)
{
}
public new static async Task<LanguageDetector> FromStoreAsync(Language language, int version, string tag)
{
var a = new LanguageDetector(version, tag);
try
{
using var sr1 = typeof(LanguageDetector).Assembly.GetManifestResourceStream($"Catalyst.Resources.LanguageDetector.binz");
using var decompressed = new MemoryStream();
using (var ds = new DeflateStream(sr1, CompressionMode.Decompress, leaveOpen: true))
{
await ds.CopyToAsync(decompressed);
decompressed.Seek(0, SeekOrigin.Begin);
a.Data = MessagePack.MessagePackSerializer.Deserialize<LanguageDetectorModel>(decompressed, Pipeline.LZ4Standard);
a.Version = 0;
}
}
catch
{
await a.LoadDataAsync();
}
return a;
}
public void Process(IDocument document, CancellationToken cancellationToken = default)
{
Detect(document);
}
public void Detect(IDocument doc)
{
doc.Language = Detect(doc.Value);
}
public Language Detect(string text)
{
var languages = DetectAll(text);
return languages.Count > 0 ? languages[0].Language : Language.Unknown;
}
public IList<DetectedLanguage> DetectAll(string text)
{
if (string.IsNullOrWhiteSpace(text)) { return Array.Empty<DetectedLanguage>(); }
var ngrams = ExtractNGrams(NormalizeText(text));
try
{
if (ngrams.Count == 0) { return Array.Empty<DetectedLanguage>(); }
Span<double> languageProbabilities = Data.Languages.Count < 256 ? stackalloc double[Data.Languages.Count] : new double[Data.Languages.Count];
for (int t = 0; t < Trials; t++)
{
var probs = InitializeProbabilities();
double alpha = Alpha + ThreadSafeFastRandom.NextDouble() * AlphaWidth;
for (int i = 0; ; i++)
{
int r = ThreadSafeFastRandom.Next(ngrams.Count);
UpdateProbabilities(probs, ngrams[r], alpha);
if (i % 5 == 0 && (NormalizeProbabilities(probs) > ConvergenceThreshold || i >= MaxIterations)) { break; }
}
for (int j = 0; j < languageProbabilities.Length; j++) { languageProbabilities[j] += probs[j] / Trials; }
}
return SortProbabilities(languageProbabilities);
}
finally
{
_listPool.Return(ngrams);
}
}
private List<int> ExtractNGrams(string text)
{
var hashes = _listPool.Rent();
if (string.IsNullOrEmpty(text)) { return hashes; }
var ngram = new NGram();
foreach (char c in text)
{
ngram.Add(c);
for (int n = 1; n <= NGram.N_GRAM; n++)
{
var w = ngram.Get(n);
if (w.Length > 0)
{
int hash = GetHash(w);
if (Data.WordLanguageProbabilities.ContainsKey(hash))
{
hashes.Add(hash);
}
}
}
}
return hashes;
}
private static int GetHash(ReadOnlySpan<char> lowerCaseFeaturesWithoutSpaces)
{
return Hashes.CaseSensitiveHash32(lowerCaseFeaturesWithoutSpaces);
}
#region Normalize text
private static readonly Regex RE_Numbers = new Regex(@"([\w+\-!?\\\/\(\)\[\]\{\},.:;=$%&]*\d[\w\d+\-!?\\\/\(\)\[\]\{\},.:;=$%&]+)", RegexOptions.Compiled);
private static readonly Regex UrlRegex = new Regex("https?://[-_.?&~;+=/#0-9A-Za-z]{1,2076}", RegexOptions.Compiled);
private static readonly Regex EmailRegex = new Regex("[-_.0-9A-Za-z]{1,64}@[-_0-9A-Za-z]{1,255}[-_.0-9A-Za-z]{1,255}", RegexOptions.Compiled);
private string NormalizeText(string text)
{
text = NormalizeWhitespace(text);
if (text.Length > MaxTextLength) { text = text.Substring(0, MaxTextLength); }
text = RemoveAddresses(text);
text = RemoveNumbers(text);
text = NormalizeAlphabet(text);
text = NormalizeVietnamese(text);
text = NormalizeWhitespace(text);
return text;
}
private string RemoveNumbers(string text)
{
return RE_Numbers.Replace(text, " ");
}
private static string NormalizeAlphabet(string text)
{
int latinCount = 0;
int nonLatinCount = 0;
for (int i = 0; i < text.Length; ++i)
{
char c = text[i];
if (c <= 'z' && c >= 'A')
{
++latinCount;
}
else if (c >= '\u0300' && !(c >= 0x1e00 && c <= 0x1eff))
{
++nonLatinCount;
}
}
if (latinCount * 2 < nonLatinCount)
{
StringBuilder textWithoutLatin = new StringBuilder();
for (int i = 0; i < text.Length; ++i)
{
char c = text[i];
if (c > 'z' || c < 'A')
textWithoutLatin.Append(c);
}
text = textWithoutLatin.ToString();
}
return text;
}
private static string NormalizeVietnamese(string text)
{
// todo
return text;
}
private static string NormalizeWhitespace(string text)
{
var sb = Pools.StringBuilder.Rent();
bool prevIsSpace = false;
foreach (char c in text)
{
if(char.IsWhiteSpace(c))
{
if(!prevIsSpace)
{
sb.Append(' ');
}
prevIsSpace = true;
}
else
{
sb.Append(c);
prevIsSpace = false;
}
}
var final = sb.ToString();
Pools.StringBuilder.Return(sb);
return final;
}
private static string RemoveAddresses(string text)
{
text = UrlRegex.Replace(text, " ");
text = EmailRegex.Replace(text, " ");
return text;
}
#endregion Normalize text
#region Probabilities
private double[] InitializeProbabilities()
{
double[] prob = new double[Data.Languages.Count];
for (int i = 0; i < prob.Length; i++) { prob[i] = 1.0 / Data.Languages.Count; }
return prob;
}
private void UpdateProbabilities(double[] prob, int hashedWord, double alpha)
{
if (!Data.WordLanguageProbabilities.ContainsKey(hashedWord)) { return; }
var languageProbabilities = Data.WordLanguageProbabilities[hashedWord];
double weight = alpha / BaseFrequency;
for (int i = 0; i < prob.Length; i++)
{
var profile = Data.Languages[i];
prob[i] *= weight + (languageProbabilities.ContainsKey(profile) ? languageProbabilities[profile] : 0);
}
}
private static double NormalizeProbabilities(double[] probs)
{
double maxp = 0, sump = 0;
for (int i = 0; i < probs.Length; ++i) { sump += probs[i]; }
for (int i = 0; i < probs.Length; ++i)
{
double p = probs[i] / sump;
if (maxp < p) maxp = p;
probs[i] = p;
}
return maxp;
}
private IList<DetectedLanguage> SortProbabilities(Span<double> probs)
{
List<DetectedLanguage> list = new List<DetectedLanguage>();
for (int j = 0; j < probs.Length; j++)
{
double p = probs[j];
if (p > ProbabilityThreshold)
{
for (int i = 0; i <= list.Count; i++)
{
if (i == list.Count || list[i].Probability < p)
{
list.Insert(i, new DetectedLanguage { Language = Data.Languages[j], Probability = p });
break;
}
}
}
}
return list;
}
#endregion Probabilities
public struct DetectedLanguage
{
public Language Language { get; set; }
public double Probability { get; set; }
}
private class NGram
{
public const int N_GRAM = 3;
private readonly char[] buffer;
private int length;
private bool capital = false;
public NGram()
{
buffer = new char[N_GRAM];
buffer[0] = ' ';
length = 1;
}
public void Add(char c)
{
char lastChar = buffer[length - 1];
if (lastChar == ' ')
{
buffer[0] = ' ';
length = 1;
capital = false;
if (c == ' ') return;
}
else if (length == N_GRAM)
{
buffer[0] = buffer[1];
buffer[1] = buffer[2];
length = 2;
}
buffer[length] = char.ToLowerInvariant(c);
length++;
if (char.IsUpper(c))
{
if (char.IsUpper(lastChar))
{
capital = true;
}
}
else
{
capital = false;
}
}
public ReadOnlySpan<char> Get(int n)
{
if (capital) return ReadOnlySpan<char>.Empty;
if (n < 1 || n > N_GRAM || length < n) return ReadOnlySpan<char>.Empty;
if (n == 1)
{
char c = buffer[length - 1];
if (c == ' ') return ReadOnlySpan<char>.Empty;
return Trim(buffer.AsSpan(length - 1, 1));
}
else
{
return Trim(buffer.AsSpan(length - n, n));
}
}
private ReadOnlySpan<char> Trim(Span<char> span)
{
if (span.Length == 0) return span;
bool allSpace = true;
foreach(var c in span) { allSpace &= char.IsWhiteSpace(c); }
if (allSpace) return ReadOnlySpan<char>.Empty;
var s = 0;
var e = span.Length-1;
for (int i = 0; i < span.Length; i++)
{
if (char.IsWhiteSpace(span[i]))
{
s = i;
}
else
{
break;
}
}
for (int i = span.Length-1; i > s; i--)
{
if (char.IsWhiteSpace(span[i]))
{
e = i;
}
else
{
break;
}
}
return span.Slice(s, e + 1);
}
}
private class JsonLanguageProfile
{
public string name = null;
public Dictionary<string, int> freq = null;
public int[] n_words = null;
}
public static void TransformJsonDataInModelData(string pathToFiles)
{
var files = Directory.GetFiles(pathToFiles);
var ld = new LanguageDetector(version: 0);
foreach (var f in files)
{
var language = Languages.CodeToEnum(Path.GetFileNameWithoutExtension(f));
ld.Data.Languages.Add(language);
string json = File.ReadAllText(f);
var jsonProfile = JsonConvert.DeserializeObject<JsonLanguageProfile>(json);
foreach (var word in jsonProfile.freq.Keys)
{
int hash = GetHash(word.ToLowerInvariant().Trim().AsSpan());
if (!ld.Data.WordLanguageProbabilities.ContainsKey(hash))
{
ld.Data.WordLanguageProbabilities[hash] = new Dictionary<Language, double>();
}
if (word.Length >= 1 && word.Length <= ld.NGramLength)
{
double prob = (double)jsonProfile.freq[word] / jsonProfile.n_words[word.Length - 1];
ld.Data.WordLanguageProbabilities[hash][language] = prob;
}
}
}
ld.StoreAsync().Wait();
}
}
}