blob: b4654f8c24dc9520858eeab71015315512591559 [file] [log] [blame]
Romain Guyc5347272009-05-20 10:37:13 -07001import java.io.File;
2import java.io.BufferedReader;
3import java.io.FileReader;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.io.Closeable;
7import java.io.DataOutputStream;
8import java.io.FileOutputStream;
9import java.io.DataInputStream;
10import java.io.FileInputStream;
11import java.io.BufferedInputStream;
12
13/**
14 * Converts text-based letter stores to binary-based stores.
15 */
16public class Converter {
17 private final File mFile;
18
19 Converter(File file) {
20 mFile = file;
21 }
22
23 private void convert() {
24 boolean read = false;
25
26 String[] classes = null;
27 int iCount = 0;
28 int hCount = 0;
29 int oCount = 0;
30 float[][] iWeights = null;
31 float[][] oWeights = null;
32
33 BufferedReader reader = null;
34
35 try {
36 reader = new BufferedReader(new FileReader(mFile));
37
38 long start = System.nanoTime();
39
40 String line = reader.readLine();
41 int startIndex = 0;
42 int endIndex;
43 endIndex = line.indexOf(" ", startIndex);
44 iCount = Integer.parseInt(line.substring(startIndex, endIndex));
45
46 startIndex = endIndex + 1;
47 endIndex = line.indexOf(" ", startIndex);
48 hCount = Integer.parseInt(line.substring(startIndex, endIndex));
49
50 startIndex = endIndex + 1;
51 endIndex = line.length();
52 oCount = Integer.parseInt(line.substring(startIndex, endIndex));
53
54 classes = new String[oCount];
55 line = reader.readLine();
56 startIndex = 0;
57
58 for (int i = 0; i < oCount; i++) {
59 endIndex = line.indexOf(" ", startIndex);
60 classes[i] = line.substring(startIndex, endIndex);
61 startIndex = endIndex + 1;
62 }
63
64 iWeights = new float[hCount][];
65 for (int i = 0; i < hCount; i++) {
66 iWeights[i] = new float[iCount];
67 line = reader.readLine();
68 startIndex = 0;
69 for (int j = 0; j < iCount; j++) {
70 endIndex = line.indexOf(" ", startIndex);
71 iWeights[i][j] = Float.parseFloat(line.substring(startIndex, endIndex));
72 startIndex = endIndex + 1;
73 }
74 }
75
76 oWeights = new float[oCount][];
77 for (int i = 0; i < oCount; i++) {
78 oWeights[i] = new float[hCount];
79 line = reader.readLine();
80 startIndex = 0;
81 for (int j = 0; j < hCount; j++) {
82 endIndex = line.indexOf(" ", startIndex);
83 oWeights[i][j] = Float.parseFloat(line.substring(startIndex, endIndex));
84 startIndex = endIndex + 1;
85 }
86 }
87
88 long end = System.nanoTime();
89 System.out.println("time to read text file = " +
90 ((end - start) / 1000.0f / 1000.0f) + " ms");
91
92 read = true;
93 } catch (FileNotFoundException e) {
94 e.printStackTrace();
95 } catch (IOException e) {
96 e.printStackTrace();
97 } finally {
98 close(reader);
99 }
100
101 if (read) {
102 boolean wrote = false;
103 DataOutputStream out = null;
104
105 try {
106 out = new DataOutputStream(new FileOutputStream(mFile));
107
108 out.writeInt(iCount);
109 out.writeInt(hCount);
110 out.writeInt(oCount);
111
112 for (String aClass : classes) {
113 out.writeUTF(aClass);
114 }
115
116 for (float[] weights : iWeights) {
117 for (float weight : weights) {
118 out.writeFloat(weight);
119 }
120 }
121
122 for (float[] weights : oWeights) {
123 for (float weight : weights) {
124 out.writeFloat(weight);
125 }
126 }
127
128 out.flush();
129
130 wrote = true;
131 } catch (FileNotFoundException e) {
132 e.printStackTrace();
133 } catch (IOException e) {
134 e.printStackTrace();
135 } finally {
136 close(out);
137 }
138
139 if (wrote) {
140 DataInputStream in = null;
141
142 try {
143 in = new DataInputStream(new BufferedInputStream(new FileInputStream(mFile)));
144
145 long start = System.nanoTime();
146
147 iCount = in.readInt();
148 hCount = in.readInt();
149 oCount = in.readInt();
150
151 classes = new String[oCount];
152 for (int i = 0; i < classes.length; i++) {
153 classes[i] = in.readUTF();
154 }
155
156 iWeights = new float[hCount][];
157 for (int i = 0; i < iWeights.length; i++) {
158 iWeights[i] = new float[iCount];
159 for (int j = 0; j < iCount; j++) {
160 iWeights[i][j] = in.readFloat();
161 }
162 }
163
164 oWeights = new float[oCount][];
165 for (int i = 0; i < oWeights.length; i++) {
166 oWeights[i] = new float[hCount];
167 for (int j = 0; j < hCount; j++) {
168 oWeights[i][j] = in.readFloat();
169 }
170 }
171
172 long end = System.nanoTime();
173 System.out.println("time to read binary file = " +
174 ((end - start) / 1000.0f / 1000.0f) + " ms");
175
176 } catch (FileNotFoundException e) {
177 e.printStackTrace();
178 } catch (IOException e) {
179 e.printStackTrace();
180 } finally {
181 close(in);
182 }
183 }
184 }
185 }
186
187 private static void close(Closeable reader) {
188 if (reader != null) {
189 try {
190 reader.close();
191 } catch (IOException e) {
192 e.printStackTrace();
193 }
194 }
195 }
196
197 public static void main(String[] args) {
198 String fileName = args[0];
199 if (fileName != null) {
200 File file = new File(fileName);
201 if (!file.exists()) {
202 printHelp(fileName);
203 } else {
204 new Converter(file).convert();
205 }
206 } else {
207 printHelp(null);
208 }
209 }
210
211 private static void printHelp(String name) {
212 if (name == null) {
213 System.out.println("You must specify the name of the file to convert:");
214 } else {
215 System.out.println("The specified file does not exist: " + name);
216 }
217 System.out.println("java Converter [filename]");
218 System.out.println("");
219 System.out.println("\t[filename]\tPath to the file to convert. The file is replaced by "
220 + "the conversion result.");
221 }
222}