-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathAveragePerceptronEntityRecognizer.cs
615 lines (507 loc) · 27.1 KB
/
AveragePerceptronEntityRecognizer.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
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
using UID;
using Mosaik.Core;
//using MessagePack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.IO;
namespace Catalyst.Models
{
public class AveragePerceptronEntityRecognizerModel : StorableObjectData
{
public DateTime TrainedTime { get; set; }
public Dictionary<int, string> IndexToEntityType { get; set; }
public Dictionary<int, EntityTag> IndexToEntityTag { get; set; }
public ConcurrentDictionary<int, float[]> Weights { get; set; }
public string[] Tags { get; set; }
public int[] TagHashes { get; set; }
public int[][] TagTagHashes { get; set; }
public List<HashSet<int>> Gazeteers { get; set; }
public string[] EntityTypes { get; set; }
public bool IgnoreCase { get; set; }
}
public class AveragePerceptronEntityRecognizer : StorableObjectV2<AveragePerceptronEntityRecognizer, AveragePerceptronEntityRecognizerModel>, IEntityRecognizer, IProcess
{
private int N_Features = 21;
private int N_Tags;
private int[] POShashes;
private ConcurrentDictionary<int, float[]> AverageWeights { get; set; }
public Dictionary<string, int> MapEntityTypeToTag { get; set; }
public bool IgnoreCase { get { return Data.IgnoreCase; } set { Data.IgnoreCase = value; } }
private AveragePerceptronEntityRecognizer(Language language, int version, string tag) : base(language, version, tag, compress: true)
{
POShashes = new int[Enum.GetValues(typeof(PartOfSpeech)).Length];
foreach (var pos in Enum.GetValues(typeof(PartOfSpeech)))
{
POShashes[(int)pos] = GetHash(pos.ToString());
}
}
public AveragePerceptronEntityRecognizer(Language language, int version, string tag, string[] entityTypes = null, bool ignoreCase = false) : this(language, version, tag)
{
if (entityTypes is object)
{
Data = new AveragePerceptronEntityRecognizerModel();
InitializeEntityTypes(entityTypes);
N_Tags = Data.Tags.Length;
}
Data.IgnoreCase = ignoreCase;
}
private void InitializeEntityTypes(string[] entityTypes)
{
Data.EntityTypes = entityTypes;
Data.Tags = new string[entityTypes.Length * 4 + 1];
Data.IndexToEntityType = new Dictionary<int, string>();
Data.IndexToEntityTag = new Dictionary<int, EntityTag>();
//TagOutside must be the first in the tag list, as it's the default tag in the indexing (a.k.a IndexTagOutside)
Data.Tags[0] = TagOutside.ToString();
int i = 1;
foreach (var et in entityTypes)
{
foreach (var s in new EntityTag[] { EntityTag.Begin, EntityTag.Inside, EntityTag.End, EntityTag.Single })
{
Data.Tags[i] = $"{(char)s}{Separator}{et}";
Data.IndexToEntityType.Add(i, et);
Data.IndexToEntityTag.Add(i, s);
i++;
}
}
int N = Data.Tags.Length;
Data.TagHashes = new int[N];
Data.TagTagHashes = new int[N][];
MapEntityTypeToTag = new Dictionary<string, int>();
for (i = 0; i < N; i++)
{
Data.TagHashes[i] = GetHash(Data.Tags[i]);
Data.TagTagHashes[i] = new int[N];
for (int j = 0; j < N; j++)
{
Data.TagTagHashes[i][j] = Hashes.CombineWeak(Data.TagHashes[i], GetHash(Data.Tags[j]));
}
MapEntityTypeToTag.Add(Data.Tags[i], i);
}
}
public const char TagBegin = (char)EntityTag.Begin;
public const char TagInside = (char)EntityTag.Inside;
public const char TagEnd = (char)EntityTag.End; //Last
public const char TagOutside = (char)EntityTag.Outside;
public const char TagSingle = (char)EntityTag.Single; //Unit
public const string Separator = "_";
private const int IndexTagOutside = 0;
public new static async Task<AveragePerceptronEntityRecognizer> FromStoreAsync(Language language, int version, string tag)
{
var a = new AveragePerceptronEntityRecognizer(language, version, tag);
await a.LoadDataAsync();
a.N_Tags = a.Data.Tags.Length;
a.N_Features += 3 * a.Data.Gazeteers.Count;
return a;
}
public override async Task LoadAsync(Stream stream)
{
await base.LoadAsync(stream);
N_Tags = Data.Tags.Length;
N_Features += 3 * Data.Gazeteers.Count;
}
public string SingleOrOutside(IReadOnlyList<EntityType> types)
{
EntityType tmp = types.Where(et => Data.EntityTypes.Any(t => t == et.Type)).FirstOrDefault();
if (tmp.Type != null)
{
return $"{(char)tmp.Tag}{Separator}{tmp.Type}";
}
else
{
return TagOutside.ToString();
}
}
public void Train(IEnumerable<IDocument> documents, int trainingSteps = 10, List<List<string>> gazeteers = null)
{
if (gazeteers is object)
{
Data.Gazeteers = gazeteers.Select(l => new HashSet<int>(l.Select(s => GetHash(s)))).ToList();
N_Features += 3 * Data.Gazeteers.Count;
}
else
{
Data.Gazeteers = new List<HashSet<int>>();
}
Data.Weights = new ConcurrentDictionary<int, float[]>();
AverageWeights = new ConcurrentDictionary<int, float[]>();
var sentences = documents.SelectMany(doc => doc.Spans).ToList();
var sw = new System.Diagnostics.Stopwatch();
int N_dev = (int)Math.Floor(0.9 * sentences.Count());
var trainSentences = sentences.Take(N_dev).ToList();
var testSentences = sentences.Skip(N_dev).ToList();
var trainSentencesTags = trainSentences.Select(st => st.Tokens.Select(tk => MapEntityTypeToTag[SingleOrOutside(tk.EntityTypes)]).ToArray()).ToArray();
var testSentencesTags = testSentences.Select(st => st.Tokens.Select(tk => MapEntityTypeToTag[SingleOrOutside(tk.EntityTypes)]).ToArray()).ToArray();
string tOutside = TagOutside.ToString();
double totalTrain = trainSentences.Sum(st => st.Tokens.Count(tk => SingleOrOutside(tk.EntityTypes) != tOutside));
double totalTest = testSentences.Sum(st => st.Tokens.Count(tk => SingleOrOutside(tk.EntityTypes) != tOutside));
double totalTokensTrain = trainSentences.Sum(st => st.TokensCount);
double totalTokensTest = testSentences.Sum(st => st.TokensCount);
int TP = 0, FN = 0, FP = 0; double precision, recall;
for (int step = 0; step < trainingSteps; step++)
{
trainSentences.ShuffleTogether(trainSentencesTags);
sw.Restart();
Parallel.ForEach(Enumerable.Range(0, trainSentences.Count), i =>
{
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
var (_TP, _FN, _FP) = TrainOnSentence(trainSentences[i], ref trainSentencesTags[i], ScoreBuffer, Features);
Interlocked.Add(ref TP, _TP);
Interlocked.Add(ref FP, _FP);
Interlocked.Add(ref FN, _FN);
});
sw.Stop();
precision = (double)TP / (TP + FP);
recall = (double)TP / (TP + FN);
Logger.LogInformation($"{Languages.EnumToCode(Language)} Step {step + 1}/{trainingSteps} Train set: F1={100 * 2 * (precision * recall) / (precision + recall):0.00}% P={100 * precision:0.00}% R={100 * recall:0.00}% at a rate of {Math.Round(1000 * totalTokensTrain / sw.ElapsedMilliseconds, 0) } tokens/second");
TP = 0; FN = 0; FP = 0;
sw.Restart();
Parallel.ForEach(Enumerable.Range(0, testSentences.Count), i =>
{
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
var (_TP, _FN, _FP) = TrainOnSentence(testSentences[i], ref testSentencesTags[i], ScoreBuffer, Features, updateModel: false);
Interlocked.Add(ref TP, _TP);
Interlocked.Add(ref FP, _FP);
Interlocked.Add(ref FN, _FN);
});
sw.Stop();
precision = (double)TP / (TP + FP);
recall = (double)TP / (TP + FN);
Logger.LogInformation($"{Languages.EnumToCode(Language)} Step {step + 1}/{trainingSteps} Test set: F1={100 * 2 * (precision * recall) / (precision + recall):0.00}% P={100 * precision:0.00}% R={100 * recall:0.00}% at a rate of {Math.Round(1000 * totalTokensTest / sw.ElapsedMilliseconds, 0) } tokens/second");
UpdateAverages();
}
UpdateAverages(final: true, trainingSteps: trainingSteps);
Data.Weights = AverageWeights;
Data.TrainedTime = DateTime.UtcNow;
AverageWeights = null;
//Final test
TP = 0; FN = 0; FP = 0;
sw.Restart();
Parallel.ForEach(Enumerable.Range(0, trainSentences.Count), i =>
{
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
var tags = trainSentencesTags[i];
var (_TP, _FN, _FP) = TrainOnSentence(trainSentences[i], ref tags, ScoreBuffer, Features, updateModel: false);
Interlocked.Add(ref TP, _TP);
Interlocked.Add(ref FP, _FP);
Interlocked.Add(ref FN, _FN);
});
sw.Stop();
precision = (double)TP / (TP + FP);
recall = (double)TP / (TP + FN);
Logger.LogInformation($"{Languages.EnumToCode(Language)} FINAL Train set: F1={100 * 2 * (precision * recall) / (precision + recall):0.00}% P={100 * precision:0.00}% R={100 * recall:0.00}% at a rate of {Math.Round(1000 * totalTokensTrain / sw.ElapsedMilliseconds, 0) } tokens/second");
TP = 0; FN = 0; FP = 0;
sw.Restart();
Parallel.ForEach(Enumerable.Range(0, testSentences.Count), i =>
{
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
var tags = testSentencesTags[i];
var (_TP, _FN, _FP) = TrainOnSentence(testSentences[i], ref tags, ScoreBuffer, Features, updateModel: false);
Interlocked.Add(ref TP, _TP);
Interlocked.Add(ref FP, _FP);
Interlocked.Add(ref FN, _FN);
});
sw.Stop();
precision = (double)TP / (TP + FP);
recall = (double)TP / (TP + FN);
Logger.LogInformation($"{Languages.EnumToCode(Language)} FINAL Test set: F1={100 * 2 * (precision * recall) / (precision + recall):0.00}% P={100 * precision:0.00}% R={100 * recall:0.00}% at a rate of {Math.Round(1000 * totalTokensTest / sw.ElapsedMilliseconds, 0) } tokens/second");
}
private void UpdateAverages(bool final = false, float trainingSteps = -1)
{
foreach (var feature in Data.Weights)
{
var weights = AverageWeights.GetOrAdd(feature.Key, k => new float[N_Tags]);
for (int i = 0; i < N_Tags; i++)
{
weights[i] += feature.Value[i];
if (final)
{
weights[i] /= trainingSteps;
}
}
}
}
public (int TP, int FN, int FP) TrainOnSentence(Span span, ref int[] spanTags, Span<float> ScoreBuffer, Span<int> features, bool updateModel = true)
{
//for training, we expect the tokens to have [BILOU]-[Type] entries as the only EntityType
Token prev = Token.BeginToken; Token prev2 = Token.BeginToken; Token curr = Token.BeginToken; Token next = Token.BeginToken; Token next2 = Token.BeginToken;
int prevTag = IndexTagOutside; int prev2Tag = IndexTagOutside; int currTag = IndexTagOutside;
int i = 0, correct = 0;
int TP = 0, FN = 0, FP = 0;
var en = span.GetStructEnumerator();
while (!next.IsEndToken)
{
prev2 = prev; prev = curr; curr = next; next = next2; prev2Tag = prevTag; prevTag = currTag;
if (en.MoveNext()) { next2 = en.Current; } else { next2 = Token.EndToken; }
if (!curr.IsBeginToken && !curr.IsEndToken)
{
int tokenTag = spanTags[i];
GetFeatures(features, curr, prev, prev2, next, next2, prevTag, prev2Tag);
currTag = PredictTagFromFeatures(features, ScoreBuffer);
if (updateModel) { UpdateModel(tokenTag, currTag, features); }
if (tokenTag != IndexTagOutside && currTag == tokenTag) { correct++; }
if (tokenTag != IndexTagOutside && currTag == tokenTag) { TP++; }
if (tokenTag == IndexTagOutside && currTag != tokenTag) { FP++; }
if (tokenTag != IndexTagOutside && currTag != tokenTag) { FN++; }
i++;
}
}
return (TP, FN, FP);
}
public void Process(IDocument document, CancellationToken cancellationToken = default)
{
RecognizeEntities(document);
}
public string[] Produces()
{
return Data.EntityTypes;
}
public bool RecognizeEntities(IDocument document)
{
if (N_Tags == 0) { N_Tags = Data.Tags.Length; }
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
var result = false;
foreach (var span in document)
{
result |= Predict(span, ScoreBuffer, Features);
}
return result;
}
public bool Predict(Span span)
{
Span<float> ScoreBuffer = stackalloc float[N_Tags];
Span<int> Features = stackalloc int[N_Features];
return Predict(span, ScoreBuffer, Features);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Predict(Span span, Span<float> ScoreBuffer, Span<int> features)
{
Token prev = Token.BeginToken; Token prev2 = Token.BeginToken; Token curr = Token.BeginToken; Token next = Token.BeginToken; Token next2 = Token.BeginToken;
int prevTag = IndexTagOutside; int prev2Tag = IndexTagOutside; int currTag = IndexTagOutside;
bool foundAny = false;
int i = 0;
var en = span.GetStructEnumerator();
var tags = new int[span.TokensCount];
while (!next.IsEndToken)
{
prev2 = prev; prev = curr; curr = next; next = next2; prev2Tag = prevTag; prevTag = currTag;
if (en.MoveNext()) { next2 = en.Current; } else { next2 = Token.EndToken; }
if (!curr.IsBeginToken)
{
GetFeatures(features, curr, prev, prev2, next, next2, prevTag, prev2Tag);
tags[i] = PredictTagFromFeatures(features, ScoreBuffer);
currTag = tags[i];
i++;
}
}
string lastBegin = null;
for (i = 0; i < span.TokensCount; i++)
{
if (tags[i] != IndexTagOutside)
{
var type = Data.IndexToEntityType[tags[i]];
var tag = Data.IndexToEntityTag[tags[i]];
bool valid = tag == EntityTag.Single; //Single is always valid
if (tag == EntityTag.Begin) //Checks if it's a valid combination of tags - i.e. B+I+E or B+E
{
for (int j = i + 1; j < span.TokensCount; j++)
{
if (tags[j] == IndexTagOutside) break;
var other_tag = Data.IndexToEntityTag[tags[j]];
if (other_tag != EntityTag.Inside && other_tag != EntityTag.End) { break; }
var other_type = Data.IndexToEntityType[tags[j]];
if (other_type != type) { break; }
if (other_tag == EntityTag.End) { valid = true; break; } //found the right tag and right type by now
}
}
else if (tag == EntityTag.Inside || tag == EntityTag.End)
{
valid = type == lastBegin;
}
if (valid)
{
if (tag == EntityTag.Begin) { lastBegin = type; }
if (tag == EntityTag.End) { lastBegin = null; }
span[i].AddEntityType(new EntityType(type, tag));
foundAny = true;
}
}
}
return foundAny;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateModel(int correctTag, int predictedTag, Span<int> features)
{
if (correctTag == predictedTag) { return; } //nothing to update
foreach (var feature in features)
{
var weights = Data.Weights.GetOrAdd(feature, k => new float[N_Tags]);
weights[correctTag] += 1f;
weights[predictedTag] -= 1f;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int PredictTagFromFeatures(Span<int> features, Span<float> ScoreBuffer)
{
bool first = true;
foreach (var feature in features)
{
if (Data.Weights.TryGetValue(feature, out float[] weights))
{
if (first)
{
weights.CopyTo(ScoreBuffer);
first = false;
}
else
{
for (var j = 0; j < ScoreBuffer.Length; j++)
{
ScoreBuffer[j] += weights[j];
}
}
}
}
var best = ScoreBuffer[0]; int index = 0;
for (int i = 1; i < ScoreBuffer.Length; i++)
{
if (ScoreBuffer[i] > best) { best = ScoreBuffer[i]; index = i; }
}
return best > 0 ? index : IndexTagOutside;
}
private readonly int _HashBias = GetIgnoreCaseHash("bias");
private readonly int _HashISufix = GetIgnoreCaseHash("i suffix");
private readonly int _HashIPrefix = GetIgnoreCaseHash("i pref1");
private readonly int _HashIShape = GetIgnoreCaseHash("i shape");
private readonly int _HashIm1Sufix = GetIgnoreCaseHash("i-1 suffix");
private readonly int _HashIp1Sufix = GetIgnoreCaseHash("i+1 suffix");
private readonly int _HashIm1Shape = GetIgnoreCaseHash("i-1 shape");
private readonly int _HashIp1Shape = GetIgnoreCaseHash("i+1 shape");
private readonly int _HashIm1TagIword = GetIgnoreCaseHash("i-1 tag i word");
private readonly int _HashIm2Word = GetIgnoreCaseHash("i-2 word");
private readonly int _HashIp1Word = GetIgnoreCaseHash("i+1 word");
private readonly int _HashIWord = GetIgnoreCaseHash("i word");
private readonly int _HashIm1Word = GetIgnoreCaseHash("i-1 word");
private readonly int _HashIp2Word = GetIgnoreCaseHash("i+2 word");
private readonly int _HashIm1Tag = GetIgnoreCaseHash("i-1 tag");
private readonly int _HashIm2Tag = GetIgnoreCaseHash("i-2 tag");
private readonly int _HashITagIm2Tag = GetIgnoreCaseHash("i tag i-2 tag");
private readonly int _HashIPOS = GetIgnoreCaseHash("i pos");
private readonly int _HashIm1POS = GetIgnoreCaseHash("i-1 pos");
private readonly int _HashIm2POS = GetIgnoreCaseHash("i-2 pos");
private readonly int _HashIp1POS = GetIgnoreCaseHash("i+1 pos");
private readonly int _HashIp2POS = GetIgnoreCaseHash("i+2 pos");
private readonly int _HashGazeteerI = GetIgnoreCaseHash("i gazeteer");
private readonly int _HashGazeteerIm1 = GetIgnoreCaseHash("i-1 gazeteer");
private readonly int _HashGazeteerIp1 = GetIgnoreCaseHash("i+1 gazeteer");
private readonly int _HashGazeteerTrue = GetIgnoreCaseHash("gazeteer true");
private readonly int _HashGazeteerFalse = GetIgnoreCaseHash("gazeteer false");
public void GetFeatures(IToken[] tokens, int[] guesses, int indexCurrent, Span<int> featuresBuffer)
{
var current = tokens[indexCurrent];
var prev = tokens[indexCurrent - 1];
var prev2 = tokens[indexCurrent - 2];
var next = tokens[indexCurrent + 1];
var next2 = tokens[indexCurrent + 2];
var prevTag = guesses[indexCurrent - 1];
var prev2Tag = guesses[indexCurrent - 2];
GetFeatures(featuresBuffer, current, prev, prev2, next, next2, prevTag, prev2Tag);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GetFeatures(Span<int> features, IToken current, IToken prev, IToken prev2, IToken next, IToken next2, int prevTag, int prev2Tag)
{
//Features from Spacy
int k = 0;
bool ignoreCase = Data.IgnoreCase;
features[k++] = _HashBias;
features[k++] = Hashes.CombineWeak(_HashISufix, GetSuffixHash(current.ValueAsSpan));
features[k++] = Hashes.CombineWeak(_HashIPrefix, GetPrefixHash(current.ValueAsSpan));
features[k++] = Hashes.CombineWeak(_HashIm1Sufix, GetSuffixHash(prev.ValueAsSpan));
features[k++] = Hashes.CombineWeak(_HashIp1Sufix, GetSuffixHash(next.ValueAsSpan));
features[k++] = Hashes.CombineWeak(_HashIm1TagIword, Hashes.CombineWeak(Data.TagHashes[prevTag], ignoreCase ? current.IgnoreCaseHash : current.Hash));
features[k++] = Hashes.CombineWeak(_HashIm2Word, ignoreCase ? prev2.IgnoreCaseHash : prev2.Hash);
features[k++] = Hashes.CombineWeak(_HashIp1Word, ignoreCase ? next.IgnoreCaseHash : next.Hash);
features[k++] = Hashes.CombineWeak(_HashIWord, ignoreCase ? current.IgnoreCaseHash : current.Hash);
features[k++] = Hashes.CombineWeak(_HashIm1Word, ignoreCase ? prev.IgnoreCaseHash : prev.Hash);
features[k++] = Hashes.CombineWeak(_HashIp2Word, ignoreCase ? next2.IgnoreCaseHash : next2.Hash);
features[k++] = Hashes.CombineWeak(_HashIm1Tag, Data.TagHashes[prevTag]);
features[k++] = Hashes.CombineWeak(_HashIm2Tag, Data.TagHashes[prev2Tag]);
features[k++] = Hashes.CombineWeak(_HashITagIm2Tag, Data.TagTagHashes[prevTag][prev2Tag]);
features[k++] = Hashes.CombineWeak(_HashIPOS, POShashes[(int)current.POS]);
features[k++] = Hashes.CombineWeak(_HashIm2POS, POShashes[(int)prev2.POS]);
features[k++] = Hashes.CombineWeak(_HashIp1POS, POShashes[(int)next.POS]);
features[k++] = Hashes.CombineWeak(_HashIp2POS, POShashes[(int)next2.POS]);
features[k++] = Hashes.CombineWeak(_HashIShape, GetShapeHash(current.ValueAsSpan, false));
features[k++] = Hashes.CombineWeak(_HashIm1Shape, GetShapeHash(current.ValueAsSpan, false));
features[k++] = Hashes.CombineWeak(_HashIp1Shape, GetShapeHash(current.ValueAsSpan, false));
for (int i = 0; i < Data.Gazeteers.Count; i++)
{
features[k++] = Hashes.CombineWeak(_HashGazeteerI + i, Data.Gazeteers[i].Contains(ignoreCase ? current.IgnoreCaseHash : current.Hash) ? _HashGazeteerTrue : _HashGazeteerFalse);
features[k++] = Hashes.CombineWeak(_HashGazeteerIm1 + i, Data.Gazeteers[i].Contains(ignoreCase ? prev.IgnoreCaseHash : prev.Hash) ? _HashGazeteerTrue : _HashGazeteerFalse);
features[k++] = Hashes.CombineWeak(_HashGazeteerIp1 + i, Data.Gazeteers[i].Contains(ignoreCase ? next.IgnoreCaseHash : next.Hash) ? _HashGazeteerTrue : _HashGazeteerFalse);
}
}
private static readonly int _H_Base = GetIgnoreCaseHash("shape");
private static readonly int _H_Digit = GetIgnoreCaseHash("shape_digit");
private static readonly int _H_Lower = GetIgnoreCaseHash("shape_lower");
private static readonly int _H_Upper = GetIgnoreCaseHash("shape_upper");
private static readonly int _H_Punct = GetIgnoreCaseHash("shape_puct");
private static readonly int _H_Symbol = GetIgnoreCaseHash("shape_symbol");
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetShapeHash(ReadOnlySpan<char> token, bool compact)
{
int hash = _H_Base;
int prevType = _H_Base;
for (int i = 0; i < token.Length; i++)
{
int type;
if (char.IsLower(token[i])) { type = _H_Lower; }
else if (char.IsUpper(token[i])) { type = _H_Upper; }
else if (char.IsNumber(token[i])) { type = _H_Digit; }
else if (char.IsPunctuation(token[i])) { type = _H_Punct; }
else { type = _H_Symbol; }
if (!compact || type != prevType)
{
hash = Hashes.CombineWeak(hash, type);
}
prevType = type;
}
return hash;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetSuffixHash(ReadOnlySpan<char> token, int suffixSize = 3)
{
int len = token.Length - 1;
int n = Math.Min(suffixSize, len);
return Data.IgnoreCase ? token.IgnoreCaseHash32(len - n + 1, len) : token.CaseSensitiveHash32(len - n + 1, len);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetPrefixHash(ReadOnlySpan<char> token, int prefixSize = 1)
{
int len = token.Length - 1;
int n = Math.Min(prefixSize, len);
return Data.IgnoreCase ? token.IgnoreCaseHash32(0, n) : token.CaseSensitiveHash32(0, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetIgnoreCaseHash(string feature)
{
return feature.IgnoreCaseHash32();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetHash(string feature)
{
return Data.IgnoreCase ? feature.IgnoreCaseHash32() : feature.CaseSensitiveHash32();
}
}
}