This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
UnipenReader.cs
277 lines (232 loc) · 8.98 KB
/
UnipenReader.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Accord Formats Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.IO
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Accord.Compat;
/// <summary>
/// Reader for UNIPEN files (such as Pendigits dataset).
/// </summary>
///
public class UnipenReader : IDisposable
{
private TextReader reader;
private List<double[]> buffer;
/// <summary>
/// Gets the lexicon information that is usually included at the header of the file.
/// This should represent the different class labels that each sample can belong to.
/// </summary>
///
public string[] Lexicon { get; private set; }
/// <summary>
/// Gets the hierarchy iniformation that is usually included at the header of the file.
/// This usually gives a description of what the <see cref="Lexicon">class labels</see> represent.
/// </summary>
///
public string Hierarchy { get; private set; }
/// <summary>
/// Creates a new <see cref="UnipenReader"/>.
/// </summary>
///
/// <param name="path">The path for the IDX file.</param>
/// <param name="compressed">Pass <c>true</c> if the stream contains a compressed (.gz) file. Default is true.</param>
///
public UnipenReader(string path, bool compressed = true)
{
init(new FileStream(path, FileMode.Open, FileAccess.Read), compressed);
}
/// <summary>
/// Creates a new <see cref="UnipenReader"/>.
/// </summary>
///
/// <param name="input">The input stream containing the UNIPEN file.</param>
/// <param name="compressed">Pass <c>true</c> if the stream contains a compressed (.Z) file. Default is true.</param>
///
public UnipenReader(Stream input, bool compressed = true)
{
init(input, compressed);
}
private void init(Stream input, bool compressed = true)
{
if (compressed)
reader = new StreamReader(new Accord.IO.Compression.LzwInputStream(input));
else
reader = new StreamReader(input);
this.buffer = new List<double[]>();
string line;
do // Read the lexicon
{
line = reader.ReadLine();
} while (!line.StartsWith(".LEXICON"));
string[] parts = line.Split(' ');
Lexicon = new string[parts.Length - 1];
for (int i = 0; i < Lexicon.Length; i++)
Lexicon[i] = parts[i + 1].Trim().TrimStart('"').TrimEnd('"');
do // Read the hierarchy
{
line = reader.ReadLine();
} while (!line.StartsWith(".HIERARCHY"));
parts = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Hierarchy = parts[1];
if (parts.Length != 2)
throw new FormatException();
}
/// <summary>
/// Reads the next sequence.
/// </summary>
///
/// <param name="label">The class label of the sample that has been read.</param>
/// <param name="comment">The comment associated with the sample that has just been read.</param>
///
/// <returns>The sequence of (X,Y) coordinates stored in the file.</returns>
///
public double[][] Read(out string label, out string comment)
{
double[][] coordinates;
if (TryRead(out coordinates, out label, out comment))
return coordinates;
throw new FormatException();
}
private bool TryRead(out double[][] coordinates, out string label, out string comment)
{
coordinates = null;
label = null;
comment = null;
buffer.Clear();
string line;
do // Find the next ".SEGMENT" region
{
try
{
line = reader.ReadLine();
}
catch
{
line = null; // TODO: This try-catch block is only necessary because of the
// current version of SharpZipLib being used. This block can be removed after
// SharpZipLib NuGet's package is finally updated.
}
if (line == null)
return false;
} while (!line.StartsWith(".SEGMENT"));
// Get the label
label = line.Split('?').Last().Trim().TrimStart('"').TrimEnd('"');
if (!Lexicon.Contains(label))
return false;
// Read .COMMENT line
comment = reader.ReadLine().Replace(".COMMENT", "").Trim();
// Read .PEN_DOWN
line = reader.ReadLine();
if (line != ".PEN_DOWN")
return false;
do // Read until empty line
{
line = reader.ReadLine();
if (line == ".PEN_UP")
continue;
if (line == ".PEN_DOWN")
continue;
if (line.StartsWith(".DT"))
break;
string[] parts = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
return false;
double x = double.Parse(parts[0]);
double y = double.Parse(parts[1]);
buffer.Add(new[] { x, y });
#if NET35
} while (line != null && String.IsNullOrEmpty(line.Trim()));
#else
} while (!String.IsNullOrWhiteSpace(line));
#endif
coordinates = buffer.ToArray();
return true;
}
/// <summary>
/// Reads all samples in the file, starting from the current position.
/// </summary>
///
/// <returns>
/// An array containing all samples from the current point until the end of the stream.
/// </returns>
///
public Tuple<double[][][], string[]> ReadToEnd()
{
double[][] coordinates;
string label;
string comment;
var vectors = new List<double[][]>();
var labels = new List<string>();
while (TryRead(out coordinates, out label, out comment))
{
vectors.Add(coordinates);
labels.Add(label);
}
return Tuple.Create(vectors.ToArray(), labels.ToArray());
}
#region IDisposable members
/// <summary>
/// Performs application-defined tasks associated with
/// freeing, releasing, or resetting unmanaged resources.
/// </summary>
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
///
/// <param name="disposing"><c>true</c> to release both managed and unmanaged
/// resources; <c>false</c> to release only unmanaged resources.</param>
///
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
if (reader != null)
{
#if !NETSTANDARD1_4
reader.Close();
#endif
reader = null;
}
}
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="IdxReader"/> is reclaimed by garbage collection.
/// </summary>
///
~UnipenReader()
{
Dispose(false);
}
#endregion
}
}