-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuesv.cs
178 lines (161 loc) · 5.82 KB
/
Guesv.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Guesv
{
public class Guesv
{
public readonly string[] Header;
public readonly string[][] Data;
public Dictionary<string, string> this[int index]
{
get
{
var ret = new Dictionary<string, string>();
for (int i = 0; i < data[index].Count; i++)
{
ret.Add(header[i], data[index][i]);
}
return ret;
}
}
public Guesv(string filename)
{
this.filename = filename;
LoadFile(filename, true);
foreach (var kv in header2i)
{
var values = data.Select(x => x[kv.Value]);
if (values.All(x => !String.IsNullOrEmpty(x)))
{ //all rows have a value in this column
allFilledHeaders.Add(kv.Key);
if (values.Distinct().Count() == data.Count())
//each value is unique
allFilledAndUniqueHeaders.Add(kv.Key);
}
examples.Add(kv.Key, values.FirstOrDefault(x => !String.IsNullOrEmpty(x)));
}
Header = header.ToArray();
Data = data.Select(x=>x.ToArray()).ToArray();
}
private List<string> header;
private Dictionary<string, int> header2i;
private int RowCount;
private string filename;
private const int SAMPLE_LINES = 5;
private const int BULK_INSERT_ROWS = 10000;
private List<List<string>> data;
private char realsep;
private List<string> allFilledHeaders = new List<string>();
private List<string> allFilledAndUniqueHeaders = new List<string>();
private Dictionary<string, string> examples = new Dictionary<string, string>();
private bool doublequoted = false;
private void LoadFile(string filename, bool onlySample = false)
{
string wholefile = File.ReadAllText(filename);
GuessSeparator(wholefile);
int i = 0;
header = GetLine(wholefile, ref i);
if (header.Count == 1)
{ //only one column? Filemaker pro csv files can have a line like
//"0,""pGEX-6p-1-hUSP7CD-HUBL C223A WE285DA"",""hUSP7"",""pGEX-6p-1"""
doublequoted = true;
i = 0;
header = GetLine(wholefile, ref i);
}
InitHeader();
data = GetData(wholefile, i, onlySample);
RowCount = data.Count;
}
private List<string> GetRow(int row)
{
return data[row];
}
private void EnsureWholeFileLoaded()
{
LoadFile(filename);
}
private List<List<string>> GetData(string wholefile, int i, bool onlySample = false)
{
var data = new List<List<string>>();
while (true)
{
var line = GetLine(wholefile, ref i);
if (line.Count == 0)
break; //EOF
data.Add(line);
if (onlySample && data.Count > SAMPLE_LINES)
break;
}
return data;
}
private List<string> GetLine(string wholefile, ref int i)
{
var line = new List<string>();
bool quoted = false;
int start = i;
for (; true; i++)
{
if (wholefile.Length <= i)
break;
if (wholefile[i] == '"')
{
if (doublequoted)
{
if (i < wholefile.Length - 1 && wholefile[i + 1] == '"')
{
quoted = !quoted;
i++; //skip the second quote as well
}
}
else
quoted = !quoted;
continue;
}
if (wholefile[i] == '\r')
continue;
if (!quoted && wholefile[i] == '\n')
break;
if (!quoted && wholefile[i] == realsep)
{
line.Add(wholefile.Substring(start, i - start).Replace("\n", " ").Replace("\"", "").Replace("\r", ""));
start = i + 1;
continue;
}
}
if (i > start)
line.Add(wholefile.Substring(start, i - start - 1).Replace("\n", " ").Replace("\"", "").Replace("\r", ""));
i++;
return line;
}
private void InitHeader()
{
header2i = new Dictionary<string, int>();
for (int i = 0; i < header.Count; i++)
{
header2i.Add(header[i], i);
}
}
private void GuessSeparator(string wholefile)
{
var firstline = wholefile.Substring(0, wholefile.IndexOf("\n"));
var secondline = wholefile.Substring(firstline.Length, wholefile.IndexOf("\n", firstline.Length));
var seps = new char[] { '\t', ',', ';', ':' };
realsep = seps[0];
int mindiff = Int32.MaxValue;
foreach (var sep in seps)
{
var sepsin1stline = firstline.Count(x => x == sep);
var sepsin2ndline = secondline.Count(x => x == sep);
if (sepsin1stline < 2 || sepsin2ndline < 2)
continue;
if (Math.Abs(sepsin1stline - sepsin2ndline) < mindiff)
{
realsep = sep;
mindiff = Math.Abs(sepsin1stline - sepsin2ndline);
}
}
}
}
}