Changeset 24398
- Timestamp:
- 2011-08-12T16:40:17+12:00 (12 years ago)
- Location:
- gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/service/GS2AudioDBSearch.java
r24352 r24398 38 38 import org.apache.log4j.*; 39 39 40 public class GS2AudioDBSearch extends AbstractGS2 Search {40 public class GS2AudioDBSearch extends AbstractGS2AudioSearch { 41 41 42 42 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2AudioDBSearch.class.getName ()); … … 48 48 49 49 protected static final String AUDIODB_DEFAULT_DIRECTORY = "audioDB"; 50 protected static final String ADB_FILENAME = "l hs-features.adb";50 protected static final String ADB_FILENAME = "lsh-features.adb"; 51 51 52 52 protected AudioDBWrapper audiodb_src = null; … … 60 60 61 61 /** do the actual query */ 62 protected Element process TextQuery (Element request) {62 protected Element processAudioQuery (Element request) { 63 63 // MG needs to be synchronized (this inspiration for this class) 64 64 // Since it is not known how concurrent audioDB can be, play it safe for … … 68 68 // Create a new (empty) result message ('doc' is in ServiceRack.java) 69 69 Element result = this.doc.createElement (GSXML.RESPONSE_ELEM); 70 result.setAttribute (GSXML.FROM_ATT, TEXT_QUERY_SERVICE); // Result looks the same as a text query 70 71 // Rather than QUERY_SERVICE use "TextQuery" 72 // => makes the result looks the same as a text query 73 result.setAttribute (GSXML.FROM_ATT, "TextQuery"); 71 74 result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 72 75 … … 74 77 Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 75 78 if (param_list == null) { 76 logger.error ("AudioDB TextQuery request had no paramList.");79 logger.error ("AudioDB AudioQuery request had no paramList."); 77 80 return result; // Return the empty result 78 81 } … … 86 89 return result; // Return the empty result 87 90 } 88 91 89 92 // If an index hasn't been specified, use the default 90 93 String index = (String) params.get (INDEX_PARAM); … … 94 97 95 98 // The location of the audioDB index 96 String index_dir = GSFile.collectionIndexDir (this.site_home, this.cluster_name) + File.separatorChar + index; 97 99 String toplevel_index_dir = GSFile.collectionIndexDir (this.site_home, this.cluster_name); 100 String audioDB_index_dir = toplevel_index_dir + File.separatorChar + index; 101 String assoc_index_dir = toplevel_index_dir + File.separatorChar + "assoc"; 102 98 103 // set the audioDB query parameters to the values the user has specified 99 104 setStandardQueryParams (params); 100 105 101 this.audiodb_src.runQuery ( index_dir, ADB_FILENAME, query);106 this.audiodb_src.runQuery (audioDB_index_dir, ADB_FILENAME, assoc_index_dir, query); 102 107 Vector docs = this.audiodb_src.getQueryResult (); 103 108 104 109 if (docs.isEmpty()) { 105 110 // something has gone wrong 106 GSXML.addError (this.doc, result, "Couldn't query the mgdatabase", GSXML.ERROR_TYPE_SYSTEM);111 GSXML.addError (this.doc, result, "Couldn't query the audioDB database", GSXML.ERROR_TYPE_SYSTEM); 107 112 return result; 108 113 } … … 129 134 result.appendChild (document_list); 130 135 for (int d = 0; d < docs.size (); d++) { 131 String doc_id = ((AudioDBDocInfo) docs.elementAt (d)).oid_; 132 float rank = ((AudioDBDocInfo) docs.elementAt (d)).rank_; 133 Element doc_node = createDocNode (doc_id, Float.toString (rank)); 136 AudioDBDocInfo adb_doc = (AudioDBDocInfo) docs.elementAt(d); 137 138 String doc_id = adb_doc.getDocID(); 139 double rank = adb_doc.getTopRank(); 140 String offsets = adb_doc.getOffsetList(); 141 142 Element doc_node = createDocNode (doc_id, Double.toString (rank)); 143 doc_node.setAttribute("frameOffset", offsets); 144 134 145 document_list.appendChild (doc_node); 135 146 } -
gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/util/AudioDBDocInfo.java
r24352 r24398 19 19 package org.greenstone.gsdl3.util; 20 20 21 import java.util.Vector; 22 import java.lang.Comparable; 21 23 22 public class AudioDBDocInfo 24 public class AudioDBDocInfo implements Comparable<AudioDBDocInfo> 23 25 { 24 public String oid_ = null; 25 public float rank_ = 0; 26 public String oid_; 27 public Vector<Double> rankVector_; 28 public Vector<Integer> offsetVector_; 26 29 27 public AudioDBDocInfo(String doc_oid, float rank)30 public AudioDBDocInfo(String doc_oid, double rank, int offset) 28 31 { 29 32 oid_ = doc_oid; 30 rank_ = rank; 33 34 rankVector_ = new Vector<Double>(); 35 rankVector_.add (rank); 36 37 offsetVector_ = new Vector<Integer>(); 38 offsetVector_.add(offset); 31 39 } 32 40 33 public String toString() 41 42 public AudioDBDocInfo(String doc_oid, Vector<Double> rankVector, Vector<Integer> offsetVector) 34 43 { 35 return "" + oid_ + " (" + rank_ + ")"; 44 oid_ = doc_oid; 45 rankVector_ = rankVector; 46 offsetVector_ = offsetVector; 47 } 48 49 public String getDocID() 50 { 51 return oid_; 52 } 53 54 public double getTopRank() 55 { 56 return rankVector_.get(0); 57 } 58 59 60 public String getOffsetList() 61 { 62 StringBuffer all_offsets = new StringBuffer(); 63 boolean first = true; 64 65 for (int i=0; i<offsetVector_.size(); i++) { 66 int offset = offsetVector_.get(i); 67 String offsetStr = Integer.toString(offset); 68 69 if (first) { 70 first = false; 71 } 72 else { 73 all_offsets.append(","); 74 } 75 76 all_offsets.append(offsetStr); 77 } 78 79 return all_offsets.toString(); 80 } 81 82 public int compareTo(AudioDBDocInfo di) 83 { 84 // based on first entry in rank Vector 85 86 // embodies a descending sort order 87 double lrank = rankVector_.get(0); 88 double rrank = di.rankVector_.get(0); 89 90 if (lrank<rrank) { 91 return 1; 92 } 93 else if (lrank>rrank) { 94 return -1; 95 } 96 else { 97 return 0; 98 } 36 99 } 37 100 } -
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.