/// <summary> /// Initializes a new <see cref="ArffAttribute"/> instance with the specified name and attribute type. /// </summary> /// <param name="name">The name of the attribute to create.</param> /// <param name="type">The type of the attribute to create.</param> /// <exception cref="ArgumentNullException"/> public ArffAttribute(string name, ArffAttributeType type) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } Name = name; Type = type; }
private object ParseValue(string value, bool quoted, ArffAttributeType attributeType) { if (!quoted && value == "?") { return(null); } if (attributeType == ArffAttributeType.Numeric) { if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double d)) { throw new InvalidDataException($"Unrecognized data value: \"{value}\""); } return(d); } else if (attributeType == ArffAttributeType.String) { return(value); } else if (attributeType is ArffNominalAttribute nominalAttribute) { int index = nominalAttribute.Values.IndexOf(value); if (index == -1) { throw new InvalidDataException($"Unrecognized data value: \"{value}\""); } return(index); } else if (attributeType is ArffDateAttribute dateAttribute) { if (!DateTime.TryParseExact(value, dateAttribute.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out DateTime d)) { throw new InvalidDataException($"Unrecognized data value: \"{value}\""); } return(d); } else if (attributeType is ArffRelationalAttribute relationalAttribute) { List <object[]> relationalInstances = new List <object[]>(); using (StringReader stringReader = new StringReader(value)) while (true) { // weights for relational instances are currently discarded object[] instance = ReadInstance(out double?instanceWeight, relationalAttribute.ChildAttributes, stringReader); if (instance == null) { break; } relationalInstances.Add(instance); } return(relationalInstances.ToArray()); } else { throw new ArgumentException("Unsupported ArffAttributeType.", nameof(attributeType)); } }
private ArffAttribute ReadAttribute() { string attributeName = ReadToken(endOfLine: false); string typeString = ReadToken(endOfLine: false, quoting: false); ArffAttributeType attributeType; if (string.Equals(typeString, "numeric", StringComparison.OrdinalIgnoreCase) || string.Equals(typeString, "integer", StringComparison.OrdinalIgnoreCase) || string.Equals(typeString, "real", StringComparison.OrdinalIgnoreCase)) { attributeType = ArffAttributeType.Numeric; ReadToken(endOfLine: true); } else if (string.Equals(typeString, "string", StringComparison.OrdinalIgnoreCase)) { attributeType = ArffAttributeType.String; ReadToken(endOfLine: true); } else if (string.Equals(typeString, "date", StringComparison.OrdinalIgnoreCase)) { string dateFormat = ReadToken(); if (dateFormat == null) { attributeType = ArffAttributeType.Date(); } else { attributeType = ArffAttributeType.Date(dateFormat); ReadToken(endOfLine: true); } } else if (typeString == "{") { List <string> nominalValues = new List <string>(); while (true) { string value = ReadToken(out bool quoted, endOfLine: false); if (!quoted && value == "}") { break; } else if (!quoted && value == ",") { continue; } else { nominalValues.Add(value); } } attributeType = ArffAttributeType.Nominal(nominalValues); ReadToken(endOfLine: true); } else if (string.Equals(typeString, "relational", StringComparison.OrdinalIgnoreCase)) { ReadToken(endOfLine: true); List <ArffAttribute> childAttributes = new List <ArffAttribute>(); while (true) { string token = ReadToken(skipEndOfLine: true, endOfLine: false, quoting: false); if (string.Equals(token, "@attribute", StringComparison.OrdinalIgnoreCase)) { ArffAttribute attribute = ReadAttribute(); childAttributes.Add(attribute); } else if (string.Equals(token, "@end", StringComparison.OrdinalIgnoreCase)) { ReadToken(expectedToken: attributeName, endOfLine: false); ReadToken(endOfLine: true); break; } else { throw new InvalidDataException($"Unexpected token \"{token}\". Expected \"@attribute\" or \"@end\"."); } } attributeType = ArffAttributeType.Relational(childAttributes); } else { throw new InvalidDataException($"Unexpected token \"{typeString}\". Expected attribute type."); } return(new ArffAttribute(attributeName, attributeType)); }