package markpeng.kaggle;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.mahout.common.Pair;
import org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable;
import org.apache.mahout.math.NamedVector;
import org.apache.mahout.math.SequentialAccessSparseVector;
import org.apache.mahout.math.VectorWritable;
import org.apache.mahout.math.Vector.Element;

public class TfIdfVectorToCsv {
	private static final int BUFFER_LENGTH = 1000;
	private static final String newLine = System.getProperty("line.separator");

	public static class WordWeight implements Comparable<WordWeight> {
		private int wordId;
		private double weight;

		public WordWeight(int wordId, double weight) {
			this.wordId = wordId;
			this.weight = weight;
		}

		public int getWordId() {
			return wordId;
		}

		public Double getWeight() {
			return weight;
		}

		@Override
		public int compareTo(WordWeight w) {
			return -getWeight().compareTo(w.getWeight());
		}
	}

	public static void main(String[] args) throws Exception {
		if (args.length < 2) {
			System.out.println("Arguments:[tfidfPath] [csvPath]");
			return;
		}
		String tfidfPath = args[0];
		String csvPath = args[1];

		Configuration conf = new Configuration();
		System.out.println("Loading TF/IDF file ......");

		int count = 0;
		File checker = new File(tfidfPath);
		if (checker.isDirectory()) {
			for (final File fileEntry : checker.listFiles()) {
				if (fileEntry.getName().startsWith("part-r-")) {
					String filePath = fileEntry.getAbsolutePath();

					StringBuffer outputStr = new StringBuffer();
					BufferedWriter out = new BufferedWriter(
							new OutputStreamWriter(new FileOutputStream(
									csvPath, false), "UTF-8"));
					try {
						FileSystem fs = FileSystem.get(conf);
						SequenceFile.Reader read = new SequenceFile.Reader(fs,
								new Path(filePath), conf);
						try {
							Text key = new Text();
							VectorWritable value = new VectorWritable();
							SequentialAccessSparseVector vect;
							while (read.next(key, value)) {
								NamedVector namedVector = (NamedVector) value
										.get();
								vect = (SequentialAccessSparseVector) namedVector
										.getDelegate();
								String name = namedVector.getName();
								String label = name.substring(1, 2);
								outputStr.append(label + " ");

								System.out.println(name + " ==> " + label);

								Iterator<Element> looper = vect.iterator();
								while (looper.hasNext()) {
									Element e = looper.next();
									int word_id = e.index();
									double tfidf = e.get();
									outputStr.append(word_id + ":" + tfidf
											+ " ");
								}

								outputStr.append(newLine);
								if (outputStr.length() >= BUFFER_LENGTH) {
									out.write(outputStr.toString());
									out.flush();
									outputStr.setLength(0);
								}

								count++;
							}
						} finally {
							read.close();
						}

					} finally {
						out.write(outputStr.toString());
						out.flush();
						out.close();
					}
				}
			}
		}
		System.out.println("Number of processed documents in training set: "
				+ count);
	}
}

@@ -108,36 +108,8 @@
 am;%0A
-import java.io.IOException;%0A
 impo
@@ -155,600 +155,249 @@
 ava.
-util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A%0Aimport org.apache.hadoop.conf.Configuration;%0Aimport org.apache.hadoop.fs.FileSystem;%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.apache.hadoop.io.IntWritable;%0Aimport org.apache.hadoop.io.LongWritable;%0Aimport org.apache.hadoop.io.SequenceFile;%0Aimport org.apache.hadoop.io.Text;%0Aimport org.apache.mahout.common.Pair;%0Aimport org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable
+text.DecimalFormat;%0Aimport java.util.Iterator;%0A%0Aimport org.apache.hadoop.conf.Configuration;%0Aimport org.apache.hadoop.fs.FileSystem;%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.apache.hadoop.io.SequenceFile;%0Aimport org.apache.hadoop.io.Text
 ;%0Aim
@@ -533,24 +533,24 @@
 h.Vector
-Writable
+.Element
 ;%0Aimport
@@ -579,24 +579,24 @@
 h.Vector
-.Element
+Writable
 ;%0A%0Apubli
@@ -747,32 +747,32 @@
 ator%22);%0A
-%0A
 %09p
-ublic
+rivate
  static 
 class Wo
@@ -767,422 +767,69 @@
 tic 
-class WordWeight implements Comparable%3CWordWeight%3E %7B%0A%09%09private int wordId;%0A%09%09private double weight;%0A%0A%09%09public WordWeight(int wordId, double weight) %7B%0A%09%09%09this.wordId = wordId;%0A%09%09%09this.weight = weight;%0A%09%09%7D%0A%0A%09%09public int getWordId() %7B%0A%09%09%09return wordId;%0A%09%09%7D%0A%0A%09%09public Double getWeight() %7B%0A%09%09%09return weight;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public int compareTo(WordWeight w) %7B%0A%09%09%09return -getWeight().compareTo(w.getWeight());%0A%09%09%7D%0A%09%7D
+final DecimalFormat formatter = new DecimalFormat(%22#.#####%22);
 %0A%0A%09p
@@ -2468,16 +2468,8 @@
  %22:%22
- + tfidf
 %0A%09%09%09
@@ -2477,16 +2477,42 @@
 %09%09%09%09%09%09%09+
+ formatter.format(tfidf) +
  %22 %22);%0A%09

Add DecimalFormat to control precision.