/// <summary>
        /// Parses a specified string for a label distribution.
        /// </summary>
        /// <param name="specification">The string to be parsed for a label distribution.</param>
        /// <param name="labelSet">A bidirectional mapping from labels to label indexes.</param>
        /// <param name="unparsedSpecification">The unparsed part of <see cref="specification"/>.</param>
        /// <returns>A label distribution that is equivalent to the given string <see cref="specification"/>.</returns>
        public static LabelDistribution Parse(string specification, IndexedSet <string> labelSet, out string unparsedSpecification)
        {
            var labelDistribution = new LabelDistribution();

            if (labelSet != null)
            {
                labelDistribution.LabelSet = labelSet;
            }

            unparsedSpecification = labelDistribution.ParseLabelDistribution(specification);

            return(labelDistribution);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a specified string for a label and its feature values.
        /// </summary>
        /// <param name="specification">The string to be parsed for a label and its feature values.</param>
        /// <param name="labelSet">An optional bidirectional mapping from class labels to class indexes.</param>
        /// <param name="featureSet">An optional bidirectional mapping from feature names to feature indexes.</param>
        /// <returns>Labeled feature values equivalent to the given string specification.</returns>
        public static LabeledFeatureValues Parse(
            string specification,
            IndexedSet <string> labelSet   = null,
            IndexedSet <string> featureSet = null)
        {
            var labeledFeatureValues = new LabeledFeatureValues();

            if (featureSet != null)
            {
                labeledFeatureValues.FeatureSet = featureSet;
            }

            // Parse label distribution
            labeledFeatureValues.LabelDistribution = LabelDistribution.Parse(specification, labelSet, out specification);

            // Parse feature values
            labeledFeatureValues.ParseFeatures(specification);

            return(labeledFeatureValues);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads label distributions from a file with the specified name.
        /// </summary>
        /// <param name="fileName">The file name.</param>
        /// <param name="labelSet">An optional set of labels.</param>
        /// <returns>A list of label distributions.</returns>
        public static IList <LabelDistribution> LoadLabelDistributions(string fileName, IndexedSet <string> labelSet = null)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("The name of the file must not be null or whitespace.", nameof(fileName));
            }

            var labels          = new List <LabelDistribution>();
            var labelDictionary = labelSet ?? new IndexedSet <string>();
            var parsingContext  = new FileParsingContext(fileName);

            using (var reader = new StreamReader(fileName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    line = line.Trim();
                    if (line.StartsWith("#") || line.StartsWith("//") || line.StartsWith("%"))
                    {
                        continue;
                    }

                    try
                    {
                        labels.Add(LabelDistribution.Parse(line, labelDictionary));
                    }
                    catch (Exception e)
                    {
                        parsingContext.RaiseError("{0}", e.Message);
                    }
                }
            }

            return(labels);
        }