/** * Given the path to a file representing the text based serialization of a Linear Classifier, * reconstitutes and returns that LinearClassifier. * * <p>TODO: Leverage Index */ public static LinearClassifier<String, String> loadFromFilename(String file) { try { BufferedReader in = IOUtils.readerFromString(file); // Format: read indices first, weights, then thresholds Index<String> labelIndex = HashIndex.loadFromReader(in); Index<String> featureIndex = HashIndex.loadFromReader(in); double[][] weights = new double[featureIndex.size()][labelIndex.size()]; int currLine = 1; String line = in.readLine(); while (line != null && line.length() > 0) { String[] tuples = line.split(LinearClassifier.TEXT_SERIALIZATION_DELIMITER); if (tuples.length != 3) { throw new Exception( "Error: incorrect number of tokens in weight specifier, line=" + currLine + " in file " + file); } currLine++; int feature = Integer.valueOf(tuples[0]); int label = Integer.valueOf(tuples[1]); double value = Double.valueOf(tuples[2]); weights[feature][label] = value; line = in.readLine(); } // First line in thresholds is the number of thresholds int numThresholds = Integer.valueOf(in.readLine()); double[] thresholds = new double[numThresholds]; int curr = 0; while ((line = in.readLine()) != null) { double tval = Double.valueOf(line.trim()); thresholds[curr++] = tval; } in.close(); LinearClassifier<String, String> classifier = new LinearClassifier<String, String>(weights, featureIndex, labelIndex); return classifier; } catch (Exception e) { System.err.println("Error in LinearClassifierFactory, loading from file=" + file); e.printStackTrace(); return null; } }