/** * Read predictions from a CSV file. * @param buf A CSV file reader. * @return The item scorer. */ public static PrecomputedItemScorer fromCSV(@WillClose BufferedReader buf) { StringTokenizer tok = new StringTokenizer((String) null, ","); Builder bld = new Builder(); try (ObjectStream<List<String>> rows = new LineStream(buf).tokenize(tok)) { for (List<String> row : rows) { // FIXME Add error checking long user = Long.parseLong(row.get(0)); long item = Long.parseLong(row.get(1)); double score = Double.parseDouble(row.get(2)); bld.addScore(user, item, score); } } return bld.build(); }
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file) throws FileNotFoundException { // REVIEW do we want to use the default charset? return new LineStream(Files.newReader(file, Charset.defaultCharset())); }
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file, CompressionMode comp) throws IOException { FileInputStream fin = new FileInputStream(file); InputStream rawin = comp.getEffectiveCompressionMode(file.getName()).wrapInput(fin); // REVIEW do we want to use the default charset? Reader reader = new InputStreamReader(rawin, Charset.defaultCharset()); BufferedReader buffer = new BufferedReader(reader); return new LineStream(buffer); }
/** * Read predictions from a CSV file. * @param buf A CSV file reader. * @return The item scorer. */ public static PrecomputedItemScorer fromCSV(@WillClose BufferedReader buf) { StrTokenizer tok = new StrTokenizer((String) null, ","); Builder bld = new Builder(); try (ObjectStream<List<String>> rows = new LineStream(buf).tokenize(tok)) { for (List<String> row : rows) { // FIXME Add error checking long user = Long.parseLong(row.get(0)); long item = Long.parseLong(row.get(1)); double score = Double.parseDouble(row.get(2)); bld.addScore(user, item, score); } } return bld.build(); }
/** * Open a stream to read entities from this source. * @return A stream of entities. */ @Override public ObjectStream<Entity> openStream() throws IOException { BufferedReader reader = source.openBufferedStream(); ObjectStream<String> lines = new LineStream(reader); int headerLines = format.getHeaderLines(); List<String> header = new ArrayList<>(); while (header.size() < headerLines) { String line = lines.readObject(); if (line == null) { IOException ex = new IOException(String.format("%s: expected %d header lines, found %d", sourceURL, headerLines, header.size())); try { lines.close(); } catch (Throwable th) { ex.addSuppressed(th); } throw ex; } header.add(line); } LineEntityParser parser = format.makeParser(header); return ObjectStreams.transform(lines, parser); }
try (InputStreamReader rdr = new InputStreamReader(proc.getInputStream()); BufferedReader buf = new BufferedReader(rdr); ObjectStream<List<String>> rows = new LineStream(buf).tokenize(tok)) { for (List<String> row: rows) {
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file) throws FileNotFoundException { // REVIEW do we want to use the default charset? return new LineStream(Files.newReader(file, Charset.defaultCharset())); }
/** * Open a delimited text stream as a file. * * @param file The file to open. * @return The stream. * @throws FileNotFoundException if there is an error opening the file. */ public static LineStream openFile(File file, CompressionMode comp) throws IOException { FileInputStream fin = new FileInputStream(file); InputStream rawin = comp.getEffectiveCompressionMode(file.getName()).wrapInput(fin); // REVIEW do we want to use the default charset? Reader reader = new InputStreamReader(rawin, Charset.defaultCharset()); BufferedReader buffer = new BufferedReader(reader); return new LineStream(buffer); }
/** * Open a stream to read entities from this source. * @return A stream of entities. */ @Override public ObjectStream<Entity> openStream() throws IOException { BufferedReader reader = source.openBufferedStream(); ObjectStream<String> lines = new LineStream(reader); int headerLines = format.getHeaderLines(); List<String> header = new ArrayList<>(); while (header.size() < headerLines) { String line = lines.readObject(); if (line == null) { IOException ex = new IOException(String.format("%s: expected %d header lines, found %d", sourceURL, headerLines, header.size())); try { lines.close(); } catch (Throwable th) { ex.addSuppressed(th); } throw ex; } header.add(line); } LineEntityParser parser = format.makeParser(header); return ObjectStreams.transform(lines, parser); }