/// <summary> /// Reads labeled feature values from a file with the specified name. /// </summary> /// <param name="fileName">The file name.</param> /// <param name="labelSet">An optional set of labels.</param> /// <param name="featureSet">An optional set of features.</param> /// <returns>A list of labeled feature values.</returns> public static IList <LabeledFeatureValues> LoadLabeledFeatureValues( string fileName, IndexedSet <string> labelSet = null, IndexedSet <string> featureSet = null) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("The name of the file must not be null or whitespace.", nameof(fileName)); } var labeledFeatureValues = new List <LabeledFeatureValues>(); var labelDictionary = labelSet ?? new IndexedSet <string>(); var featureDictionary = featureSet ?? 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 { labeledFeatureValues.Add(LabeledFeatureValues.Parse(line, labelDictionary, featureDictionary)); } catch (Exception e) { parsingContext.RaiseError("{0}", e.Message); } } } return(labeledFeatureValues); }
/// <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); }