@@ -0,0 +1,1934 @@
+package cc.mrlda;%0A%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Aimport org.apache.hadoop.fs.FileSystem;%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.apache.hadoop.io.IOUtils;%0Aimport org.apache.hadoop.io.IntWritable;%0Aimport org.apache.hadoop.io.SequenceFile;%0Aimport org.apache.hadoop.io.Text;%0Aimport org.apache.hadoop.mapred.JobConf;%0A%0Apublic class TopicUtils %7B%0A%09public static int%5B%5D sortedIndices(double%5B%5D vals)%7B%0A%09%09int%5B%5D indices = new int%5Bvals.length%5D;%0A%09%09Set%3CInteger%3E availIndices = new HashSet%3CInteger%3E(vals.length);%0A%09%09for(int i = 0; i %3C vals.length; i++)%7B%0A%09%09%09availIndices.add(new Integer(i));%0A%09%09%7D%0A%09%09int key = 0;%0A%09%09while(!availIndices.isEmpty())%7B%0A%09%09%09Integer maxIdx = new Integer(-1);%0A%09%09%09Iterator%3CInteger%3E j = availIndices.iterator();%0A%09%09%09while(j.hasNext())%7B%0A%09%09%09%09Integer idx = j.next();%0A%09%09%09%09if(maxIdx.intValue() %3C 0 %7C%7C vals%5Bidx.intValue()%5D %3E vals%5BmaxIdx.intValue()%5D)%7B%0A%09%09%09%09%09maxIdx = idx;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09indices%5Bkey%5D=maxIdx.intValue();%0A%09%09%09key++;%0A%09%09%09availIndices.remove(maxIdx);%0A%09%09%7D%0A%09%09return indices;%0A%09%7D%0A%0A%09public static int maxIndex(double%5B%5D vals)%7B%0A%09%09int maxIdx = -1;%0A%09%09for(int i = 0; i %3C vals.length; i++)%7B%0A%09%09%09if(maxIdx %3C 0 %7C%7C vals%5BmaxIdx%5D %3C vals%5Bi%5D)%7B%0A%09%09%09%09maxIdx = i;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return maxIdx;%0A%09%7D%0A%09public static Map%3CInteger,String%3E initializeIndex(FileSystem fs, JobConf conf, Path indexPath) throws Exception%7B%0A%09%09SequenceFile.Reader sequenceFileReader = null;%0A%09%09Map%3CInteger, String%3E index = new HashMap%3CInteger, String%3E();%0A%09%09try %7B%0A%09%09%09IntWritable intWritable = new IntWritable();%0A%09%09%09Text text = new Text();%0A%09%09%09sequenceFileReader = new SequenceFile.Reader(fs, indexPath, conf);%0A%09%09%09while (sequenceFileReader.next(intWritable, text)) %7B%0A%09%09%09%09index.put(intWritable.get(),text.toString());%0A%09%09%09%7D%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09System.err.println(%22Error loading index %22+e.getMessage());%0A%09%09%7D finally %7B%0A%09%09%09IOUtils.closeStream(sequenceFileReader);%0A%09%09%7D%0A%09%09return index;%0A%09%7D%0A%09%0A%09%0A%7D%0A

Put some common function into a Utils class for better reuse