// --------------------------------------------------------------------------------------------------------------------
//
// 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.
//
// --------------------------------------------------------------------------------------------------------------------
using Newtonsoft.Json;
using System;
using System.Diagnostics.Contracts;
using VW.Labels;
using VW.Serializer;
namespace VW
{
///
/// A VowpalWabbit wrapper reading from JSON (see https://github.com/JohnLangford/vowpal_wabbit/wiki/JSON)
///
public sealed class VowpalWabbitJson : IDisposable
{
private VowpalWabbit vw;
///
/// Initializes a new instance of the class.
///
/// Command line arguments passed to native instance.
public VowpalWabbitJson(String args) : this(new VowpalWabbit(args))
{
}
///
/// Initializes a new instance of the class.
///
/// Arguments passed to native instance.
public VowpalWabbitJson(VowpalWabbitSettings settings)
: this(new VowpalWabbit(settings))
{
}
///
/// Initializes a new instance of the class.
///
/// The native instance to wrap.
/// This instance takes ownership of instance and disposes it.
public VowpalWabbitJson(VowpalWabbit vw)
{
if (vw == null)
{
throw new ArgumentNullException("vw");
}
Contract.EndContractBlock();
this.vw = vw;
}
///
/// The wrapped VW instance.
///
public VowpalWabbit Native
{
get
{
return this.vw;
}
}
///
/// Learns from the given example.
///
/// The example to learn.
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
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();
}
}
///
/// Learns from the given example.
///
/// The example to learn.
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
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();
}
}
///
/// Learn from the given example and return the current prediction for it.
///
/// The prediction type.
/// The example to learn.
/// The prediction factory to be used. See .
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
/// The prediction for the given .
public TPrediction Learn(string json, IVowpalWabbitPredictionFactory 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);
}
}
///
/// Learn from the given example and return the current prediction for it.
///
/// The prediction type.
/// The example to learn.
/// The prediction factory to be used. See .
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
/// The prediction for the given .
public TPrediction Learn(JsonReader reader, IVowpalWabbitPredictionFactory 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);
}
}
///
/// Predicts for the given example.
///
/// The example to predict for.
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
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();
}
}
///
/// Predicts for the given example.
///
/// The example to predict for.
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
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();
}
}
///
/// Predicts for the given example.
///
/// The prediction type.
/// The example to predict for.
/// The prediction factory to be used. See .
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
public TPrediction Predict(string json, IVowpalWabbitPredictionFactory 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);
}
}
///
/// Predicts for the given example.
///
/// The prediction type.
/// The example to predict for.
/// The prediction factory to be used. See .
///
/// Optional label, taking precedence over "_label" property found in .
/// If null, will be inspected and the "_label" property used as label.
///
/// Optional index of example the given label should be applied for multi-line examples.
public TPrediction Predict(JsonReader reader, IVowpalWabbitPredictionFactory 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);
}
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
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;
}
}
}
}
}