Tagcloud changes
authorRaphael Velt <raph.velt@gmail.com>
Thu, 19 Jan 2012 13:52:58 +0100
changeset 483 e1350651c490
parent 480 67ee4a400ec4
child 484 e6ac91d5462b
Tagcloud changes
web/res/js-tweetcast/live-polemic.js
--- a/web/res/js-tweetcast/live-polemic.js	Thu Jan 19 12:29:05 2012 +0100
+++ b/web/res/js-tweetcast/live-polemic.js	Thu Jan 19 13:52:58 2012 +0100
@@ -41,13 +41,16 @@
     }    
 }
 
+if (typeof suggested_keywords == "undefined") {
+    suggested_keywords = [ ];
+}
+
 if (typeof max_pages == "undefined" || !max_pages) {
     max_pages = 5;
 }
 
-var socket,
-    tlPaper,
-    twCx = {
+var twCx = {
+        tlPaper : null,
         followLast : true,
         position : "0",
         date_levels : [
@@ -61,21 +64,34 @@
         tlWidth : 150,
         tlHeight : 480,
         globalWords : {},
+        suggestCount : suggested_keywords.map(function(_w) {
+            return {
+                "word" : _w,
+                "rgxp" : new RegExp(_w.replace(/(\W)/g, '\\$1'), "im"),
+                "freq" : 0,
+                "annotations" : {}
+            }
+        }),
         refMouse : { x : 0, y : 0},
         refPosTl : { x : 0, y : 0},
         tlMouseMoved : false,
         tlMouseClicked : false,
-        filtre : null
-        },
-    tlBuffer = '',
-    relHover = [],
-    wheelDelta = 0,
-    scrollEnabled = false,
-    scrollExtent = 8000 - 480,
-    lastScrollPos = 0,
-    rx_url = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
-    rx_word = /[^ \.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
-    stop_list = [ 'and', 'avec', 'aux', 'car', 'comme', 'dans', 'donc', 'des', 'elle', 'est', 'être', 'eux', 'ils', 'les', 'leur', 'leurs', 'mes', 'mon', 'tes', 'ton', 'notre', 'nos', 'nous', 'ont', 'par', 'pas', 'que', 'qui', 'sont', 'sur', 'the', 'une', 'votre', 'vos', 'vous' ];
+        filtre : null,
+        tlBuffer : '',
+        relHover : [],
+        wheelDelta : 0,
+        scrollEnabled : false,
+        scrollExtent : 8000 - 480,
+        lastScrollPos : 0,
+        urlRegExp : /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
+        wordRegExp : /[^ \.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
+        stopWords : [
+            'aussi', 'and', 'avec', 'aux', 'car', 'cette', 'comme', 'dans', 'donc', 'des', 'elle', 'est',
+            'être', 'eux', 'fait', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre',
+            'non', 'nos', 'nous', 'ont', 'par', 'pas', 'peu', 'pour', 'que', 'qui', 'ses' ,'son', 'sont', 'sur',
+            'tes', 'très', 'the', 'ton', 'tous', 'tout', 'une', 'votre', 'vos', 'vous'
+            ],
+    }
 
 function getTweets(options) {
     
@@ -252,23 +268,40 @@
             }
         }
     }
-     
-    var localWords = []
+    
     
-    var tab = tweet.text.replace(rx_url,'').match(rx_word);
+    var tab = tweet.text.replace(twCx.urlRegExp,'').match(twCx.wordRegExp);
     for (var i in tab) {
         var word = tab[i].toLowerCase();
-        if (stop_list.indexOf(word) == -1 && tracking_keywords.indexOf(word) == -1) {
+        if (twCx.stopWords.indexOf(word) == -1 && tracking_keywords.indexOf(word) == -1) {
             if (twCx.globalWords[word]) {
-                twCx.globalWords[word]++;
+                twCx.globalWords[word].freq++;
             } else {
-                twCx.globalWords[word] = 1;
+                twCx.globalWords[word] = {
+                    "freq" : 1,
+                    "annotations" : {}
+                }
+                for (var j in annotations) {
+                    if (j != 'default') {
+                        twCx.globalWords[word].annotations[j] = 0;
+                    }
+                }
             }
-            localWords.push(word);
+            for (var j in ann) {
+                twCx.globalWords[word].annotations[ann[j]]++;
+            }
         }
     }
     
-    tweet.words = localWords;
+    _(twCx.suggestCount).each(function(_k) {
+        if (tweet.text.search(_k.rgxp) != -1) {
+            _k.freq++;
+            _(ann).each(function(_a) {
+                _k.annotations[_a] = 1 + ( _k.annotations[_a] || 0 )
+            })
+        }
+    });
+    
     
     var p = twCx.idIndex.length;
     while (p && tweet.id < twCx.idIndex[p-1]) {
@@ -529,15 +562,15 @@
     } else {
         $("#hovertweet").hide();
     }
-    for (var i in relHover) {
-        relHover[i].remove();
+    for (var i in twCx.relHover) {
+        twCx.relHover[i].remove();
     }
-    relHover = drawTweetArcs(t, p, '#303030');
-    relHover.push(drawTweetPos(p, '#ffffff'));
+    twCx.relHover = drawTweetArcs(t, p, '#303030');
+    twCx.relHover.push(drawTweetPos(p, '#ffffff'));
 }
 
 function drawTweetPos(pos, color) {
-    var rel = tlPaper.rect(pos.x - .5 * twCx.scaleX, pos.y - .5 * twCx.scaleY, twCx.scaleX, twCx.scaleY);
+    var rel = twCx.tlPaper.rect(pos.x - .5 * twCx.scaleX, pos.y - .5 * twCx.scaleY, twCx.scaleX, twCx.scaleY);
     rel.attr({ "stroke" : color, "fill" : color, "fill-opacity" : .25 });
     return rel;
 }
@@ -549,7 +582,7 @@
     function tweetAndArc(a, b, aorb) {
         if (a && b) {
             res.push(drawTweetPos(aorb ? a : b, color));
-            var aa = tlPaper.path(arc(a,b))
+            var aa = twCx.tlPaper.path(arc(a,b))
                 .attr({ "stroke" : color, "stroke-width" : 1.5, "stroke-opacity" : .8 });
             res.push(aa);
         }
@@ -671,62 +704,20 @@
     }
     
     
-    if (suggested_keywords && suggested_keywords.length) {
-        var _skw = suggested_keywords.map(function(_e) {
-            return {
-                "word" : _e,
-                "rgxp" : new RegExp(_e, "im"),
-                "freq" : 0,
-                "annotations" : {}
-            }
-        })
-    }
-    
     var l = tweets.length,
         lines = 0,
         ppy = 0,
         html = '',
-        tweetsOnDisplay = [],
-        localWords = {};
+        tweetsOnDisplay = [];
     
     function pushTweet(tp, className) {
         
         if (tp < l && tp >= 0) {
             
-            if (_skw) {
-                _(_skw).each(function(_k) {
-                    if (tweets[tp].text.search(_k.rgxp) != -1) {
-                        _k.freq++;
-                        _(tweets[tp].annotations).each(function(_a) {
-                            _k.annotations[_a] = 1 + ( _k.annotations[_a] || 0 )
-                        })
-                    }
-                });
-            }
-            
             html += tweetToHtml(tweets[tp], className);
             
             tweetsOnDisplay.push(tp);
             
-            for (var i in tweets[tp].words) {
-                var w = tweets[tp].words[i];
-                if (localWords[w]) {
-                    localWords[w].freq++
-                } else {
-                    localWords[w] = {
-                        "freq" : 1,
-                        "annotations" : {}
-                    }
-                    for (var j in annotations) {
-                        if (j != 'default') {
-                            localWords[w].annotations[j] = 0;
-                        }
-                    }
-                }
-                for (var j in tweets[tp].annotations) {
-                    localWords[w].annotations[tweets[tp].annotations[j]]++;
-                }
-            }
         } else {
             html += placeHolder(className);
         }
@@ -734,8 +725,8 @@
     
     if (l) {
     
-        lastScrollPos = Math.floor( scrollExtent * ( 1 - ( p / l ) ) );
-        $("#scrollcont").scrollTop(lastScrollPos);
+        twCx.lastScrollPos = Math.floor( twCx.scrollExtent * ( 1 - ( p / l ) ) );
+        $("#scrollcont").scrollTop(twCx.lastScrollPos);
         
         if (l > p + 18) {
             lines++;
@@ -774,23 +765,21 @@
             pushTweet(n, 'icons');
             n--;
         }
-        if (html != tlBuffer) {
+        if (html != twCx.tlBuffer) {
             $("#tweetlist").html(html);
             $(".tweet.full").fadeIn();
-            tlBuffer = html;
+            twCx.tlBuffer = html;
         }
         
-        if (_skw) {
-            makeTagCloud(_skw, "#suggkw");
+        if (twCx.suggestCount.length) {
+            makeTagCloud(twCx.suggestCount, "#suggkw");
         }
-        /* Recherche des mots pertinents correspondant à la sélection */
         
-        var tab = _(localWords).map(function(v, k) {
+        var tab = _(twCx.globalWords).map(function(v, k) {
             return {
                 "word": k,
                 "freq" : v.freq,
                 "annotations" : v.annotations,
-                "score" : v.freq / Math.log( 2 + twCx.globalWords[j] )
             };
         }).filter(function(v) {
             return v.freq > 1;
@@ -798,7 +787,7 @@
         
         if (tab.length) {
             
-            tab = _(tab).sortBy( function(a) { return ( - a.score ) }).slice(0,20);
+            tab = _(tab).sortBy( function(a) { return ( - a.freq ) }).slice(0,20);
             makeTagCloud(tab,"#motscles");
         } else {
             $("#motscles").html('');
@@ -806,7 +795,7 @@
         twCx.centralTweet = tweets[p];
     } else {
         $("#tweetlist").html('');
-        tlBuffer = '';
+        twCx.tlBuffer = '';
         $("#motscles").html('');
     }
     
@@ -854,24 +843,24 @@
         twCx.tlOnDisplay[i].totalTweets = nbT;
     }
     twCx.scaleX = ( twCx.tlWidth - twCx.deltaX ) / maxTweets;
-    tlPaper.clear();
-    relHover = null;
+    twCx.tlPaper.clear();
+    twCx.relHover = null;
     
     // Dessin de la correspondance liste-timeline
     if (l) {
         var startY = twCx.tlHeight - startTl * twCx.scaleY,
             endY = twCx.tlHeight - ( endTl + 1 ) * twCx.scaleY,
             path = "M0 " + twCx.tlHeight + "C" + .7*twCx.deltaX + " " + twCx.tlHeight + " " + .3*twCx.deltaX + " " + startY + " " + twCx.deltaX + " " + startY + "L" + twCx.tlWidth + " " + startY + "L" + twCx.tlWidth + " " + endY + "L" + twCx.deltaX + " " + endY + "C" + .3*twCx.deltaX + " " + endY + " " + .7*twCx.deltaX + " 0 0 0";
-        tlPaper.path( path ).attr({ "stroke" : "none", "fill" : "#000080", "opacity" : .2 });
+        twCx.tlPaper.path( path ).attr({ "stroke" : "none", "fill" : "#000080", "opacity" : .2 });
     }   
     // dessin de la date de début
     
-    tlPaper.text(twCx.deltaX / 2, twCx.tlHeight - 7, new Date(twCx.tlOnDisplay[0].start).toTimeString().substr(0,5))
+    twCx.tlPaper.text(twCx.deltaX / 2, twCx.tlHeight - 7, new Date(twCx.tlOnDisplay[0].start).toTimeString().substr(0,5))
         .attr({ "text-anchor" : "middle", "font-size": "9px" });
     
     // dessin de la date de fin
     
-    tlPaper.text(twCx.deltaX / 2, 7, new Date(twCx.tlOnDisplay[twCx.tlOnDisplay.length - 1].end).toTimeString().substr(0,5))
+    twCx.tlPaper.text(twCx.deltaX / 2, 7, new Date(twCx.tlOnDisplay[twCx.tlOnDisplay.length - 1].end).toTimeString().substr(0,5))
         .attr({ "text-anchor" : "middle", "font-size": "9px" });
     
     for (var i = 0; i < twCx.tlOnDisplay.length; i++) {
@@ -880,7 +869,7 @@
         for (var j in twCx.tlOnDisplay[i].displayData) {
             var ll = twCx.tlOnDisplay[i].displayData[j].length;
             if (ll > 0) {
-                tlPaper.rect( twCx.deltaX + n * twCx.scaleX, posY, ll * twCx.scaleX, twCx.scaleY )
+                twCx.tlPaper.rect( twCx.deltaX + n * twCx.scaleX, posY, ll * twCx.scaleX, twCx.scaleY )
                     .attr({"stroke" : "none", "fill" : getColor(j, .4).hex });
                 n += ll;
             }
@@ -889,8 +878,8 @@
         // Si on est à une demi-heure, on trace un axe secondaire + heure
         
         if (i < twCx.tlOnDisplay.length - 1 && !(twCx.tlOnDisplay[i].end % 1800000)) {
-            tlPaper.path("M0 "+posY+"L" + twCx.tlWidth +" "+posY).attr({"stroke":"#ccc"});
-            tlPaper.text(twCx.deltaX / 2, posY, new Date(twCx.tlOnDisplay[i].end).toTimeString().substr(0,5)).attr({ "text-anchor" : "middle", "font-size": "9px" });
+            twCx.tlPaper.path("M0 "+posY+"L" + twCx.tlWidth +" "+posY).attr({"stroke":"#ccc"});
+            twCx.tlPaper.text(twCx.deltaX / 2, posY, new Date(twCx.tlOnDisplay[i].end).toTimeString().substr(0,5)).attr({ "text-anchor" : "middle", "font-size": "9px" });
         }
     }
     
@@ -919,7 +908,7 @@
             yy = posp.y + .5 * twCx.scaleY;
             ppy += 117;
             path += "L" + ( posp.x - .5 * twCx.scaleX ) + " " + yy + "L" + twCx.deltaX + " " + yy + "C"  + ( .2 * twCx.deltaX ) + " " + yy + " " + ( .7 * twCx.deltaX ) + " " + ppy + " 0 " + ppy;
-            tlPaper.path( path ).attr({"stroke":"#ffff00", "fill" : "#ffff00", "fill-opacity" : .15});
+            twCx.tlPaper.path( path ).attr({"stroke":"#ffff00", "fill" : "#ffff00", "fill-opacity" : .15});
             
             drawTweetArcs(tweets[p], posp, '#800080');
         }
@@ -1052,7 +1041,7 @@
 }
 
 $(document).ready(function() {
-    tlPaper = Raphael("timeline", twCx.tlWidth, twCx.tlHeight);
+    twCx.tlPaper = Raphael("timeline", twCx.tlWidth, twCx.tlHeight);
     
     connectTweets();
     
@@ -1067,17 +1056,17 @@
     chaineTimeZoom();
     
     $("#tweetlist").mousewheel(function(e, d) {
-        wheelDelta += d;
-        if (Math.abs(wheelDelta) >= 1) {
-            movePos( parseInt(wheelDelta) );
-            wheelDelta = 0;
+        twCx.wheelDelta += d;
+        if (Math.abs(twCx.wheelDelta) >= 1) {
+            movePos( parseInt(twCx.wheelDelta) );
+            twCx.wheelDelta = 0;
         }
         return false;
     });
     $("#timeline").mousewheel(function(e, d) {
-        wheelDelta += d;
-        if (Math.abs(wheelDelta) >= 1) {
-            if (wheelDelta > 0) {
+        twCx.wheelDelta += d;
+        if (Math.abs(twCx.wheelDelta) >= 1) {
+            if (twCx.wheelDelta > 0) {
                 tl = Math.min(twCx.date_levels.length - 1, twCx.timeLevel + 1);
             } else {
                 tl = Math.max(0, twCx.timeLevel - 1);
@@ -1087,7 +1076,7 @@
                 chaineTimeZoom();
                 updateDisplay();
             }
-            wheelDelta = 0;
+            twCx.wheelDelta = 0;
         }
         return false;
     });
@@ -1173,21 +1162,21 @@
     
     setInterval(function() {
         var sc = $("#scrollcont");
-        if (sc.scrollTop() != lastScrollPos && twCx.tweets && twCx.currentIdIndex) {
-            var p = Math.floor( twCx.currentIdIndex.length * ( 1 - sc.scrollTop() / scrollExtent ) );
+        if (sc.scrollTop() != twCx.lastScrollPos && twCx.tweets && twCx.currentIdIndex) {
+            var p = Math.floor( twCx.currentIdIndex.length * ( 1 - sc.scrollTop() / twCx.scrollExtent ) );
             goToPos(p);
         }
     }, 100)
 });
 
 function connectTweets() {
-    tlPaper.clear();
-    var _sq = tlPaper.rect(0, twCx.tlHeight, twCx.tlWidth, 0)
+    twCx.tlPaper.clear();
+    var _sq = twCx.tlPaper.rect(0, twCx.tlHeight, twCx.tlWidth, 0)
         .attr({
             "stroke" : "none",
             "fill" : "#8080cc"
         });
-    var _lb = tlPaper.text(twCx.tlWidth / 2, twCx.tlHeight / 2, "0 tweet")
+    var _lb = twCx.tlPaper.text(twCx.tlWidth / 2, twCx.tlHeight / 2, "0 tweet")
         .attr({
             "font-size" : "20px",
             "text-anchor" : "middle"