00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package com.meapsoft.composers;
00024
00025 import gnu.getopt.Getopt;
00026
00027 import java.io.IOException;
00028 import java.text.NumberFormat;
00029 import java.util.Vector;
00030
00031 import com.meapsoft.ChunkDist;
00032 import com.meapsoft.EDLChunk;
00033 import com.meapsoft.EDLFile;
00034 import com.meapsoft.EuclideanDist;
00035 import com.meapsoft.FeatChunk;
00036 import com.meapsoft.FeatFile;
00037 import com.meapsoft.Heap;
00038 import com.meapsoft.MaxHeap;
00039 import com.meapsoft.MinHeap;
00040 import com.meapsoft.ParserException;
00041
00049 public class SortComposer extends Composer
00050 {
00051 public static String description = "Sorts the " +
00052 "features in ascending or descending " +
00053 "order. If there are multiple features, " +
00054 "or more than one value per feature, it " +
00055 "sorts according to distance in" +
00056 " Euclidean space.";
00057
00058 String outFileName = "sorted.edl";
00059
00060 FeatFile featFile;
00061 int[] featdim = null;
00062 ChunkDist dist;
00063 boolean reverseSort = false;
00064 boolean debug = false;
00065
00066 public SortComposer(String featFN, String outFN)
00067 {
00068 this(featFN, outFN, new EuclideanDist());
00069 }
00070
00071 public SortComposer(FeatFile featFN, EDLFile outFN)
00072 {
00073 this(featFN, outFN, new EuclideanDist());
00074 }
00075
00076 public SortComposer(String featFN, String outFN, ChunkDist cd)
00077 {
00078 this(new FeatFile(featFN), new EDLFile(outFN), cd);
00079 }
00080
00081 public SortComposer(FeatFile featFN, EDLFile outFN, ChunkDist cd)
00082 {
00083 featFile = featFN;
00084 outFile = outFN;
00085 dist = cd;
00086 featdim = cd.featdim;
00087
00088 if(outFile == null)
00089 outFile = new EDLFile("");
00090 }
00091
00092 public void printUsageAndExit()
00093 {
00094 System.out.println("Usage: SortComposer [-options] features.feat \n\n" +
00095 " where options include:\n" +
00096 " -o output_file the file to write the output to (defaults to sorted.edl)\n" +
00097 " -g debug mode (prints out chunk features on each line of output file)\n" +
00098 " -r sort in reverse order");
00099 printCommandLineOptions('i');
00100 printCommandLineOptions('d');
00101 printCommandLineOptions('c');
00102 System.out.println();
00103 System.exit(0);
00104 }
00105
00109 public SortComposer(String[] args)
00110 {
00111 if(args.length == 0)
00112 printUsageAndExit();
00113
00114 Vector features = new Vector();
00115
00116
00117 String argString = "d:i:o:c:rg";
00118 featdim = parseFeatDim(args, argString);
00119 dist = parseChunkDist(args, argString, featdim);
00120 parseCommands(args, argString);
00121
00122 Getopt opt = new Getopt("SortComposer", args, argString);
00123 opt.setOpterr(false);
00124
00125 int c = -1;
00126 while ((c =opt.getopt()) != -1)
00127 {
00128 switch(c)
00129 {
00130 case 'o':
00131 outFileName = opt.getOptarg();
00132 break;
00133 case 'r':
00134 reverseSort = true;
00135 break;
00136 case 'g':
00137 debug = true;
00138 break;
00139 case 'd':
00140 break;
00141 case 'i':
00142 break;
00143 case 'c':
00144 break;
00145 case '?':
00146 printUsageAndExit();
00147 break;
00148 default:
00149 System.out.print("getopt() returned " + c + "\n");
00150 }
00151 }
00152
00153
00154 int ind = opt.getOptind();
00155 if(ind > args.length)
00156 printUsageAndExit();
00157
00158 featFile = new FeatFile(args[args.length-1]);
00159 outFile = new EDLFile(outFileName);
00160
00161 System.out.println("Composing " + outFileName +
00162 " from " + args[args.length-1] + ".");
00163 }
00164
00165 public void setReverseSort(boolean b)
00166 {
00167 reverseSort = b;
00168 }
00169
00170 public void setup() throws IOException, ParserException
00171 {
00172 super.setup();
00173
00174 if(!featFile.haveReadFile)
00175 featFile.readFile();
00176
00177 if(featFile.chunks.size() == 0)
00178 throw new ParserException(featFile.filename, "No chunks found");
00179 }
00180
00181 public EDLFile compose()
00182 {
00183
00184 int maxdim = 0;
00185 if(featdim != null)
00186 {
00187 for(int x = 0; x < featdim.length; x++)
00188 {
00189 if(featdim[x] > maxdim)
00190 maxdim = featdim[x];
00191 }
00192 }
00193 else
00194 maxdim = ((FeatChunk)featFile.chunks.get(0)).numFeatures();
00195
00196 FeatChunk targetChunk = new FeatChunk("",0,0);
00197 for(int i = 0; i <= maxdim; i++)
00198 targetChunk.addFeature(0);
00199
00200 dist.setTarget(targetChunk);
00201
00202
00203 Heap chunks = null;
00204 if(reverseSort)
00205 chunks = new MaxHeap(dist);
00206 else
00207 chunks = new MinHeap(dist);
00208 chunks.addAll(featFile.chunks);
00209
00210 NumberFormat fmt = NumberFormat.getInstance();
00211 fmt.setMaximumFractionDigits(3);
00212
00213 double currTime = 0;
00214 while(chunks.size() > 0)
00215 {
00216 FeatChunk match = (FeatChunk)chunks.remove(0);
00217
00218
00219 EDLChunk nc = new EDLChunk(match, currTime);
00220
00221 if(debug)
00222 {
00223 nc.comment = " # feats = ";
00224 double[] feat = match.getFeatures(featdim);
00225 for(int x = 0; x < feat.length-1; x++)
00226 nc.comment += fmt.format(feat[x]) + ", ";
00227 nc.comment += fmt.format(feat[feat.length-1]);
00228 }
00229
00230 outFile.chunks.add(nc);
00231
00232 currTime += match.length;
00233 }
00234
00235
00236 outFile.haveReadFile = true;
00237
00238 return outFile;
00239 }
00240
00241 public static void main(String[] args)
00242 {
00243 SortComposer m = new SortComposer(args);
00244 long startTime = System.currentTimeMillis();
00245 m.go();
00246 System.out.println("Done. Took " +
00247 ((System.currentTimeMillis() - startTime)/1000.0)
00248 + "s");
00249 System.exit(0);
00250 }
00251 }