-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstdlib.ijs
More file actions
221 lines (193 loc) · 7.13 KB
/
stdlib.ijs
File metadata and controls
221 lines (193 loc) · 7.13 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
smoutput=: (150!:0)&'Console.WriteLine(v.ToString());return new MicroJ.A<MicroJ.JString>(0);'
NB. fib test
fibtestx =: 3 : 0
x1 =. (3!:101) 1 NB. convert to bignum
x2 =. (3!:101) 1
for_c. i. (y-2) do. NB. !hotspot
tmp =. x1
x1 =. x2
x2 =. tmp + x1 NB. type:BigInteger
end.
x2
)
NB. fib test
fibtest =: 3 : 0
x1 =. 1
x2 =. 1
for_c. i. (y-2) do.
tmp =. x1
x1 =. x2
x2 =. tmp + x1
end.
x2
)
NB. requires LumenWorks.Framework.IO.dll
NB. https://www.nuget.org/packages/LumenWorksCsvReader/
readcsvCode =: 0 : 0
//css_ref LumenWorks.Framework.IO.dll
//css_ref System.Data
//css_using MicroJ
//css_using System.IO
//css_using System.Linq
//css_using System.Collections.Generic
//css_using LumenWorks.Framework.IO.Csv
//css_using System.Text.RegularExpressions
var y = v as MicroJ.A<Box>;
string fileName = ((A<JString>)y.Ravel[0].val).Ravel[0].str;
string tableName = ((A<JString>)y.Ravel[1].val).Ravel[0].str;
var optionsDict = new Dictionary<string, string>();
if (y.Ravel.Length >= 3) {
optionsDict = AHelper.ToOptions((A<Box>)y.Ravel[2].val);
}
bool keepLeadingZero = optionsDict.ContainsKey("KeepLeadingZero");
var newNames = new List<string>();
var limit = optionsDict.ContainsKey("limit") ? Int64.Parse(optionsDict["limit"]) : Int64.MaxValue;
var delimiter = !optionsDict.ContainsKey("delimiter") ? ',' : Char.Parse(optionsDict["delimiter"].Replace("\\t", "\t"));
HashSet<string> keepColumns = null;
if (optionsDict.ContainsKey("cols")) {
keepColumns = new HashSet<string>();
keepColumns.UnionWith(optionsDict["cols"].Split(','));
}
var iLine = 0;
var types = new int[500];
string[] headers = null;
int fieldCount = 0;
using (var csv = new CsvReader(new StreamReader(fileName), true, delimiter)) {
headers = csv.GetFieldHeaders();
fieldCount = csv.FieldCount;
var total = 0;
while (csv.ReadNextRecord() && iLine++ < 10000 && iLine < limit)
{
for(var k = 0; k < fieldCount;k++)
{
int n = 0;
double d = 0;
if (csv[k] == "") continue;
if ((types[k] == 0 || types[k] == 3) && Int32.TryParse(csv[k], out n)) {
if (keepLeadingZero && csv[k].StartsWith("0") && csv[k].Length > 1) {
types[k] = 1;
} else {
types[k] = 3;
}
}
else if ((types[k] == 0 || types[k] == 2) && Double.TryParse(csv[k], out d))
{
types[k] = 2;
}
else {
types[k] = 1;
}
}
}
/*
for (int i = 0; i < fieldCount; i++) {
Console.WriteLine(string.Format("{0} = {1}; Type={2}",
headers[i], csv[i], types[i]));
}
*/
}
var finalColumnNames = new List<string>();
var boxedRows = new List<Box>();
using (var csv = new CsvReader(new StreamReader(fileName), true, delimiter)) {
csv.Columns = new List<LumenWorks.Framework.IO.Csv.Column>();
for(var k = 0; k < fieldCount; k++) {
Type t = null;
if (types[k] == 2) { t = typeof(long); }
else if (types[k] == 3) { t = typeof(double); }
else { t = typeof(string); }
csv.Columns.Add(new Column { Name = headers[k], Type = t });
}
csv.ReadNextRecord();
var strings = new Dictionary<string, List<string>>();
var longs = new Dictionary<string, List<long>>();
var doubles = new Dictionary<string, List<double>>();
var rowCount = 0;
while (csv.ReadNextRecord()) {
if (rowCount >= limit) { break; }
rowCount++;
for (int i = 0; i < csv.FieldCount; i++) {
var columnType = types[i];
var columnName = headers[i];
if (keepColumns != null && !keepColumns.Contains(columnName)) { continue; }
if (columnType == 3) {
if (!longs.ContainsKey(columnName)) {
longs[columnName] = new List<long>();
}
long lv;
if (!Int64.TryParse(csv[i] != "" ? csv[i] : "0", out lv)) {
Console.WriteLine("bad data: " + csv[i]);
}
longs[columnName].Add(lv);
}
else if (columnType == 1) {
if (!strings.ContainsKey(columnName)) {
strings[columnName] = new List<string>();
}
var sv = csv[i];
strings[columnName].Add(sv);
}
else if (columnType == 2) {
if (!doubles.ContainsKey(columnName)) {
doubles[columnName] = new List<double>();
}
double dv = Double.Parse(csv[i] != "" ? csv[i] : "0");
doubles[columnName].Add(dv);
}
/*
else if (columnType == "System.DateTime") {
if (!strings.ContainsKey(columnName)) {
strings[columnName] = new List<string>();
}
DateTime dv = (DateTime) (reader.IsDBNull(i) ? DateTime.MinValue : reader.GetDateTime(i));
strings[columnName].Add(dv.ToString("yyyy-MM-dd"));
}
*/
}
}
Func<string, string> createName = s => {
return Regex.Replace(s, @"[^A-Za-z]+", "") + "_" + tableName + "_";
};
foreach(var col in longs.Keys) {
var jname = new MicroJ.A<long>(rowCount) { Ravel = longs[col].ToArray() };
var newName = createName(col);
newNames.Add(newName);
parser.Names[newName] = jname;
finalColumnNames.Add(col);
boxedRows.Add(new Box { val = jname });
}
foreach(var col in doubles.Keys) {
var jname = new MicroJ.A<double>(rowCount) { Ravel = doubles[col].ToArray() };
var newName = createName(col);
newNames.Add(newName);
parser.Names[newName] = jname;
finalColumnNames.Add(col);
boxedRows.Add(new Box { val = jname });
}
foreach(var col in strings.Keys) {
var max = strings[col].Select(x=>x.Length).Max();
var jname = new MicroJ.A<JString>(rowCount, new long[] { rowCount, max }) { Ravel = strings[col].Select(x=>new MicroJ.JString { str = String.Intern(x.PadRight(max)) }).ToArray() };
var newName = createName(col);
newNames.Add(newName);
parser.Names[newName] = jname;
finalColumnNames.Add(col);
boxedRows.Add(new Box { val = jname });
}
/*
foreach(var newName in newNames) {
Console.WriteLine(newName);
}
*/
}
//return new MicroJ.A<MicroJ.JString>(new long[] { newNames.Count, 100 }) { Ravel = newNames.Select(x=>new MicroJ.JString { str = x }).ToArray() };
return new JTable {
Columns = finalColumnNames.ToArray(),
Rows = boxedRows.ToArray()
}.WrapA();
)
NB. reads a csv file delimited by comma and writes names to global namespace for each column prefixed by table parameter
NB. auto-detects type from first 1000 rows
NB. parameter: box of filename;tablename
NB. example: readcsv 'abc.csv';'abc'
NB. readcsv 'abc.txt';'abc';<(('delimiter';'\t'),.('limit';10))
readcsv =: (150!:0) & readcsvCode
flip =: (3!:102)