Parse() 공개 메소드

Parses the example.
public Parse ( JsonReader reader, ILabel label = null, List extensions = null ) : void
reader Newtonsoft.Json.JsonReader The example to parse.
label ILabel /// Optional label, taking precedence over "_label" property found in . /// If null, will be inspected and the "_label" property used as label. ///
extensions List Action to be executed when special properties are discovered.
리턴 void
예제 #1
0
        /// <summary>
        /// Marshals JSON string into VW example.
        /// </summary>
        public void Marshal(VowpalWabbitMarshalContext ctx, Namespace ns, Feature feature)
        {
            if (this.value == null)
            {
                return;
            }

            var jsonSerializer = new JsonSerializer();

            using (var jsonBuilder = new VowpalWabbitJsonBuilder(ctx.VW, VowpalWabbitDefaultMarshaller.Instance, jsonSerializer))
            {
                // serialize from object to JSON
                var sb = new StringBuilder();
                using (var writer = new JsonTextWriter(new StringWriter(sb)))
                {
                    this.jsonConverter.WriteJson(writer, this.value, jsonSerializer);
                }

                // marshal from JSON to VW
                using (var reader = new JsonTextReader(new StringReader(sb.ToString())))
                {
                    jsonBuilder.Parse(reader, ctx, new Namespace(ctx.VW, feature.Name));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Marshals JSON string into VW example.
        /// </summary>
        public void Marshal(VowpalWabbitMarshalContext ctx, Namespace ns, Feature feature)
        {
            if (this.value == null)
            {
                return;
            }

            try
            {
                var jsonSerializer = new JsonSerializer();
                using (var jsonBuilder = new VowpalWabbitJsonBuilder(ctx.VW, VowpalWabbitDefaultMarshaller.Instance, jsonSerializer))
                {
                    // marshal from JSON to VW
                    foreach (var json in jsonConverter.JsonFragments(this.value))
                    {
                        if (json == null)
                        {
                            continue;
                        }

                        using (var reader = new JsonTextReader(new StringReader(json)))
                        {
                            jsonBuilder.Parse(reader, ctx, new Namespace(ctx.VW, feature.Name));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new VowpalWabbitSerializationException("Optimized marshalling failed", e, ns, feature);
            }
        }
        private bool HandleMultiProperty(VowpalWabbitJsonParseState state, string property)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;

            if (!property.Equals(multiPropertyName, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            var reader = state.Reader;

            if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
            {
                throw new VowpalWabbitJsonException(reader, "Expected start array for '" + multiPropertyName + "'");
            }

            if (this.ExampleBuilders == null)
            {
                this.ExampleBuilders = new List <VowpalWabbitJsonBuilder>();
            }

            state.MultiIndex = 0;

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonToken.StartObject:
                    VowpalWabbitJsonBuilder builder = null;

                    try
                    {
                        builder = new VowpalWabbitJsonBuilder(this, this.vwPool, VowpalWabbitDefaultMarshaller.Instance, this.jsonSerializer, state.MultiIndex);
                        this.ExampleBuilders.Add(builder);
                    }
                    catch (Exception)
                    {
                        builder.Dispose();
                        throw;
                    }

                    // pass the label to the selected example
                    builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null, this.extensions);

                    state.MultiIndex++;
                    break;

                case JsonToken.EndArray:
                    return(true);

                default:
                    throw new VowpalWabbitJsonException(reader, "Unexpected token: " + reader.TokenType);
                }
            }

            throw new VowpalWabbitJsonException(reader, "Unexpected end");
        }
예제 #4
0
        /// <summary>
        /// Parses the example.
        /// </summary>
        /// <param name="reader">The example to parse.</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 Parse(JsonReader reader, ILabel label = null, int?index = null)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;

            // only pass the label if it's not targeted at a particular index
            this.ExampleBuilder.Parse(reader, index == null ? label : null,
                                      propertyName =>
            {
                if (propertyName != multiPropertyName)
                {
                    return(false);
                }

                if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                {
                    throw new VowpalWabbitJsonException(reader.Path, "Expected start array for '" + multiPropertyName + "'");
                }

                if (this.ExampleBuilders == null)
                {
                    this.ExampleBuilders = new List <VowpalWabbitJsonBuilder>();
                }

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonToken.StartObject:
                        VowpalWabbitJsonBuilder builder = null;

                        try
                        {
                            builder = new VowpalWabbitJsonBuilder(this.vwPool, this.defaultMarshaller, this.jsonSerializer);
                            this.ExampleBuilders.Add(builder);
                        }
                        catch (Exception)
                        {
                            builder.Dispose();
                            throw;
                        }

                        // pass the label to the selected example
                        builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null);
                        break;

                    case JsonToken.EndArray:
                        return(true);

                    default:
                        throw new VowpalWabbitJsonException(reader.Path, "Unexpected token: " + reader.TokenType);
                    }
                }

                throw new VowpalWabbitJsonException(reader.Path, "Unexpected end");
            });
        }
        public void Marshal(VowpalWabbitMarshalContext ctx, Namespace ns, Feature feature)
        {
            var jsonSerializer = new JsonSerializer();
            using (var jsonBuilder = new VowpalWabbitJsonBuilder(ctx.VW, VowpalWabbitDefaultMarshaller.Instance, jsonSerializer))
            {
                // serialize from object to JSON
                var sb = new StringBuilder();
                using (var writer = new JsonTextWriter(new StringWriter(sb)))
                {
                    this.jsonConverter.WriteJson(writer, this.value, jsonSerializer);
                }

                // marshal from JSON to VW
                using (var reader = new JsonTextReader(new StringReader(sb.ToString())))
                {
                    jsonBuilder.Parse(reader, ctx, new Namespace(ctx.VW, feature.Name));
                }
            }
        }
        private bool HandleMultiProperty(VowpalWabbitJsonParseState state, string property)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;
            if (!property.Equals(multiPropertyName, StringComparison.OrdinalIgnoreCase))
                return false;

            var reader = state.Reader;
            if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                throw new VowpalWabbitJsonException(reader, "Expected start array for '" + multiPropertyName + "'");

            if (this.ExampleBuilders == null)
                this.ExampleBuilders = new List<VowpalWabbitJsonBuilder>();

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                    case JsonToken.StartObject:
                        VowpalWabbitJsonBuilder builder = null;

                        try
                        {
                            builder = new VowpalWabbitJsonBuilder(this, this.vwPool, VowpalWabbitDefaultMarshaller.Instance, this.jsonSerializer);
                            this.ExampleBuilders.Add(builder);
                        }
                        catch (Exception)
                        {
                            builder.Dispose();
                            throw;
                        }

                        // pass the label to the selected example
                        builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null, this.extensions);
                        break;
                    case JsonToken.EndArray:
                        return true;
                    default:
                        throw new VowpalWabbitJsonException(reader, "Unexpected token: " + reader.TokenType);
                }
            }

            throw new VowpalWabbitJsonException(reader, "Unexpected end");
        }
        /// <summary>
        /// Parses the example.
        /// </summary>
        /// <param name="reader">The example to parse.</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 Parse(JsonReader reader, ILabel label = null, int? index = null)
        {
            // only pass the label if it's not targeted at a particular index
            this.ExampleBuilder.Parse(reader, index == null ? label : null,
                propertyName =>
                {
                    if (propertyName != "_multi")
                        return false;

                    if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                        throw new VowpalWabbitJsonException(reader.Path, "Expected start array for _multi");

                    if (this.ExampleBuilders == null)
                        this.ExampleBuilders = new List<VowpalWabbitJsonBuilder>();

                    while (reader.Read())
                    {
                        switch (reader.TokenType)
                        {
                            case JsonToken.StartObject:
                                VowpalWabbitJsonBuilder builder = null;

                                try
                                {
                                    builder = new VowpalWabbitJsonBuilder(this.vw, this.defaultMarshaller, this.jsonSerializer);
                                    this.ExampleBuilders.Add(builder);
                                }
                                catch (Exception)
                                {
                                    builder.Dispose();
                                    throw;
                                }

                                // pass the label to the selected example
                                builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null);
                                break;
                            case JsonToken.EndArray:
                                return true;
                            default:
                                throw new VowpalWabbitJsonException(reader.Path, "Unexpected token: " + reader.TokenType);
                        }
                    }

                    throw new VowpalWabbitJsonException(reader.Path, "Unexpected end");
                });
        }
        /// <summary>
        /// Parses the example.
        /// </summary>
        /// <param name="reader">The example to parse.</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 Parse(JsonReader reader, ILabel label = null, int? index = null)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;

            // only pass the label if it's not targeted at a particular index
            this.ExampleBuilder.Parse(reader, index == null ? label : null,
                propertyName =>
                {
                    if (propertyName != multiPropertyName)
                        return false;

                    if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                        throw new VowpalWabbitJsonException(reader, "Expected start array for '" + multiPropertyName + "'");

                    if (this.ExampleBuilders == null)
                        this.ExampleBuilders = new List<VowpalWabbitJsonBuilder>();

                    while (reader.Read())
                    {
                        switch (reader.TokenType)
                        {
                            case JsonToken.StartObject:
                                VowpalWabbitJsonBuilder builder = null;

                                try
                                {
                                    builder = new VowpalWabbitJsonBuilder(this, this.vwPool, this.defaultMarshaller, this.jsonSerializer);
                                    this.ExampleBuilders.Add(builder);
                                }
                                catch (Exception)
                                {
                                    builder.Dispose();
                                    throw;
                                }

                                // pass the label to the selected example
                                builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null);
                                break;
                            case JsonToken.EndArray:
                                return true;
                            default:
                                throw new VowpalWabbitJsonException(reader, "Unexpected token: " + reader.TokenType);
                        }
                    }

                    throw new VowpalWabbitJsonException(reader, "Unexpected end");
                });

            // check if the outer example foudn a label
            if (this.ExampleBuilder.Label != null)
            {
                VowpalWabbitDefaultMarshaller.Instance.MarshalLabel(
                    this.ExampleBuilders[this.ExampleBuilder.LabelIndex].DefaultNamespaceContext,
                    this.ExampleBuilder.Label);
            }
        }