Esempio n. 1
0
        public VowpalWabbitExampleCollection Serialize(TExample example, Interfaces.ILabel label = null, int?index = null)
        {
            VowpalWabbitExample shared = null;
            var adf = new List <VowpalWabbitExample>();

            try
            {
                if (this.sharedSerializer != null)
                {
                    shared = this.sharedSerializer.Serialize(example, SharedLabel.Instance);
                }

                var adfCollection = this.adfAccessor(example);
                if (adfCollection != null)
                {
                    var i = 0;
                    foreach (var adfExample in adfCollection)
                    {
                        adf.Add(this.adfSerializer.Serialize(adfExample, index != null && i == index ? label : null));
                        i++;
                    }
                }

                return(new VowpalWabbitMultiLineExampleCollection(this.vw, shared, adf.ToArray()));
            }
            catch (Exception)
            {
                if (shared != null)
                {
                    shared.Dispose();
                }

                foreach (var adfExample in adf)
                {
                    adfExample.Dispose();
                }

                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Serializes the given <paramref name="example"/> to VW string format.
        /// </summary>
        /// <param name="example">The example to serialize.</param>
        /// <param name="label">The label to serialize.</param>
        /// <param name="index">The optional index of the example, the <paramref name="label"/> should be attributed to.</param>
        /// <param name="dictionary">Dictionary used for dictify operation.</param>
        /// <param name="fastDictionary">Dictionary used for dictify operation.</param>
        /// <returns>The resulting VW string.</returns>
        public string SerializeToString(TExample example, Interfaces.ILabel label = null, int?index = null, Dictionary <string, string> dictionary = null, Dictionary <object, string> fastDictionary = null)
        {
            var sb = new StringBuilder();

            if (this.sharedSerializer != null)
            {
                sb.AppendLine(this.sharedSerializer.SerializeToString(example, SharedLabel.Instance, null, dictionary, fastDictionary));
            }

            var adfCollection = this.adfAccessor(example);

            if (adfCollection != null)
            {
                var i = 0;
                foreach (var adfExample in adfCollection)
                {
                    sb.AppendLine(this.adfSerializer.SerializeToString(adfExample, index != null && i == index ? label : null, null, dictionary, fastDictionary));
                    i++;
                }
            }

            return(sb.ToString());
        }