-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
VowpalWabbitJson.cs
249 lines (229 loc) · 11.1 KB
/
VowpalWabbitJson.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VowpalWabbitJson.cs">
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD
// license as described in the file LICENSE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using Newtonsoft.Json;
using System;
using System.Diagnostics.Contracts;
using VW.Labels;
using VW.Serializer;
namespace VW
{
/// <summary>
/// A VowpalWabbit wrapper reading from JSON (see https://github.com/JohnLangford/vowpal_wabbit/wiki/JSON)
/// </summary>
public sealed class VowpalWabbitJson : IDisposable
{
private VowpalWabbit vw;
/// <summary>
/// Initializes a new instance of the <see cref="VowpalWabbitJson"/> class.
/// </summary>
/// <param name="args">Command line arguments passed to native instance.</param>
public VowpalWabbitJson(String args) : this(new VowpalWabbit(args))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VowpalWabbitJson"/> class.
/// </summary>
/// <param name="settings">Arguments passed to native instance.</param>
public VowpalWabbitJson(VowpalWabbitSettings settings)
: this(new VowpalWabbit(settings))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VowpalWabbitJson"/> class.
/// </summary>
/// <param name="vw">The native instance to wrap.</param>
/// <remarks>This instance takes ownership of <paramref name="vw"/> instance and disposes it.</remarks>
public VowpalWabbitJson(VowpalWabbit vw)
{
if (vw == null)
{
throw new ArgumentNullException("vw");
}
Contract.EndContractBlock();
this.vw = vw;
}
/// <summary>
/// The wrapped VW instance.
/// </summary>
public VowpalWabbit Native
{
get
{
return this.vw;
}
}
/// <summary>
/// Learns from the given example.
/// </summary>
/// <param name="json">The example to learn.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="json"/>.
/// If null, <paramref name="json"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public void Learn(string json, ILabel label = null, int? index = null)
{
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(json, label, index))
{
result.Learn();
}
}
/// <summary>
/// Learns from the given example.
/// </summary>
/// <param name="reader">The example to learn.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="reader"/>.
/// If null, <paramref name="reader"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public void Learn(JsonReader reader, ILabel label = null, int? index = null)
{
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(reader, label, index))
{
result.Learn();
}
}
/// <summary>
/// Learn from the given example and return the current prediction for it.
/// </summary>
/// <typeparam name="TPrediction">The prediction type.</typeparam>
/// <param name="json">The example to learn.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="json"/>.
/// If null, <paramref name="json"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
/// <returns>The prediction for the given <paramref name="json"/>.</returns>
public TPrediction Learn<TPrediction>(string json, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory, ILabel label = null, int? index = null)
{
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(json, label, index))
{
return result.Learn(predictionFactory);
}
}
/// <summary>
/// Learn from the given example and return the current prediction for it.
/// </summary>
/// <typeparam name="TPrediction">The prediction type.</typeparam>
/// <param name="reader">The example to learn.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="reader"/>.
/// If null, <paramref name="reader"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
/// <returns>The prediction for the given <paramref name="reader"/>.</returns>
public TPrediction Learn<TPrediction>(JsonReader reader, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory, ILabel label = null, int? index = null)
{
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(reader, label, index))
{
return result.Learn(predictionFactory);
}
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <param name="json">The example to predict for.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="json"/>.
/// If null, <paramref name="json"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public void Predict(string json, ILabel label = null, int? index = null)
{
Contract.Requires(json != null);
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(json, label, index))
{
result.Predict();
}
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <param name="reader">The example to predict for.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="reader"/>.
/// If null, <paramref name="reader"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public void Predict(JsonReader reader, ILabel label = null, int? index = null)
{
Contract.Requires(reader != null);
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(reader, label, index))
{
result.Predict();
}
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <typeparam name="TPrediction">The prediction type.</typeparam>
/// <param name="json">The example to predict for.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="json"/>.
/// If null, <paramref name="json"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public TPrediction Predict<TPrediction>(string json, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory, ILabel label = null, int? index = null)
{
Contract.Requires(json != null);
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(json, label, index))
{
return result.Predict(predictionFactory);
}
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <typeparam name="TPrediction">The prediction type.</typeparam>
/// <param name="reader">The example to predict for.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <param name="label">
/// Optional label, taking precedence over "_label" property found in <paramref name="reader"/>.
/// If null, <paramref name="reader"/> will be inspected and the "_label" property used as label.
/// </param>
/// <param name="index">Optional index of example the given label should be applied for multi-line examples.</param>
public TPrediction Predict<TPrediction>(JsonReader reader, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory, ILabel label = null, int? index = null)
{
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(reader, label, index))
{
return result.Predict(predictionFactory);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
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;
}
}
}
}
}