Example #1
0
        /// <summary>
        /// Expects that actual feature value.
        /// </summary>
        private void ParseFeature(VowpalWabbitMarshalContext context, Namespace ns, string featureName)
        {
            switch (reader.TokenType)
            {
            case JsonToken.Float:
                VowpalWabbitMarshalActions.Marshal(this.defaultMarshaller, context, ns, featureName, (double)reader.Value);
                break;

            case JsonToken.Integer:
                VowpalWabbitMarshalActions.Marshal(this.defaultMarshaller, context, ns, featureName, (long)reader.Value);
                break;

            case JsonToken.String:
                VowpalWabbitMarshalActions.Marshal(this.defaultMarshaller, context, ns, featureName, (string)reader.Value);
                break;

            case JsonToken.Boolean:
                VowpalWabbitMarshalActions.Marshal(this.defaultMarshaller, context, ns, featureName, (bool)reader.Value);
                break;

            case JsonToken.Comment:
            case JsonToken.Null:
                // probably best to ignore?
                break;

            case JsonToken.StartArray:
                this.ParseFeatureArray(context, ns);
                break;

            default:
                throw new VowpalWabbitJsonException(this.reader, "Unexpected token " + reader.TokenType + " while deserializing primitive feature");
            }
        }
Example #2
0
        private IVowpalWabbitMarshalAction ParseFeatureReUsable()
        {
            // make sure the returned action is independent of the current parsing context, so we can ship it
            switch (reader.TokenType)
            {
            case JsonToken.Float:
                return(VowpalWabbitMarshalActions.Create((double)reader.Value));

            case JsonToken.Integer:
                return(VowpalWabbitMarshalActions.Create((long)reader.Value));

            case JsonToken.String:
                return(VowpalWabbitMarshalActions.Create((string)reader.Value));

            case JsonToken.Boolean:
                return(VowpalWabbitMarshalActions.Create((bool)reader.Value));

            case JsonToken.Comment:
            case JsonToken.Null:
                // probably best to ignore?
                break;

            case JsonToken.StartArray:
                return(this.ParseFeatureArrayReUsable());
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Expects: "1,2.2,3]" (excluding the leading [)
        /// </summary>
        private IVowpalWabbitMarshalAction ParseFeatureArrayReUsable()
        {
            var values = new float[16];
            var index  = 0;

            while (reader.Read())
            {
                float val;
                switch (reader.TokenType)
                {
                case JsonToken.Integer:
                    val = (float)(long)reader.Value;
                    break;

                case JsonToken.Float:
                    val = (float)(double)reader.Value;
                    break;

                case JsonToken.EndArray:
                    goto done;

                default:
                    throw new VowpalWabbitJsonException(this.reader, "Unxpected token " + reader.TokenType + " while deserializing dense feature array");
                }

                if (index == values.Length)
                {
                    var newValues = new float[values.Length * 2];
                    Array.Copy(values, newValues, values.Length);
                    values = newValues;
                }

                values[index++] = val;
            }
done:

            return(VowpalWabbitMarshalActions.Create(values, index));
        }