/** * Returns an unmodifiable view of the Index. It is just a locked index that cannot be unlocked, * so if you try to add something, nothing will happen (it won't throw an exception). Trying to * unlock it will throw an UnsupportedOperationException. If the underlying Index is modified, the * change will "write-through" to the view. * * @return An unmodifiable view of the Index */ public HashIndex<E> unmodifiableView() { HashIndex<E> newIndex = new HashIndex<E>(objects, indexes) { @Override public void unlock() { throw new UnsupportedOperationException("This is an unmodifiable view!"); } private static final long serialVersionUID = 3415903369787491736L; }; newIndex.lock(); return newIndex; }
/** * This is the analogue of {@code loadFromFilename}, and is intended to be included in a routine * that unpacks a text-serialized form of an object that incorporates an Index. NOTE: presumes * that the next readLine() will read in the first line of the portion of the text file * representing the saved Index. Currently reads until it encounters a blank line, consuming that * line and returning the Index. TODO: figure out how best to terminate: currently a blank line is * considered to be a terminator. * * @param br The Reader to read the index from * @return An Index read from a file */ public static Index<String> loadFromReader(BufferedReader br) throws IOException { HashIndex<String> index = new HashIndex<String>(); String line = br.readLine(); // terminate if EOF reached, or if a blank line is encountered. while ((line != null) && (line.length() > 0)) { int start = line.indexOf('='); if (start == -1 || start == line.length() - 1) { continue; } index.add(line.substring(start + 1)); line = br.readLine(); } return index; }
/** * 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; } }