Changeset 24398 for gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/util/AudioDBWrapper.java
- Timestamp:
- 2011-08-12T16:40:17+12:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/util/AudioDBWrapper.java
r24352 r24398 19 19 package org.greenstone.gsdl3.util; 20 20 21 import java.io. File;21 import java.io.*; 22 22 import java.util.Vector; 23 import java.util.Collections; 24 25 import org.apache.log4j.*; 23 26 24 27 /** java wrapper class for access to the audioDB executable … … 30 33 { 31 34 /** the query result, filled in by runQuery */ 32 protected Vector query_result_ = null;35 protected Vector query_result_; 33 36 34 37 protected int offset_ = 100; … … 40 43 protected int max_docs_; 41 44 45 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.util.AudioDBWrapper.class.getName ()); 46 42 47 public AudioDBWrapper() { 43 query_result_ = n ew Vector();48 query_result_ = null; 44 49 } 45 50 … … 70 75 // the following was in MG version, do we need this in audioDB version? 71 76 //public String getQueryParams() {} 77 78 79 protected boolean addQueryResult(boolean first_entry, String doc_id, 80 Vector<Double> rankVector, Vector<Integer> offsetVector) 81 { 82 83 if (first_entry) { 84 AudioDBDocInfo audioDB_doc_info = new AudioDBDocInfo(doc_id,rankVector,offsetVector); 85 query_result_.add(audioDB_doc_info); 86 first_entry = false; 87 } 88 else { 89 double rank = rankVector.get(0); 90 int offset = offsetVector.get(0); 91 AudioDBDocInfo audioDB_doc_info = new AudioDBDocInfo(doc_id,rank,offset); 92 93 query_result_.add(audioDB_doc_info); 94 } 95 96 return first_entry; 97 } 98 72 99 73 100 /** actually carry out the query. … … 79 106 * base_dir must end with a file separator (OS dependant) 80 107 */ 81 public void runQuery(String index_dir, String adb_file, String query_string) { 108 public void runQuery(String audioDB_index_dir, String adb_file, 109 String assoc_index_dir, String query_string) { 82 110 83 111 // combine index_dir with audiodb fileanem 84 112 85 String full_adb_filename = index_dir + File.separatorChar + adb_file; 113 String full_adb_filename = audioDB_index_dir + File.separatorChar + adb_file; 114 String full_chr12_filename = assoc_index_dir + File.separatorChar 115 + query_string + File.separatorChar + "doc.chr12"; 116 117 int num_matches_within_track = 6; 86 118 87 119 String cmd = "audioDB" 88 120 + " -d " + full_adb_filename 89 + " -Q sequence"121 + " -Q nsequence" 90 122 + " -p " + offset_ 91 + " -n 1"123 + " -n " + num_matches_within_track 92 124 + " -l " + length_ 93 + " -r " + "numMatchingDocs" 94 + " -f " + "track.chr12"; 95 125 + " -r " + max_docs_ 126 + " -f " + full_chr12_filename; 127 128 System.err.println("**** cmd = " + cmd); 129 130 Runtime runtime = Runtime.getRuntime(); 131 try { 132 Process audioDB_proc = runtime.exec(cmd); 133 InputStream ais = audioDB_proc.getInputStream(); 134 InputStreamReader aisr = new InputStreamReader(ais); 135 BufferedReader abr = new BufferedReader(aisr); 136 137 query_result_ = new Vector(); 138 139 boolean first_entry = true; 140 int line_count = 0; 141 142 String root_doc_id = null; 143 Vector<Double> rankVector = new Vector<Double>(); 144 Vector<Integer> offsetVector = new Vector<Integer>(); 145 146 // Example output 147 // D8 0.00105175 148 // 1.69786e-16 392 392 149 // 0.00113568 392 673 150 // 0.00127239 392 910 151 // 0.00139736 392 481 152 // 0.00145331 392 303 153 // D2 0.00429758 154 // 0.00403335 392 865 155 // 0.00411288 392 458 156 // 0.00442461 392 866 157 // 0.00444272 392 864 158 // 0.00447434 392 424 159 // ... 160 161 String line; 162 while ((line = abr.readLine()) != null) { 163 String[] tokens = line.split("\\s+"); 164 line_count++; 165 166 if (tokens.length==2) { 167 // processing a top-level doc line 168 169 if (line_count>1) { 170 // struck new top-level entry => store vector vals for previous block 171 first_entry = addQueryResult(first_entry,root_doc_id,rankVector,offsetVector); 172 // and now reset vectors to empty to be ready for next chain of values 173 rankVector = new Vector<Double>(); 174 offsetVector = new Vector<Integer>(); 175 } 176 177 root_doc_id = tokens[0]; 178 } 179 else { 180 // should be 3 items 181 double euclidean_dist = Double.parseDouble(tokens[0]); 182 int src_frame = Integer.parseInt(tokens[1]); 183 int target_frame = Integer.parseInt(tokens[2]); 184 185 // enforce 1.0 as upper limit due to rounding errors 186 // in audioDB distance calculations 187 double rank = Math.min(1.0 - euclidean_dist,1.0); 188 189 if ((line_count==2) && (src_frame==target_frame)) { 190 // Found match with self 191 continue; 192 } 193 194 rankVector.add(rank); 195 offsetVector.add(target_frame); 196 } 197 198 } 199 200 addQueryResult(first_entry,root_doc_id,rankVector,offsetVector); 201 202 abr.close(); 203 204 // sort query_result_ on 'rank' field 205 // note: compareTo() method impelemented to sort into descending order 206 207 Collections.sort(query_result_); 208 209 210 } 211 catch (Exception e) { 212 logger.error("Failed to execute the following command: " + cmd); 213 e.printStackTrace(); 214 } 96 215 97 216 }
Note:
See TracChangeset
for help on using the changeset viewer.