Changeset 24398


Ignore:
Timestamp:
08/12/11 16:40:17 (9 years ago)
Author:
davidb
Message:

Latest round of changes to code, in preparing for demo to Goldsmiths

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  
    3838import org.apache.log4j.*;
    3939
    40 public class GS2AudioDBSearch extends AbstractGS2Search {
     40public class GS2AudioDBSearch extends AbstractGS2AudioSearch {
    4141
    4242    static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2AudioDBSearch.class.getName ());
     
    4848
    4949    protected static final String AUDIODB_DEFAULT_DIRECTORY = "audioDB";
    50     protected static final String ADB_FILENAME = "lhs-features.adb";
     50    protected static final String ADB_FILENAME = "lsh-features.adb";
    5151   
    5252    protected AudioDBWrapper audiodb_src = null;
     
    6060   
    6161     /** do the actual query */
    62     protected Element processTextQuery (Element request) {
     62    protected Element processAudioQuery (Element request) {
    6363    // MG needs to be synchronized (this inspiration for this class)
    6464    // Since it is not known how concurrent audioDB can be, play it safe for
     
    6868        // Create a new (empty) result message ('doc' is in ServiceRack.java)
    6969        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");
    7174        result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    7275       
     
    7477        Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    7578        if (param_list == null) {
    76         logger.error ("AudioDB TextQuery request had no paramList.");
     79        logger.error ("AudioDB AudioQuery request had no paramList.");
    7780        return result;  // Return the empty result
    7881        }
     
    8689        return result;  // Return the empty result
    8790        }
    88        
     91
    8992        // If an index hasn't been specified, use the default
    9093        String index = (String) params.get (INDEX_PARAM);
     
    9497       
    9598        // 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
    98103        // set the audioDB query parameters to the values the user has specified
    99104        setStandardQueryParams (params);
    100105       
    101         this.audiodb_src.runQuery (index_dir, ADB_FILENAME, query);
     106        this.audiodb_src.runQuery (audioDB_index_dir, ADB_FILENAME, assoc_index_dir, query);
    102107        Vector docs = this.audiodb_src.getQueryResult ();
    103108
    104109        if (docs.isEmpty()) {
    105110        // something has gone wrong
    106         GSXML.addError (this.doc, result, "Couldn't query the mg database", GSXML.ERROR_TYPE_SYSTEM);
     111        GSXML.addError (this.doc, result, "Couldn't query the audioDB database", GSXML.ERROR_TYPE_SYSTEM);
    107112        return result;
    108113        }
     
    129134        result.appendChild (document_list);
    130135        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
    134145            document_list.appendChild (doc_node);
    135146        }
  • gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/util/AudioDBDocInfo.java

    r24352 r24398  
    1919package org.greenstone.gsdl3.util;
    2020
     21import java.util.Vector;
     22import java.lang.Comparable;
    2123
    22 public class AudioDBDocInfo
     24public class AudioDBDocInfo implements Comparable<AudioDBDocInfo>
    2325{
    24     public String oid_ = null;
    25     public float rank_ = 0;
     26    public String oid_;
     27    public Vector<Double> rankVector_;
     28    public Vector<Integer> offsetVector_;
    2629
    27     public AudioDBDocInfo(String doc_oid, float rank)
     30    public AudioDBDocInfo(String doc_oid, double rank, int offset)
    2831    {
    2932    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);
    3139    }
    3240
    33     public String toString()
     41
     42    public AudioDBDocInfo(String doc_oid, Vector<Double> rankVector, Vector<Integer> offsetVector)
    3443    {
    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    }
    3699    }
    37100}
  • gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/util/AudioDBWrapper.java

    r24352 r24398  
    1919package org.greenstone.gsdl3.util;
    2020
    21 import java.io.File;
     21import java.io.*;
    2222import java.util.Vector;
     23import java.util.Collections;
     24
     25import org.apache.log4j.*;
    2326
    2427/** java wrapper class for access to the audioDB executable
     
    3033{
    3134    /** the query result, filled in by runQuery */
    32     protected Vector query_result_ = null;
     35    protected Vector query_result_;
    3336
    3437    protected int offset_ = 100;
     
    4043    protected int max_docs_;
    4144
     45    static Logger logger = Logger.getLogger (org.greenstone.gsdl3.util.AudioDBWrapper.class.getName ());
     46
    4247    public AudioDBWrapper() { 
    43     query_result_ = new Vector();
     48    query_result_ = null;
    4449    }
    4550
     
    7075    // the following was in MG version, do we need this in audioDB version?
    7176    //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
    7299
    73100    /** actually carry out the query.
     
    79106     * base_dir must end with a file separator (OS dependant)
    80107     */
    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) {
    82110
    83111    // combine index_dir with audiodb fileanem
    84112
    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;
    86118
    87119    String cmd = "audioDB"
    88120        + " -d " + full_adb_filename
    89         + " -Q sequence"
     121        + " -Q nsequence"
    90122        + " -p " + offset_
    91         + " -n 1"
     123        + " -n " + num_matches_within_track
    92124        + " -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    }
    96215
    97216    }
Note: See TracChangeset for help on using the changeset viewer.