List of usage examples for com.google.common.io Closeables close
public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException
From source file:org.apache.mahout.classifier.sgd.PrintResourceOrFile.java
public static void main(String[] args) throws Exception { Preconditions.checkArgument(args.length == 1, "Must have a single argument that names a file or resource."); BufferedReader in = TrainLogistic.open(args[0]); try {//from ww w . j a v a2 s . co m String line; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { Closeables.close(in, true); } }
From source file:com.google.android.stardroid.data.AsciiToBinaryProtoWriter.java
public static void main(String[] args) throws IOException { if (args.length != 1 || !args[0].endsWith(".ascii")) { System.out.println("Usage: AsciiToBinaryProtoWriter <inputprefix>.ascii"); System.exit(1);//from w ww .j ava 2s . c o m } FileReader in = null; FileOutputStream out = null; try { in = new FileReader(args[0]); AstronomicalSourcesProto.Builder builder = AstronomicalSourcesProto.newBuilder(); TextFormat.merge(in, builder); out = new FileOutputStream(args[0].substring(0, args[0].length() - 6) + ".binary"); AstronomicalSourcesProto sources = builder.build(); System.out.println("Source count " + sources.getSourceCount()); sources.writeTo(out); } finally { Closeables.closeQuietly(in); Closeables.close(out, false); } }
From source file:org.apache.mahout.cf.taste.example.kddcup.ToCSV.java
public static void main(String[] args) throws Exception { File inputFile = new File(args[0]); File outputFile = new File(args[1]); int columnsToOutput = 4; if (args.length >= 3) { columnsToOutput = Integer.parseInt(args[2]); }//from ww w . j a v a 2 s .c o m OutputStream outStream = new GZIPOutputStream(new FileOutputStream(outputFile)); Writer outWriter = new BufferedWriter(new OutputStreamWriter(outStream, Charsets.UTF_8)); try { for (Pair<PreferenceArray, long[]> user : new DataFileIterable(inputFile)) { PreferenceArray prefs = user.getFirst(); long[] timestamps = user.getSecond(); for (int i = 0; i < prefs.length(); i++) { outWriter.write(String.valueOf(prefs.getUserID(i))); outWriter.write(','); outWriter.write(String.valueOf(prefs.getItemID(i))); if (columnsToOutput > 2) { outWriter.write(','); outWriter.write(String.valueOf(prefs.getValue(i))); } if (columnsToOutput > 3) { outWriter.write(','); outWriter.write(String.valueOf(timestamps[i])); } outWriter.write('\n'); } } } finally { Closeables.close(outWriter, false); } }
From source file:org.apache.mahout.clustering.display.DisplaySpectralKMeans.java
public static void main(String[] args) throws Exception { System.out.println("hello"); DistanceMeasure measure = new ManhattanDistanceMeasure(); Path samples = new Path(SAMPLES); Path output = new Path(OUTPUT); Path tempDir = new Path(TEMP); Configuration conf = new Configuration(); conf.set("mapred.job.tracker", "ajila-server-01:54311"); conf.set("fs.default.name", "hdfs://ajila-server-01:54310"); HadoopUtil.delete(conf, samples);//from w ww . java 2 s.c o m HadoopUtil.delete(conf, output); RandomUtils.useTestSeed(); DisplayClustering.generateSamples(); writeSampleData(samples); Path affinities = new Path(output, AFFINITIES); FileSystem fs = FileSystem.get(output.toUri(), conf); if (!fs.exists(output)) { fs.mkdirs(output); } Writer writer = null; try { writer = Files.newWriter(new File(affinities.toString()), Charsets.UTF_8); for (int i = 0; i < SAMPLE_DATA.size(); i++) { for (int j = 0; j < SAMPLE_DATA.size(); j++) { writer.write(i + "," + j + ',' + measure.distance(SAMPLE_DATA.get(i).get(), SAMPLE_DATA.get(j).get()) + '\n'); } } } finally { Closeables.close(writer, false); } int maxIter = 10; double convergenceDelta = 0.001; SpectralKMeansDriver.run(new Configuration(), affinities, output, SAMPLE_DATA.size(), 3, measure, convergenceDelta, maxIter, tempDir, false); new DisplaySpectralKMeans(); }
From source file:com.google.android.stardroid.data.AsciiProtoRewriter.java
public static void main(String[] args) throws IOException { if (args.length != 1 || !args[0].endsWith("_R.ascii")) { System.out.println("Usage: AsciiToBinaryProtoWriter <inputprefix>_R.ascii"); System.exit(1);//from w ww . ja va 2 s .co m } String inputFile = args[0]; String outputFile = args[0].replaceAll("_R.ascii", ".ascii"); BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new FileReader(inputFile)); out = new PrintWriter(new FileWriter(outputFile)); String s; while ((s = in.readLine()) != null) { if (s.contains("REMOVE")) { try { s = s.replaceAll("REMOVE_", ""); String[] tokens = s.split("\\s+"); for (String token : tokens) { if (token.contains("R.string")) { int value = getString(token); s = s.replaceAll(token, "" + value); } } } catch (MissingStringException m) { System.out.println("Warning: " + m.getMessage() + ". Skipping..."); continue; } } out.println(s); } } finally { Closeables.closeQuietly(in); Closeables.close(out, false); } }
From source file:org.apache.mahout.cf.taste.example.kddcup.track1.Track1Runner.java
public static void main(String[] args) throws Exception { File dataFileDirectory = new File(args[0]); if (!dataFileDirectory.exists() || !dataFileDirectory.isDirectory()) { throw new IllegalArgumentException("Bad data file directory: " + dataFileDirectory); }/*w ww . j av a 2 s.c o m*/ long start = System.currentTimeMillis(); KDDCupDataModel model = new KDDCupDataModel(KDDCupDataModel.getTrainingFile(dataFileDirectory)); Track1Recommender recommender = new Track1Recommender(model); long end = System.currentTimeMillis(); log.info("Loaded model in {}s", (end - start) / 1000); start = end; Collection<Track1Callable> callables = Lists.newArrayList(); for (Pair<PreferenceArray, long[]> tests : new DataFileIterable( KDDCupDataModel.getTestFile(dataFileDirectory))) { PreferenceArray userTest = tests.getFirst(); callables.add(new Track1Callable(recommender, userTest)); } int cores = Runtime.getRuntime().availableProcessors(); log.info("Running on {} cores", cores); ExecutorService executor = Executors.newFixedThreadPool(cores); List<Future<byte[]>> results = executor.invokeAll(callables); executor.shutdown(); end = System.currentTimeMillis(); log.info("Ran recommendations in {}s", (end - start) / 1000); start = end; OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(args[1]))); try { for (Future<byte[]> result : results) { for (byte estimate : result.get()) { out.write(estimate); } } } finally { Closeables.close(out, false); } end = System.currentTimeMillis(); log.info("Wrote output in {}s", (end - start) / 1000); }
From source file:org.apache.mahout.classifier.sequencelearning.hmm.RandomSequenceGenerator.java
public static void main(String[] args) throws IOException { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option outputOption = optionBuilder.withLongName("output") .withDescription("Output file with sequence of observed states").withShortName("o") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("path").create()) .withRequired(false).create(); Option modelOption = optionBuilder.withLongName("model").withDescription("Path to serialized HMM model") .withShortName("m") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("path").create()) .withRequired(true).create(); Option lengthOption = optionBuilder.withLongName("length").withDescription("Length of generated sequence") .withShortName("l") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("number").create()) .withRequired(true).create(); Group optionGroup = new GroupBuilder().withOption(outputOption).withOption(modelOption) .withOption(lengthOption).withName("Options").create(); try {/*www. ja va 2 s . c o m*/ Parser parser = new Parser(); parser.setGroup(optionGroup); CommandLine commandLine = parser.parse(args); String output = (String) commandLine.getValue(outputOption); String modelPath = (String) commandLine.getValue(modelOption); int length = Integer.parseInt((String) commandLine.getValue(lengthOption)); //reading serialized HMM DataInputStream modelStream = new DataInputStream(new FileInputStream(modelPath)); HmmModel model; try { model = LossyHmmSerializer.deserialize(modelStream); } finally { Closeables.close(modelStream, true); } //generating observations int[] observations = HmmEvaluator.predict(model, length, System.currentTimeMillis()); //writing output PrintWriter writer = new PrintWriter( new OutputStreamWriter(new FileOutputStream(output), Charsets.UTF_8), true); try { for (int observation : observations) { writer.print(observation); writer.print(' '); } } finally { Closeables.close(writer, false); } } catch (OptionException e) { CommandLineUtil.printHelp(optionGroup); } }
From source file:org.apache.mahout.classifier.sequencelearning.hmm.ViterbiEvaluator.java
public static void main(String[] args) throws IOException { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputOption = DefaultOptionCreator.inputOption().create(); Option outputOption = DefaultOptionCreator.outputOption().create(); Option modelOption = optionBuilder.withLongName("model").withDescription("Path to serialized HMM model") .withShortName("m") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("path").create()) .withRequired(true).create(); Option likelihoodOption = optionBuilder.withLongName("likelihood") .withDescription("Compute likelihood of observed sequence").withShortName("l").withRequired(false) .create();/*w w w .j a va 2s .c o m*/ Group optionGroup = new GroupBuilder().withOption(inputOption).withOption(outputOption) .withOption(modelOption).withOption(likelihoodOption).withName("Options").create(); try { Parser parser = new Parser(); parser.setGroup(optionGroup); CommandLine commandLine = parser.parse(args); String input = (String) commandLine.getValue(inputOption); String output = (String) commandLine.getValue(outputOption); String modelPath = (String) commandLine.getValue(modelOption); boolean computeLikelihood = commandLine.hasOption(likelihoodOption); //reading serialized HMM DataInputStream modelStream = new DataInputStream(new FileInputStream(modelPath)); HmmModel model; try { model = LossyHmmSerializer.deserialize(modelStream); } finally { Closeables.close(modelStream, true); } //reading observations List<Integer> observations = Lists.newArrayList(); Scanner scanner = new Scanner(new FileInputStream(input), "UTF-8"); try { while (scanner.hasNextInt()) { observations.add(scanner.nextInt()); } } finally { scanner.close(); } int[] observationsArray = new int[observations.size()]; for (int i = 0; i < observations.size(); ++i) { observationsArray[i] = observations.get(i); } //decoding int[] hiddenStates = HmmEvaluator.decode(model, observationsArray, true); //writing output PrintWriter writer = new PrintWriter( new OutputStreamWriter(new FileOutputStream(output), Charsets.UTF_8), true); try { for (int hiddenState : hiddenStates) { writer.print(hiddenState); writer.print(' '); } } finally { Closeables.close(writer, false); } if (computeLikelihood) { System.out.println("Likelihood: " + HmmEvaluator.modelLikelihood(model, observationsArray, true)); } } catch (OptionException e) { CommandLineUtil.printHelp(optionGroup); } }
From source file:SimpleCsvExamples.java
public static void main(String[] args) throws IOException { FeatureVectorEncoder[] encoder = new FeatureVectorEncoder[FIELDS]; for (int i = 0; i < FIELDS; i++) { encoder[i] = new ConstantValueEncoder("v" + 1); }/*from w ww.j a v a 2 s . c om*/ OnlineSummarizer[] s = new OnlineSummarizer[FIELDS]; for (int i = 0; i < FIELDS; i++) { s[i] = new OnlineSummarizer(); } long t0 = System.currentTimeMillis(); Vector v = new DenseVector(1000); if ("--generate".equals(args[0])) { PrintWriter out = new PrintWriter( new OutputStreamWriter(new FileOutputStream(new File(args[2])), Charsets.UTF_8)); try { int n = Integer.parseInt(args[1]); for (int i = 0; i < n; i++) { Line x = Line.generate(); out.println(x); } } finally { Closeables.close(out, false); } } else if ("--parse".equals(args[0])) { BufferedReader in = Files.newReader(new File(args[1]), Charsets.UTF_8); double total = 0; try { String line = in.readLine(); while (line != null) { v.assign(0); Line x = new Line(line); for (int i = 0; i < FIELDS; i++) { double z = x.getDouble(i); total += z; //s[i].add(x.getDouble(i)); encoder[i].addToVector(x.get(i), v); } line = in.readLine(); } } finally { Closeables.close(in, true); } // String separator = ""; // for (int i = 0; i < FIELDS; i++) { // System.out.printf("%s%.3f", separator, s[i].getMean()); // separator = ","; // } System.out.println("total: " + total); } else if ("--fast".equals(args[0])) { FastLineReader in = new FastLineReader(new FileInputStream(args[1])); double total = 0; try { FastLine line = in.read(); while (line != null) { v.assign(0); for (int i = 0; i < FIELDS; i++) { double z = line.getDouble(i); total += z; //s[i].add(z); encoder[i].addToVector((byte[]) null, z, v); } line = in.read(); } } finally { Closeables.close(in, true); } // String separator = ""; // for (int i = 0; i < FIELDS; i++) { // System.out.printf("%s%.3f", separator, s[i].getMean()); // separator = ","; // } System.out.println("total: " + total); } System.out.printf("\nElapsed time = %.3f%n", (System.currentTimeMillis() - t0) / 1000.0); }
From source file:org.apache.mahout.cf.taste.example.kddcup.track2.Track2Runner.java
public static void main(String[] args) throws Exception { File dataFileDirectory = new File(args[0]); if (!dataFileDirectory.exists() || !dataFileDirectory.isDirectory()) { throw new IllegalArgumentException("Bad data file directory: " + dataFileDirectory); }//w w w. j a v a 2 s. c o m long start = System.currentTimeMillis(); KDDCupDataModel model = new KDDCupDataModel(KDDCupDataModel.getTrainingFile(dataFileDirectory)); Track2Recommender recommender = new Track2Recommender(model, dataFileDirectory); long end = System.currentTimeMillis(); log.info("Loaded model in {}s", (end - start) / 1000); start = end; Collection<Track2Callable> callables = Lists.newArrayList(); for (Pair<PreferenceArray, long[]> tests : new DataFileIterable( KDDCupDataModel.getTestFile(dataFileDirectory))) { PreferenceArray userTest = tests.getFirst(); callables.add(new Track2Callable(recommender, userTest)); } int cores = Runtime.getRuntime().availableProcessors(); log.info("Running on {} cores", cores); ExecutorService executor = Executors.newFixedThreadPool(cores); List<Future<UserResult>> futures = executor.invokeAll(callables); executor.shutdown(); end = System.currentTimeMillis(); log.info("Ran recommendations in {}s", (end - start) / 1000); start = end; OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(args[1]))); try { long lastUserID = Long.MIN_VALUE; for (Future<UserResult> future : futures) { UserResult result = future.get(); long userID = result.getUserID(); if (userID <= lastUserID) { throw new IllegalStateException(); } lastUserID = userID; out.write(result.getResultBytes()); } } finally { Closeables.close(out, false); } end = System.currentTimeMillis(); log.info("Wrote output in {}s", (end - start) / 1000); }