Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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.", "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);
        }