-
Notifications
You must be signed in to change notification settings - Fork 73
/
FastTextLanguageDetector.cs
168 lines (147 loc) · 5.66 KB
/
FastTextLanguageDetector.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
//using MessagePack;
using Mosaik.Core;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Catalyst.Models
{
[FormerName("Mosaik.NLU.Models", "VectorizerLanguageDetectorModel")]
public class FastTextLanguageDetectorData : StorableObjectData
{
}
[FormerName("Mosaik.NLU.Models", "VectorizerLanguageDetector")]
public class FastTextLanguageDetector : StorableObject<FastTextLanguageDetector, FastTextLanguageDetectorData>, IProcess
{
private SpaceTokenizer Tokenizer;
private FastText Model;
private NumberToWordNormalizer NumberNormalizer = new NumberToWordNormalizer() { ReplacementText = "" };
public FastTextLanguageDetector(int version) : base(Language.Any, version, nameof(FastTextLanguageDetector), compress: false)
{
Model = new FastText(Language.Any, version, "language-detector");
Model.Data.Type = FastText.ModelType.Supervised;
Model.Data.MaximumWordNgrams = 0;
Model.Data.MinimumNgrams = 2;
Model.Data.MaximumNgrams = 5;
Model.Data.VectorQuantization = QuantizationType.None;
Model.Data.LearningRate = 0.1f;
Model.Data.Epoch = 50;
Model.Data.Dimensions = 16;
Model.Data.IgnoreCase = false;
Model.Data.Loss = FastText.LossType.NegativeSampling;
Model.Data.MinimumCount = 5;
Tokenizer = new SpaceTokenizer();
}
public static Task<FastTextLanguageDetector> LoadAsync(int version)
{
return FromStoreAsync(Language.Any, version, "");
}
public new static async Task<FastTextLanguageDetector> FromStoreAsync(Language language, int version, string tag)
{
var a = new FastTextLanguageDetector(version);
//Because we use the model name as the tag of this model, we've to check for formernames here
try
{
await a.LoadDataAsync();
}
catch (FileNotFoundException)
{
if (ObjectStore.TryGetFormerNames(nameof(FastTextLanguageDetector), out var formerNames))
{
var correctTag = a.Tag;
foreach (var formerName in formerNames)
{
try
{
a.Tag = formerName;
await a.LoadDataAsync();
a.Tag = correctTag;
break;
}
catch (FileNotFoundException)
{
//ignore
}
}
}
}
a.Model = await FastText.FromStoreAsync_Internal(Language.Any, version, "language-detector");
a.Model?.CompactSupervisedModel();
return a;
}
public new static async Task<bool> DeleteAsync(Language language, int version, string tag)
{
var a = new FastTextLanguageDetector(version);
bool deleted = false;
deleted |= await FastText.DeleteAsync(Language.Any, version, "language-detector");
deleted |= await a.DeleteDataAsync();
return deleted;
}
public void Process(IDocument document, CancellationToken cancellationToken = default)
{
if (document.Length == 0 || (document.Language != Language.Unknown && document.Language != Language.Any)) { return; } //Don't try to identify documents that already have their language set or is empty
IDocument tempDocument = Prepare(document);
try
{
var tag = Model.PredictMax(tempDocument, 200);
if (tag.label is null)
{
document.Language = Language.Unknown;
}
else
{
document.Language = Languages.CodeToEnum(tag.label);
}
}
catch
{
document.Language = Language.Unknown;
}
}
public Dictionary<Language, float> Predict(IDocument document)
{
IDocument tempDocument = Prepare(document);
try
{
var predictions = Model.Predict(tempDocument);
return predictions.ToDictionary(kv => Languages.CodeToEnum(kv.Key), kv => kv.Value);
}
catch
{
return new Dictionary<Language, float>()
{
[Language.Unknown] = 1f
};
}
}
private IDocument Prepare(IDocument document)
{
IDocument tempDocument = document;
if (document.SpansCount == 0) // Have to tokenize temporarily the document
{
if (document.Length > 1000)
{
tempDocument = new Document(document.Value.Substring(0, 1000));
}
else
{
tempDocument = new Document(document.Value);
}
Tokenizer.Process(tempDocument);
NumberNormalizer.Process(tempDocument);
}
return tempDocument;
}
public void Train(IEnumerable<IDocument> documents)
{
Model.Train(documents);
}
public override async Task StoreAsync()
{
Model.CompactSupervisedModel();
await Model.StoreAsync();
await base.StoreAsync();
}
}
}