sort by weight for mashupbytag
authorcavaliet
Wed, 12 Dec 2012 17:49:57 +0100
changeset 1025 fc3d5e88cd45
parent 1024 f1d6e7acbd52
child 1026 f60f05ce77fe
sort by weight for mashupbytag
src/ldt/ldt/ldt_utils/views/json.py
--- a/src/ldt/ldt/ldt_utils/views/json.py	Wed Dec 12 13:12:01 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/json.py	Wed Dec 12 17:49:57 2012 +0100
@@ -8,9 +8,11 @@
 from ldt.ldt_utils.models import Project
 from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
 from ldt.ldt_utils.searchutils import search_generate_ldt
+from operator import itemgetter
 from datetime import datetime
 import ldt.auth as ldt_auth
 import lxml.etree
+import logging
 
 
 def project_json_id(request, id): 
@@ -94,6 +96,7 @@
     
     # We search
     s = request.REQUEST.get("tag")
+    sort_type = request.REQUEST.get("sort", "")
     if s:
         # We get the projects with all the segments
         project_xml, results = search_generate_ldt(request, "tags", s, False)
@@ -127,18 +130,48 @@
                 },
                 "id": "generated_mashup_list"
             }
-            #filtered_results = []
-            for res in results:
-                cur_in = float(res["start_ts"])
-                cur_out = cur_in + float(res["duration"])
-                if tc_in<=cur_in and cur_out<=tc_out:
-                    #filtered_results.append(res)
-                    mashup_list["items"].append(res["element_id"])
+            # If sort_type = weight, we sort the result by the tag's weight
+            if sort_type.lower() == "weight":
+                # First we turn each string timecode to a float
+                for res in results:
+                    res["start_ts"] = float(res["start_ts"])
+                    res["duration"] = float(res["duration"])
+                # We sort to group by start_ts for each media/iri_id
+                sorted_results = sorted(results, key=itemgetter("iri_id", "start_ts", "duration"))
+                highest_weighted = []
+                current_weight = 1
+                nb_res = len(sorted_results)
+                for i, res in enumerate(sorted_results):
+                    # Explanation : we calculate the weight, which is the number of segments 
+                    # tagged with the searched tag for the same iri_id at the same start_ts.
+                    # Thanks to the previous sort, the last segment is the one with the longest duration and the one we finally keep
+                    next_res = None
+                    if i<(nb_res-1):
+                        next_res = sorted_results[i+1]
+                    if next_res and next_res["iri_id"]==res["iri_id"] and next_res["start_ts"]==res["start_ts"]:
+                        current_weight += 1
+                        continue
+                    res["weight"] = current_weight
+                    highest_weighted.append(res)
+                    current_weight = 1
+                
+                # Now that we have the weight for all temporal segments, we just have to sort the array.
+                highest_weighted = sorted(highest_weighted, key=itemgetter("weight"), reverse=True)
+                for res in highest_weighted:
+                    cur_in = res["start_ts"]
+                    cur_out = cur_in + res["duration"]
+                    if tc_in<=cur_in and cur_out<=tc_out:
+                        #mashup_list["items"].append(res["iri_id"] + ", " + res["element_id"] + ", " + str(res["start_ts"]) + ", " + str(res["duration"]) + ", " + str(res["weight"]))
+                        mashup_list["items"].append(res["element_id"])
+            else:
+                # no particular sorting
+                for res in results:
+                    cur_in = float(res["start_ts"])
+                    cur_out = cur_in + float(res["duration"])
+                    if tc_in<=cur_in and cur_out<=tc_out:
+                        mashup_list["items"].append(res["element_id"])
             mashup_dict["lists"].append(mashup_list)
     
-    #mashup_dict["escape_bool"] = escape_bool
-    #mashup_dict["indent"] = indent
-    
     json_str = simplejson.dumps(mashup_dict, ensure_ascii=False, indent=indent)
     if escape_bool:
         json_str = escape(json_str)