-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
VowpalWabbitExampleJsonValidator.cs
242 lines (206 loc) · 9.69 KB
/
VowpalWabbitExampleJsonValidator.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VW;
using VW.Labels;
using VW.Serializer;
namespace cs_unittest
{
internal sealed class VowpalWabbitExampleJsonValidator : IDisposable
{
private VowpalWabbit vw;
internal VowpalWabbitExampleJsonValidator(string args = null) : this(new VowpalWabbitSettings((args ?? "") + " --json"))
{
}
internal VowpalWabbitExampleJsonValidator(VowpalWabbitSettings settings)
{
settings = (VowpalWabbitSettings)settings.Clone();
settings.EnableStringExampleGeneration = true;
this.vw = new VowpalWabbit(settings);
}
public void Validate(string line, VowpalWabbitExampleCollection example, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null, bool validateVowpalWabbitString = true)
{
Assert.IsNotNull(example);
var jsonExample = example as VowpalWabbitSingleLineExampleCollection;
Assert.IsNotNull(jsonExample);
using (var strExample = this.vw.ParseLine(line))
{
var diff = strExample.Diff(this.vw, jsonExample.Example, labelComparator);
Assert.IsNull(diff, diff + " generated string: '" + jsonExample.VowpalWabbitString + "'");
if (validateVowpalWabbitString)
{
using (var strJsonExample = this.vw.ParseLine(jsonExample.Example.VowpalWabbitString))
{
diff = strExample.Diff(this.vw, strJsonExample, labelComparator);
Assert.IsNull(diff, diff);
}
}
}
}
public void Validate(string line, string json, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null, bool enableNativeJsonValidation = true)
{
using (var jsonSerializer = new VowpalWabbitJsonSerializer(this.vw))
using (var jsonExample = jsonSerializer.ParseAndCreate(json, label))
{
this.Validate(line, jsonExample, labelComparator, label);
if (enableNativeJsonValidation)
{
var examples = this.vw.ParseJson(json);
Assert.AreEqual(1, examples.Count);
using (var jsonNativeExample = new VowpalWabbitSingleLineExampleCollection(this.vw, examples[0]))
{
this.Validate(line, jsonNativeExample, labelComparator, label, validateVowpalWabbitString: false);
}
}
}
}
public void Validate(string[] lines, JsonReader jsonReader, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null, int? index = null, VowpalWabbitJsonExtension extension = null)
{
VowpalWabbitExample[] strExamples = new VowpalWabbitExample[lines.Count()];
try
{
for (int i = 0; i < lines.Length; i++)
strExamples[i] = this.vw.ParseLine(lines[i]);
using (var jsonSerializer = new VowpalWabbitJsonSerializer(this.vw))
{
if (extension != null)
{
jsonSerializer.RegisterExtension(extension);
// extension are not supported with native JSON parsing
}
using (var jsonExample = (VowpalWabbitMultiLineExampleCollection)jsonSerializer.ParseAndCreate(jsonReader, label, index))
{
var jsonExamples = new List<VowpalWabbitExample>();
if (jsonExample.SharedExample != null)
jsonExamples.Add(jsonExample.SharedExample);
jsonExamples.AddRange(jsonExample.Examples);
Assert.AreEqual(strExamples.Length, jsonExamples.Count);
for (int i = 0; i < strExamples.Length; i++)
{
using (var strJsonExample = this.vw.ParseLine(jsonExamples[i].VowpalWabbitString))
{
var diff = strExamples[i].Diff(this.vw, jsonExamples[i], labelComparator);
Assert.IsNull(diff, diff + " generated string: '" + jsonExamples[i].VowpalWabbitString + "'");
diff = strExamples[i].Diff(this.vw, strJsonExample, labelComparator);
Assert.IsNull(diff, diff);
}
}
}
}
}
finally
{
foreach (var ex in strExamples)
if (ex != null)
ex.Dispose();
}
}
public void Validate(string[] lines, List<VowpalWabbitExample> examples, IVowpalWabbitLabelComparator labelComparator = null)
{
VowpalWabbitExample[] strExamples = new VowpalWabbitExample[lines.Count()];
try
{
for (int i = 0; i < lines.Length; i++)
strExamples[i] = this.vw.ParseLine(lines[i]);
for (int i = 0; i < strExamples.Length; i++)
{
var diff = strExamples[i].Diff(this.vw, examples[i], labelComparator);
Assert.IsNull(diff, diff + " generated string: '" + strExamples[i].VowpalWabbitString + "'");
}
}
finally
{
foreach (var ex in strExamples)
if (ex != null)
ex.Dispose();
}
}
public void Validate(string[] lines, string json, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null, int? index = null, VowpalWabbitJsonExtension extension = null, bool enableNativeJsonValidation = true)
{
VowpalWabbitExample[] strExamples = new VowpalWabbitExample[lines.Count()];
try
{
for (int i = 0; i < lines.Length; i++)
strExamples[i] = this.vw.ParseLine(lines[i]);
using (var jsonSerializer = new VowpalWabbitJsonSerializer(this.vw))
{
if (extension != null)
{
jsonSerializer.RegisterExtension(extension);
// extension are not supported with native JSON parsing
enableNativeJsonValidation = false;
}
List<VowpalWabbitExample> jsonNativeExamples = null;
try
{
if (enableNativeJsonValidation)
{
jsonNativeExamples = this.vw.ParseJson(json);
Assert.IsNotNull(jsonNativeExamples);
}
using (var jsonExample = (VowpalWabbitMultiLineExampleCollection)jsonSerializer.ParseAndCreate(json, label, index))
{
var jsonExamples = new List<VowpalWabbitExample>();
if (jsonExample.SharedExample != null)
jsonExamples.Add(jsonExample.SharedExample);
jsonExamples.AddRange(jsonExample.Examples);
Assert.AreEqual(strExamples.Length, jsonExamples.Count);
if (enableNativeJsonValidation)
Assert.AreEqual(strExamples.Length, jsonNativeExamples.Count);
for (int i = 0; i < strExamples.Length; i++)
{
using (var strJsonExample = this.vw.ParseLine(jsonExamples[i].VowpalWabbitString))
{
var diff = strExamples[i].Diff(this.vw, jsonExamples[i], labelComparator);
Assert.IsNull(diff, diff + " generated string: '" + jsonExamples[i].VowpalWabbitString + "'");
diff = strExamples[i].Diff(this.vw, strJsonExample, labelComparator);
Assert.IsNull(diff, diff);
if (enableNativeJsonValidation)
{
diff = strExamples[i].Diff(this.vw, jsonNativeExamples[i], labelComparator);
Assert.IsNull(diff, diff);
}
}
}
}
}
finally
{
if (jsonNativeExamples != null)
{
foreach (var ex in jsonNativeExamples)
ex.Dispose();
}
}
}
}
finally
{
foreach (var ex in strExamples)
if (ex != null)
ex.Dispose();
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (this.vw != null)
{
this.vw.Dispose();
this.vw = null;
}
}
}
}
}