Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding better selection of separator in header #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions Sources/DataAccess/DataTableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,27 @@ public static MutableDataTable GetMutableCopy(this DataTableBuilder builder, Dat
/// <param name="builder">ignored</param>
/// <param name="filename">filename of table to load. Schema is inferred from header row.</param>
/// <returns>a in-memory table containing the topN rows from the supplied file.</returns>
public static MutableDataTable ReadSampleTopN(this DataTableBuilder builder, string filename)
public static MutableDataTable ReadSampleTopN(this DataTableBuilder builder, string filename, char columnSeparator = default (char))
{
return ReadSampleTopN(builder, filename, 100);
return ReadSampleTopN(builder, filename, columnSeparator, 100);
}

/// <summary>
/// Return an in-memory table that contains the topN rows from the table in the filename.
/// </summary>
/// <param name="builder">ignored</param>
/// <param name="filename">filename of table to load. Schema is inferred from header row.</param>
/// <param name="topN">reads the topN rows from the table.</param>
/// <returns>a in-memory table containing the topN rows from the supplied file.</returns>
public static MutableDataTable ReadSampleTopN(this DataTableBuilder builder, string filename, int topN = 100)
public static MutableDataTable ReadSampleTopN(this DataTableBuilder builder, string filename, char columnSeparator = default(char), int topN = 100)
{
Debug.Assert(builder != null);
if (filename == null)
{
throw new ArgumentNullException("filename");
}

DataTable source = new FileStreamingDataTable(filename);
DataTable source = new FileStreamingDataTable(filename, columnSeparator);
MutableDataTable dt = Analyze.SampleTopN(source, topN);
return dt;
}
Expand All @@ -147,11 +147,11 @@ public static MutableDataTable ReadSampleTopN(this DataTableBuilder builder, str
/// <param name="builder"></param>
/// <param name="filename">filename of CSV to read</param>
/// <returns>a streaming data table for the given filename</returns>
public static DataTable ReadLazy(this DataTableBuilder builder, string filename)
public static DataTable ReadLazy(this DataTableBuilder builder, string filename, char columnSeparator = default(char))
{
Debug.Assert(builder != null);

return new FileStreamingDataTable(filename) { Name = filename };
return new FileStreamingDataTable(filename, columnSeparator) { Name = filename };
}

/// <summary>
Expand All @@ -161,14 +161,13 @@ public static DataTable ReadLazy(this DataTableBuilder builder, string filename)
/// <param name="builder"></param>
/// <param name="inputStream">input stream. Must be seekable and readable</param>
/// <returns>a streaming data table for the given filename</returns>
public static DataTable ReadLazy(this DataTableBuilder builder, Stream inputStream)
public static DataTable ReadLazy(this DataTableBuilder builder, Stream inputStream, char columnSeparator = default(char))
{
Debug.Assert(builder != null);

return new StreamingDataTable(inputStream);
return new StreamingDataTable(inputStream, columnSeparator);
}


/// <summary>
/// Create an in-memory table with 2 columns (key and value), where each row is a KeyValuePair from the dictionary.
/// </summary>
Expand Down
29 changes: 11 additions & 18 deletions Sources/DataAccess/Readers.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace DataAccess
{

Expand Down Expand Up @@ -225,24 +227,15 @@ public static MutableDataTable Read(TextReader stream, char delimiter = '\0')

public static char GuessSeparateFromHeaderRow(string header)
{
if (header.Contains("\t"))
{
return '\t';
}

if (header.Contains(","))
{
return ',';
}

if (header.Contains(";"))
{
return ';';
}

// Fallback is always comma. This implies a single column.
return ',';

var validSeparators = new[] { '\t', ',', ';' };

var firstSeparator =
(from x in validSeparators.Select(c => new { separator = c, index = header.IndexOf(c) })
where x.index >= 0
orderby x.index
select x.separator).ToList();

return firstSeparator.Any() ? firstSeparator.FirstOrDefault() : ',';
}

// Read in a Ascii file that uses the given separate characters.
Expand Down
28 changes: 18 additions & 10 deletions Sources/DataAccess/StreamingDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ internal class StreamingDataTable : TextReaderDataTable
{
readonly Stream _input;

public StreamingDataTable(Stream input)
public StreamingDataTable(Stream input, char columnSeparator)
: base(columnSeparator)
{
// We could optimize to avoid requiring CanSeek if we failed on attemps
// to read the the rows multiple times.
Expand All @@ -30,7 +31,7 @@ protected override TextReader OpenText()
{
_input.Position = 0;


return new StreamReader(_input);
}
protected override void CloseText(TextReader reader)
Expand All @@ -43,8 +44,9 @@ protected override void CloseText(TextReader reader)
internal class FileStreamingDataTable : TextReaderDataTable
{
private readonly string _filename;

public FileStreamingDataTable(string filename)

public FileStreamingDataTable(string filename, char columnSeparator)
: base(columnSeparator)
{
_filename = filename;
}
Expand All @@ -65,9 +67,15 @@ protected override void CloseText(TextReader reader)
/// </summary>
internal abstract class TextReaderDataTable : DataTable
{
private readonly char columnSeparator;

private string[] _names;


protected TextReaderDataTable(char columnSeparator)
{
this.columnSeparator = columnSeparator;
}

public override IEnumerable<string> ColumnNames
{
get
Expand All @@ -77,10 +85,10 @@ public override IEnumerable<string> ColumnNames
TextReader sr = null;
try
{
sr = this.OpenText();
sr = this.OpenText();
// First get columns.
string header = sr.ReadLine();
char ch = Reader.GuessSeparateFromHeaderRow(header);
char ch = this.columnSeparator == default(char) ? Reader.GuessSeparateFromHeaderRow(header) : this.columnSeparator;
_names = Reader.split(header, ch);
}
finally
Expand All @@ -100,7 +108,7 @@ public override IEnumerable<string> ColumnNames
// called on reader from OpenText
// Don't call dipose because that can close streams.
protected abstract void CloseText(TextReader reader);

public override IEnumerable<Row> Rows
{
get
Expand All @@ -113,7 +121,7 @@ public override IEnumerable<Row> Rows
sr = this.OpenText();

string header = sr.ReadLine(); // skip past header
char chSeparator = Reader.GuessSeparateFromHeaderRow(header);
char chSeparator = this.columnSeparator == default(char) ? Reader.GuessSeparateFromHeaderRow(header) : this.columnSeparator;

int illegal = 0;
string line;
Expand All @@ -124,7 +132,7 @@ public override IEnumerable<Row> Rows
{

string[] parts = Reader.split(line, chSeparator);

// $$$ Major hack for dealing with newlines in quotes strings.
// The better fix here would be to switch to a streaming interface.
if (parts.Length != columnCount)
Expand Down