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