Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 1 | import java.io.File; |
| 2 | import java.io.BufferedReader; |
| 3 | import java.io.FileReader; |
| 4 | import java.io.FileNotFoundException; |
| 5 | import java.io.IOException; |
| 6 | import java.io.Closeable; |
| 7 | import java.io.DataOutputStream; |
| 8 | import java.io.FileOutputStream; |
| 9 | import java.io.DataInputStream; |
| 10 | import java.io.FileInputStream; |
| 11 | import java.io.BufferedInputStream; |
| 12 | |
| 13 | /** |
| 14 | * Converts text-based letter stores to binary-based stores. |
| 15 | */ |
| 16 | public class Converter { |
| 17 | private final File mFile; |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 18 | private static final short VERSION_NUMBER = 1; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 19 | |
| 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 Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 67 | iWeights[i] = new float[iCount + 1]; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 68 | line = reader.readLine(); |
| 69 | startIndex = 0; |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 70 | for (int j = 0; j <= iCount; j++) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 71 | 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 Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 79 | oWeights[i] = new float[hCount + 1]; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 80 | line = reader.readLine(); |
| 81 | startIndex = 0; |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 82 | for (int j = 0; j <= hCount; j++) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 83 | 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 Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 109 | out.writeShort(VERSION_NUMBER); |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 110 | 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 | } |