Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ public class Options
[Argument(ArgumentType.AtMostOnce, Name = nameof(Separator), Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly, HelpText = "Source column separator.", ShortName = "sep")]
public char[] Separators = new[] { Defaults.Separator };

/// <summary>
/// The character that should be used as the decimal marker.
/// </summary>
[Argument(ArgumentType.AtMostOnce, Name = "Decimal Marker", HelpText = "Character symbol used to separate the integer part from the fractional part of a number written in decimal form.", ShortName = "decimal")]
public char DecimalMarker = Defaults.DecimalMarker;
Comment thread
mstfbl marked this conversation as resolved.
Comment thread
mstfbl marked this conversation as resolved.

/// <summary>
/// Specifies the input columns that should be mapped to <see cref="IDataView"/> columns.
/// </summary>
Expand Down Expand Up @@ -535,6 +541,7 @@ internal static class Defaults
internal const bool AllowQuoting = false;
internal const bool AllowSparse = false;
internal const char Separator = '\t';
internal const char DecimalMarker = '.';
internal const bool HasHeader = false;
internal const bool TrimWhitespace = false;
internal const bool ReadMultilines = false;
Expand Down Expand Up @@ -1063,7 +1070,8 @@ private static VersionInfo GetVersionInfo()
// verWrittenCur: 0x00010009, // Introduced _flags
//verWrittenCur: 0x0001000A, // Added ForceVector in Range
//verWrittenCur: 0x0001000B, // Header now retained if used and present
verWrittenCur: 0x0001000C, // Removed Min and Contiguous from KeyType, and added ReadMultilines flag to OptionFlags
//verWrittenCur: 0x0001000C, // Removed Min and Contiguous from KeyType, and added ReadMultilines flag to OptionFlags
verWrittenCur: 0x0001000D, // Added decimal marker option to allow for ',' to be a decimal marker
Comment thread
mstfbl marked this conversation as resolved.
Outdated
verReadableCur: 0x0001000A,
verWeCanReadBack: 0x00010009,
loaderSignature: LoaderSignature,
Expand Down Expand Up @@ -1094,6 +1102,7 @@ private enum OptionFlags : uint
// Input size is zero for unknown - determined by the data (including sparse rows).
private readonly int _inputSize;
private readonly char[] _separators;
private readonly char _decimalMarker;
private readonly Bindings _bindings;

private readonly Parser _parser;
Expand Down Expand Up @@ -1210,6 +1219,9 @@ internal TextLoader(IHostEnvironment env, Options options = null, IMultiStreamSo
}
}

if (options.DecimalMarker == ',' && _separators.Contains<char>(','))
throw _host.ExceptUserArg(nameof(Options.DecimalMarker), "Decimal marker and separator cannot be the same '{0}' character.", options.DecimalMarker);
Comment thread
mstfbl marked this conversation as resolved.
Outdated
_decimalMarker = options.DecimalMarker;
Comment thread
mstfbl marked this conversation as resolved.
_bindings = new Bindings(this, cols, headerFile, dataSample);
_parser = new Parser(this);
}
Expand Down Expand Up @@ -1373,6 +1385,7 @@ private TextLoader(IHost host, ModelLoadContext ctx)
// int: inputSize: 0 for determined from data
// int: number of separators
// char[]: separators
// char: decimal marker
// bindings
int cbFloat = ctx.Reader.ReadInt32();
host.CheckDecode(cbFloat == sizeof(float));
Expand All @@ -1397,6 +1410,8 @@ private TextLoader(IHost host, ModelLoadContext ctx)
if (_separators.Contains(':'))
host.CheckDecode((_flags & OptionFlags.AllowSparse) == 0);

_decimalMarker = ctx.Reader.ReadChar();
host.CheckDecode(_decimalMarker == '.' || _decimalMarker == ',');
_bindings = new Bindings(ctx, this);
_parser = new Parser(this);
}
Expand Down Expand Up @@ -1437,6 +1452,7 @@ void ICanSaveModel.Save(ModelSaveContext ctx)
// int: inputSize: 0 for determined from data
// int: number of separators
// char[]: separators
// char: decimal marker
// bindings
ctx.Writer.Write(sizeof(float));
ctx.Writer.Write(_maxRows);
Expand All @@ -1445,6 +1461,7 @@ void ICanSaveModel.Save(ModelSaveContext ctx)
_host.Assert(0 <= _inputSize && _inputSize < SrcLim);
ctx.Writer.Write(_inputSize);
ctx.Writer.WriteCharArray(_separators);
ctx.Writer.Write(_decimalMarker);

_bindings.Save(ctx);
}
Expand Down Expand Up @@ -1473,12 +1490,14 @@ internal static TextLoader CreateTextLoader<TInput>(IHostEnvironment host,
bool allowQuoting = Defaults.AllowQuoting,
bool supportSparse = Defaults.AllowSparse,
bool trimWhitespace = Defaults.TrimWhitespace,
IMultiStreamSource dataSample = null)
IMultiStreamSource dataSample = null,
char decimalMarker = Defaults.DecimalMarker)
{
Comment thread
mstfbl marked this conversation as resolved.
Options options = new Options
{
HasHeader = hasHeader,
Separators = new[] { separator },
DecimalMarker = decimalMarker,
AllowQuoting = allowQuoting,
AllowSparse = supportSparse,
TrimWhitespace = trimWhitespace
Expand Down
12 changes: 12 additions & 0 deletions test/BaselineOutput/Common/EntryPoints/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@
"\t"
]
},
{
"Name": "Decimal Marker",
"Type": "Char",
"Desc": "Character symbol used to separate the integer part from the fractional part of a number written in decimal form.",
"Aliases": [
"decimal"
],
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": "."
},
{
"Name": "TrimWhitespace",
"Type": "Bool",
Expand Down
151 changes: 151 additions & 0 deletions test/data/iris_decimal_marker_as_comma.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#Label Sepal length Sepal width Petal length Petal width
0 5,1 3,5 1,4 0,2
0 4,9 3,0 1,4 0,2
Comment thread
mstfbl marked this conversation as resolved.
0 4,7 3,2 1,3 0,2
0 4,6 3,1 1,5 0,2
0 5,0 3,6 1,4 0,2
0 5,4 3,9 1,7 0,4
0 4,6 3,4 1,4 0,3
0 5,0 3,4 1,5 0,2
0 4,4 2,9 1,4 0,2
0 4,9 3,1 1,5 0,1
0 5,4 3,7 1,5 0,2
0 4,8 3,4 1,6 0,2
0 4,8 3,0 1,4 0,1
0 4,3 3,0 1,1 0,1
0 5,8 4,0 1,2 0,2
0 5,7 4,4 1,5 0,4
0 5,4 3,9 1,3 0,4
0 5,1 3,5 1,4 0,3
0 5,7 3,8 1,7 0,3
0 5,1 3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