src/js/serializers/JSONSerializer.js
branchpopcorn-port
changeset 395 5766e8238aaf
parent 352 cc2cdecc5ce5
child 513 4589b90fe1ad
equal deleted inserted replaced
386:c3a08790e8c8 395:5766e8238aaf
    37   return this._data.medias[0]; /* FIXME: don't hardcode it */
    37   return this._data.medias[0]; /* FIXME: don't hardcode it */
    38 };
    38 };
    39 
    39 
    40 /* this function searches for an annotation which matches title, description and keyword 
    40 /* this function searches for an annotation which matches title, description and keyword 
    41    "" matches any field. 
    41    "" matches any field. 
       
    42    Note: it ignores tweets.
    42 */    
    43 */    
    43 IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
    44 IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
    44  
       
    45     /* we can have many types of annotations. We want search to only look for regular segments */
    45     /* we can have many types of annotations. We want search to only look for regular segments */
    46     /* the next two lines are a bit verbose because for some test data, _serializer.data.view is either
    46     /* the next two lines are a bit verbose because for some test data, _serializer.data.view is either
    47        null or undefined.
    47        null or undefined.
    48     */
    48     */
    49     var view;
    49     var view;
    55 
    55 
    56     if(typeof(view) !== "undefined" && typeof(view.annotation_types) !== "undefined" && view.annotation_types.length > 1) {
    56     if(typeof(view) !== "undefined" && typeof(view.annotation_types) !== "undefined" && view.annotation_types.length > 1) {
    57             searchViewType = view.annotation_types[0];
    57             searchViewType = view.annotation_types[0];
    58     }
    58     }
    59 
    59 
       
    60     var filterfn = function(annotation) {
       
    61       if( searchViewType  != "" && 
       
    62           typeof(annotation.meta) !== "undefined" && 
       
    63           typeof(annotation.meta["id-ref"]) !== "undefined" &&
       
    64           annotation.meta["id-ref"] !== searchViewType) {
       
    65         return true; // don't pass
       
    66       } else {
       
    67           return false;
       
    68       }
       
    69     };
       
    70 
       
    71     return this.searchAnnotationsFilter(title, description, keyword, filterfn);
       
    72 
       
    73 };
       
    74 
       
    75 /* only look for tweets */
       
    76 IriSP.JSONSerializer.prototype.searchTweets = function(title, description, keyword) {
       
    77     /* we can have many types of annotations. We want search to only look for regular segments */
       
    78     /* the next two lines are a bit verbose because for some test data, _serializer.data.view is either
       
    79        null or undefined.
       
    80     */
       
    81     var view;
       
    82 
       
    83     if (typeof(this._data.views) !== "undefined" && this._data.views !== null)
       
    84        view = this._data.views[0];
       
    85 
       
    86     var searchViewType = "";
       
    87 
       
    88     if(typeof(view) !== "undefined" && typeof(view.annotation_types) !== "undefined" && view.annotation_types.length > 1) {
       
    89             searchViewType = view.annotation_types[0];
       
    90     }
       
    91 
       
    92     var filterfn = function(annotation) {
       
    93       if( searchViewType  != "" && 
       
    94           typeof(annotation.meta) !== "undefined" && 
       
    95           typeof(annotation.meta["id-ref"]) !== "undefined" &&
       
    96           annotation.meta["id-ref"] !== searchViewType) {
       
    97         return false; // pass
       
    98       } else {
       
    99           return true;
       
   100       }
       
   101     };
       
   102 
       
   103     return this.searchAnnotationsFilter(title, description, keyword, filterfn);
       
   104 
       
   105 };
       
   106 
       
   107 /*
       
   108   the previous function call this one, which is more general:
       
   109  */    
       
   110 IriSP.JSONSerializer.prototype.searchAnnotationsFilter = function(title, description, keyword, filter) {
       
   111 
    60     var rTitle;
   112     var rTitle;
    61     var rDescription;
   113     var rDescription;
    62     var rKeyword;
   114     var rKeyword;
    63     
       
    64     /* match anything if given the empty string */
   115     /* match anything if given the empty string */
    65     if (title == "")
   116     if (title == "")
    66       title = ".*";
   117       title = ".*";
    67     if (description == "")
   118     if (description == "")
    68       description = ".*";
   119       description = ".*";
    78     var i;
   129     var i;
    79     for (i in this._data.annotations) {
   130     for (i in this._data.annotations) {
    80       var annotation = this._data.annotations[i];
   131       var annotation = this._data.annotations[i];
    81       
   132       
    82       /* filter the annotations whose type is not the one we want */
   133       /* filter the annotations whose type is not the one we want */
    83       if (searchViewType  != "" && typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined"
   134       if (filter(annotation)) {
    84             && annotation.meta["id-ref"] !== searchViewType) {
       
    85           continue;
   135           continue;
    86       }
   136       }
    87     
   137       
    88       if (rTitle.test(annotation.content.title) && 
   138       if (rTitle.test(annotation.content.title) && 
    89           rDescription.test(annotation.content.description)) {
   139           rDescription.test(annotation.content.description)) {
    90           /* FIXME : implement keyword support */
   140           /* FIXME : implement keyword support */
    91           ret_array.push(annotation);
   141           ret_array.push(annotation);
    92       }
   142       }
   126   };
   176   };
   127   
   177   
   128   return ret;
   178   return ret;
   129 };
   179 };
   130 
   180 
       
   181 /* breaks a string in words and searches each of these words. Returns an array
       
   182    of objects with the id of the annotation and its number of occurences.
       
   183    
       
   184    FIXME: optimize ? seems to be n^2 in the worst case.
       
   185 */
       
   186 IriSP.JSONSerializer.prototype.searchTweetsOccurences = function(searchString) {
       
   187   var ret = { };
       
   188   var keywords = searchString.split(/\s+/);
       
   189   
       
   190   for (var i in keywords) {
       
   191     var keyword = keywords[i];
       
   192     
       
   193     // search this keyword in descriptions and title
       
   194     var found_annotations = []
       
   195     found_annotations = found_annotations.concat(this.searchTweets(keyword, "", ""));
       
   196     found_annotations = found_annotations.concat(this.searchTweets("", keyword, ""));
       
   197     
       
   198     for (var j in found_annotations) {
       
   199       var current_annotation = found_annotations[j];
       
   200       
       
   201       if (!ret.hasOwnProperty(current_annotation.id)) {
       
   202         ret[current_annotation.id] = 1;
       
   203       } else {
       
   204         ret[current_annotation.id] += 1;
       
   205       }
       
   206       
       
   207     }
       
   208 
       
   209   };
       
   210   
       
   211   return ret;
       
   212 };
       
   213 
   131 /* takes the currentTime and returns all the annotations that are displayable at the moment 
   214 /* takes the currentTime and returns all the annotations that are displayable at the moment 
   132    NB: only takes account the first type of annotations - ignores tweets 
   215    NB: only takes account the first type of annotations - ignores tweets 
   133    currentTime is in seconds.
   216    currentTime is in seconds.
   134  */
   217  */
   135 
   218