block by nitaku 005b68abc80f5f514230

OpeNER - Opinion targets (Amsterdam)

Full Screen

This word cloud shows opinion target words from reviews of accommodations in Amsterdam. Bigger words were identified more often by sentiment analyzers. Color represents the aggregated sentiment towards the target words (more green is more positive, more red is more negative and a yellow color is neutral). For example, both “hotel” and “room” have been mentioned a lot, but the overall sentiment towards “hotel” is more positive than the one towards “room”.

Use the mouse to pan and zoom.

Data is obtained from the Tour-Pedia APIs, that in turn get their input from the sentiment analysis pipeline of the OpeNER project.

The implementation makes use of d3.layout.cloud by Jason Davies.

index.js

// DATA
var words = {"dreamz":{"polarity":1,"count":1},"":{"polarity":48,"count":156},"staff":{"polarity":59,"count":65},"wifi":{"polarity":49,"count":81},"centre":{"polarity":3,"count":5},"wi":{"polarity":2,"count":2},"bed":{"polarity":34,"count":42},"bathroom":{"polarity":-1,"count":5},"restaurant":{"polarity":6,"count":8},"word":{"polarity":-2,"count":2},"cuisine":{"polarity":2,"count":2},"buffet":{"polarity":3,"count":3},"/":{"polarity":1,"count":5},"room":{"polarity":19,"count":121},"min.":{"polarity":-1,"count":1},"service":{"polarity":22,"count":36},"feeling":{"polarity":-1,"count":1},"value":{"polarity":12,"count":12},"internet":{"polarity":8,"count":16},"app":{"polarity":1,"count":3},"hostel":{"polarity":8,"count":14},"hotel":{"polarity":80,"count":114},"experience":{"polarity":0,"count":4},"manager":{"polarity":-1,"count":1},"place":{"polarity":47,"count":51},"horror":{"polarity":-1,"count":1},"roons":{"polarity":1,"count":1},"wi-fi":{"polarity":13,"count":17},"peppermint":{"polarity":-1,"count":1},"bus":{"polarity":2,"count":4},"architecture":{"polarity":1,"count":1},"ticket":{"polarity":1,"count":1},"walk":{"polarity":1,"count":1},"arhitecture":{"polarity":1,"count":1},"customer":{"polarity":1,"count":1},"downtown":{"polarity":-2,"count":2},"business":{"polarity":4,"count":4},"environment":{"polarity":1,"count":1},"breakfast":{"polarity":24,"count":40},"city":{"polarity":-2,"count":14},"art":{"polarity":4,"count":4},"expo":{"polarity":1,"count":1},"month":{"polarity":2,"count":2},"food":{"polarity":9,"count":13},"steak":{"polarity":1,"count":1},"cocktail":{"polarity":5,"count":5},"great":{"polarity":1,"count":5},"dutch":{"polarity":3,"count":3},"style":{"polarity":3,"count":3},"idea":{"polarity":1,"count":1},"decision":{"polarity":1,"count":1},"relation":{"polarity":1,"count":1},"departure":{"polarity":1,"count":1},"parking":{"polarity":3,"count":3},"ka":{"polarity":1,"count":1},"part":{"polarity":3,"count":3},"furniture":{"polarity":1,"count":1},"area":{"polarity":8,"count":8},"the":{"polarity":1,"count":1},"~":{"polarity":-1,"count":1},"modern":{"polarity":1,"count":1},"building":{"polarity":0,"count":2},"amsterdam":{"polarity":6,"count":8},"kitchen":{"polarity":0,"count":2},"guest":{"polarity":0,"count":2},"brand":{"polarity":1,"count":1},"dam":{"polarity":-1,"count":3},"access":{"polarity":4,"count":4},"hiex":{"polarity":1,"count":1},"neat":{"polarity":1,"count":1},"point":{"polarity":1,"count":1},"night":{"polarity":-2,"count":6},"stay":{"polarity":8,"count":8},"person":{"polarity":11,"count":13},"cooky":{"polarity":1,"count":1},"view":{"polarity":27,"count":29},"everything":{"polarity":6,"count":6},"solarium":{"polarity":1,"count":1},"hair":{"polarity":1,"count":1},"desk":{"polarity":-1,"count":1},"tram":{"polarity":4,"count":8},"railway":{"polarity":1,"count":1},"shuttle":{"polarity":1,"count":1},"floor":{"polarity":0,"count":2},"tv":{"polarity":1,"count":1},"hotell":{"polarity":1,"count":1},"timetable":{"polarity":1,"count":1},"nice":{"polarity":5,"count":5},"fast":{"polarity":1,"count":1},"dirty":{"polarity":-1,"count":1},"costumers":{"polarity":-1,"count":1},"ok":{"polarity":1,"count":1},"day":{"polarity":4,"count":6},"checkin":{"polarity":1,"count":1},"tuna":{"polarity":0,"count":2},"over":{"polarity":-1,"count":1},"shop":{"polarity":1,"count":1},"euro":{"polarity":2,"count":2},"price":{"polarity":17,"count":19},"connection":{"polarity":-1,"count":5},"way":{"polarity":3,"count":3},"wifis":{"polarity":-1,"count":1},"location":{"polarity":55,"count":55},"feed":{"polarity":1,"count":1},"bir":{"polarity":1,"count":1},"bagels":{"polarity":-1,"count":1},"mark":{"polarity":-1,"count":1},"middle":{"polarity":1,"count":1},"cooking":{"polarity":1,"count":1},"indulge":{"polarity":1,"count":1},"swimming-pool":{"polarity":1,"count":1},"beers":{"polarity":1,"count":1},"drink":{"polarity":3,"count":3},"deal":{"polarity":1,"count":3},"sleep":{"polarity":1,"count":1},"environmentalism":{"polarity":0,"count":2},"stuff":{"polarity":2,"count":2},"bar":{"polarity":3,"count":3},"coverage":{"polarity":1,"count":1},"coffee":{"polarity":6,"count":6},"b":{"polarity":1,"count":3},"street":{"polarity":3,"count":9},"cheesecake":{"polarity":1,"count":1},"accomodation":{"polarity":1,"count":1},"amsterdamse":{"polarity":1,"count":1},"club":{"polarity":0,"count":2},"window":{"polarity":2,"count":2},"impression":{"polarity":1,"count":1},"cost":{"polarity":-1,"count":1},"ac":{"polarity":-1,"count":1},"dining":{"polarity":1,"count":1},"terrace":{"polarity":3,"count":3},"song":{"polarity":1,"count":1},"position":{"polarity":3,"count":3},"canal":{"polarity":3,"count":3},"wouldn":{"polarity":1,"count":1},"bit":{"polarity":1,"count":3},"assortment":{"polarity":1,"count":1},"partner":{"polarity":-1,"count":1},"carpet":{"polarity":-1,"count":1},"museum":{"polarity":0,"count":2},"drawer":{"polarity":-1,"count":1},"neighbourhood":{"polarity":3,"count":3},"left":{"polarity":-1,"count":1},"takeaway":{"polarity":1,"count":1},"budget":{"polarity":5,"count":5},"music":{"polarity":2,"count":2},"family":{"polarity":1,"count":1},"very":{"polarity":3,"count":3},"iron":{"polarity":1,"count":1},"thing":{"polarity":2,"count":4},"garden":{"polarity":2,"count":2},"cash":{"polarity":-2,"count":2},"summer":{"polarity":1,"count":1},"fireplace":{"polarity":2,"count":2},"water":{"polarity":2,"count":2},"robbery":{"polarity":-1,"count":1},"blackberry":{"polarity":2,"count":2},"bif":{"polarity":2,"count":2},"hilton":{"polarity":1,"count":1},"sushi":{"polarity":2,"count":2},"spa":{"polarity":-1,"count":1},"mushroom":{"polarity":1,"count":1},"moment":{"polarity":1,"count":1},"usage":{"polarity":1,"count":1},"europe":{"polarity":-1,"count":1},"mood":{"polarity":1,"count":1},"mustiness":{"polarity":-1,"count":1},"comfort":{"polarity":-1,"count":3},"center":{"polarity":-2,"count":2},"😍":{"polarity":1,"count":1},"aspect":{"polarity":0,"count":2},"care":{"polarity":1,"count":1},"souvenir":{"polarity":1,"count":1},"oj":{"polarity":1,"count":1},"perk":{"polarity":1,"count":1},"home":{"polarity":2,"count":2},"imacs":{"polarity":2,"count":2},"designer":{"polarity":0,"count":2},"movie":{"polarity":2,"count":2},"leisure":{"polarity":1,"count":1},"madness":{"polarity":-1,"count":1},"ride":{"polarity":1,"count":1},"lot":{"polarity":3,"count":3},"bowl":{"polarity":1,"count":1},"atmosphere":{"polarity":5,"count":5},"wifino":{"polarity":1,"count":1},"decoration":{"polarity":4,"count":4},"setting":{"polarity":1,"count":1},"am":{"polarity":1,"count":1},"cool":{"polarity":1,"count":1},"meeting":{"polarity":0,"count":2},"luxury":{"polarity":3,"count":3},"merchandise":{"polarity":1,"count":1},"beer":{"polarity":2,"count":2},"remote":{"polarity":1,"count":1},"lavender":{"polarity":1,"count":1},"coctail":{"polarity":1,"count":1},"martini":{"polarity":1,"count":1},"lobby":{"polarity":3,"count":5},"lounge":{"polarity":2,"count":4},"video":{"polarity":1,"count":1},"trendy":{"polarity":1,"count":1},"hip":{"polarity":1,"count":1},"porn":{"polarity":1,"count":1},"system":{"polarity":1,"count":1},"shower":{"polarity":0,"count":4},"issue":{"polarity":-1,"count":1},"ambience":{"polarity":1,"count":1},"cup":{"polarity":2,"count":2},"spot":{"polarity":-3,"count":5},"rai":{"polarity":1,"count":1},"colour":{"polarity":1,"count":1},"key":{"polarity":1,"count":1},"rest":{"polarity":1,"count":1},"smiler":{"polarity":1,"count":1},"tapa":{"polarity":1,"count":1},"edition":{"polarity":-1,"count":1},"leidseplein":{"polarity":1,"count":1},"exec":{"polarity":1,"count":1},"rms":{"polarity":2,"count":2},"executive":{"polarity":1,"count":1},"meat":{"polarity":1,"count":1},"reception":{"polarity":0,"count":2},"<":{"polarity":-1,"count":1},"coco":{"polarity":1,"count":1},"fantastic":{"polarity":1,"count":1},"+":{"polarity":5,"count":5},"weather":{"polarity":0,"count":2},"anything":{"polarity":1,"count":1},"avotre":{"polarity":2,"count":2},"summertime":{"polarity":1,"count":1},"heart":{"polarity":1,"count":1},"café":{"polarity":1,"count":1},"wo":{"polarity":1,"count":1},"terras":{"polarity":1,"count":1},"hour":{"polarity":1,"count":1},"rotterdam":{"polarity":1,"count":1},"boarding":{"polarity":1,"count":1},"dormitory":{"polarity":-1,"count":1},"school":{"polarity":1,"count":1},"rachel":{"polarity":1,"count":1},"tour":{"polarity":1,"count":1},"eleanor":{"polarity":1,"count":1},"choice":{"polarity":8,"count":8},"radisson":{"polarity":1,"count":1},"comfy":{"polarity":-1,"count":1},"sticking":{"polarity":1,"count":1},"pasta":{"polarity":-1,"count":1},"attraction":{"polarity":2,"count":4},"girl":{"polarity":1,"count":1},"oyster":{"polarity":1,"count":1},"boutique":{"polarity":2,"count":2},"stedelijk":{"polarity":1,"count":1},"minibar":{"polarity":1,"count":1},"marcel":{"polarity":-1,"count":1},"morning":{"polarity":-1,"count":1},"here":{"polarity":1,"count":1},"check":{"polarity":1,"count":1},"quality":{"polarity":0,"count":2},"bike":{"polarity":1,"count":3},"museumplein":{"polarity":1,"count":1},"pillow":{"polarity":-2,"count":2},"loft":{"polarity":1,"count":1},"tho":{"polarity":1,"count":1},"public":{"polarity":1,"count":1},"stair":{"polarity":-1,"count":3},"bacon":{"polarity":1,"count":1},"feel":{"polarity":1,"count":1},"advance":{"polarity":1,"count":1},"o":{"polarity":-1,"count":1},"towel":{"polarity":-1,"count":1},"youth":{"polarity":1,"count":1},"toilet":{"polarity":-1,"count":1},"coffe":{"polarity":2,"count":2},"pay.":{"polarity":2,"count":2},"cafe":{"polarity":3,"count":3},"localization":{"polarity":2,"count":2},"too":{"polarity":1,"count":1},"space":{"polarity":-1,"count":1},"smalls":{"polarity":1,"count":1},"smoke":{"polarity":-1,"count":1},"visitor":{"polarity":2,"count":2},"coffeeshops":{"polarity":1,"count":1},"personel":{"polarity":1,"count":1},"♥":{"polarity":1,"count":1},"buzz":{"polarity":1,"count":1},"venue":{"polarity":3,"count":3},"avoid":{"polarity":1,"count":1},"stank":{"polarity":-1,"count":1},"marihuana":{"polarity":-1,"count":1},"side":{"polarity":1,"count":3},"crazy":{"polarity":1,"count":1},"nibble":{"polarity":1,"count":1},"visit":{"polarity":1,"count":1},"danny":{"polarity":1,"count":1},"information":{"polarity":1,"count":1},"ice":{"polarity":1,"count":1},"momo":{"polarity":1,"count":1},"don":{"polarity":-2,"count":2},"enough":{"polarity":1,"count":1},"balcony":{"polarity":1,"count":1},"machine":{"polarity":-1,"count":1},"new":{"polarity":3,"count":3},"tea":{"polarity":3,"count":3},"gerco":{"polarity":-1,"count":1},"investment":{"polarity":-1,"count":1},"receptionist":{"polarity":0,"count":2},"leidseplain":{"polarity":1,"count":1},"cleanliness":{"polarity":-1,"count":1},"accent":{"polarity":1,"count":1},"metro":{"polarity":1,"count":1},"computer":{"polarity":1,"count":1},"i":{"polarity":0,"count":2},"price-comfort-location":{"polarity":1,"count":1},"stop":{"polarity":-1,"count":1},"wi-fis":{"polarity":1,"count":1},"variety":{"polarity":-1,"count":1},"crowne":{"polarity":1,"count":1},"payment":{"polarity":-1,"count":1},"queue":{"polarity":-1,"count":1},"employee":{"polarity":-1,"count":1},"supermarket":{"polarity":2,"count":2},"level":{"polarity":0,"count":2},"toast":{"polarity":-1,"count":1},"host":{"polarity":2,"count":2},"cheese":{"polarity":0,"count":2},"entourage":{"polarity":1,"count":1},"minute":{"polarity":1,"count":1},"mojitos":{"polarity":1,"count":1},"order":{"polarity":2,"count":2},"rooms-":{"polarity":1,"count":1},"intimate":{"polarity":1,"count":1},"huis":{"polarity":1,"count":1},"pancake":{"polarity":-1,"count":1},"property":{"polarity":1,"count":1},"hint":{"polarity":1,"count":1},"stand":{"polarity":1,"count":1},"combination":{"polarity":1,"count":1},"today":{"polarity":-1,"count":1},"craft":{"polarity":1,"count":1},"hotel-":{"polarity":1,"count":1},"door":{"polarity":1,"count":1},"offer":{"polarity":1,"count":1},"cake":{"polarity":1,"count":1},"choc":{"polarity":1,"count":1},"chocolate":{"polarity":1,"count":1},"selection":{"polarity":1,"count":1},"check-in":{"polarity":1,"count":1},"surprise":{"polarity":-1,"count":1},"size":{"polarity":1,"count":1},"sunday":{"polarity":-1,"count":1},"option":{"polarity":1,"count":1},"history":{"polarity":1,"count":1},"station":{"polarity":3,"count":3},"signal":{"polarity":-1,"count":1},"ambiance":{"polarity":1,"count":1},"otel":{"polarity":1,"count":1},"starbucks":{"polarity":1,"count":1},"friend":{"polarity":-1,"count":1},"password":{"polarity":1,"count":1},"delinquent":{"polarity":1,"count":1},"slloooooooowwwwww":{"polarity":1,"count":1},"sound":{"polarity":-1,"count":1},"staying":{"polarity":-2,"count":2},"sort":{"polarity":2,"count":2},"basic":{"polarity":-1,"count":1},"anyone":{"polarity":1,"count":1},"district":{"polarity":1,"count":1},"college":{"polarity":1,"count":1},"neighbour":{"polarity":1,"count":1},"hotel@a":{"polarity":1,"count":1},"http://soundcloud.com/7lifelovemisery":{"polarity":1,"count":1},"end":{"polarity":-1,"count":1},"nh":{"polarity":-1,"count":1},"standard":{"polarity":1,"count":1},"mini":{"polarity":-1,"count":1},"average":{"polarity":0,"count":2},"wardrobe":{"polarity":1,"count":1},"plenty":{"polarity":1,"count":1},"juice":{"polarity":1,"count":1},"bartender":{"polarity":1,"count":1},"wise":{"polarity":1,"count":1},"hop":{"polarity":-1,"count":1},"coffee-tea":{"polarity":1,"count":1},"hospitality":{"polarity":1,"count":1},"orange":{"polarity":1,"count":1},"modest":{"polarity":1,"count":1},"xx":{"polarity":1,"count":1},"staf":{"polarity":-1,"count":1},"speed":{"polarity":-1,"count":1},"tramp":{"polarity":1,"count":1},"hash":{"polarity":0,"count":2},"amount":{"polarity":-1,"count":1},"booth":{"polarity":1,"count":1},"worker":{"polarity":1,"count":1},"marina":{"polarity":-1,"count":1},"vibes":{"polarity":1,"count":1},"treatment":{"polarity":1,"count":1},"concierge":{"polarity":2,"count":2},"hammering":{"polarity":-1,"count":1},"cannelloni":{"polarity":1,"count":1},"€":{"polarity":-2,"count":2},"renaissance":{"polarity":1,"count":1},"—":{"polarity":1,"count":1},"life":{"polarity":1,"count":1},"job":{"polarity":1,"count":1},"beeakfast":{"polarity":1,"count":1},"breast":{"polarity":1,"count":1},"salmon":{"polarity":2,"count":2},"soup":{"polarity":1,"count":1},"range":{"polarity":2,"count":2},"beef":{"polarity":-2,"count":2},"salad":{"polarity":-1,"count":1},"pea":{"polarity":1,"count":1},"year":{"polarity":1,"count":1},"soupe":{"polarity":1,"count":1},"scene":{"polarity":1,"count":1}};

Math.seedrandom('abcde');  // define a fixed random seed, to avoid to have a different layout on each page reload. change the string to randomize

var words_frequency = [];
Object.keys(words).forEach(function(word_lemma){
    o = words[word_lemma];
    o.lemma = word_lemma;
    words_frequency.push(o);
});

var max = d3.max(Object.keys(words), function(k){
    return words[k].count;
});
console.log(max);


var font_size = d3.scale.linear()
    .domain([1, max])
    .range([10,100]);

var color = d3.scale.linear()
    .domain([-max, 0, max])
    .range([d3.hcl(36, 65, 50), d3.hcl(95, 65, 80), d3.hcl(150, 65, 50)])
    .interpolate(d3.interpolateHcl);

/*var color = d3.scale.quantize()
      .domain([-max, max])
      .range([d3.hcl(36, 65, 50), d3.hcl(150, 65, 50)]);*/

words_frequency.sort(function(a,b){
    return b.count - a.count;
});


d3.layout.cloud().size([960, 500])
    .words(words_frequency)
    .rotate(function() { return ~~(Math.random() * 2) * 90; })
    .font("Impact")
    .spiral('rectangular')
    .text(function(d){ return d.lemma; })
    .fontSize(function(d){ return font_size(d.count); })
    .on("end", draw)
    .start();

function draw(words) {
    var svg = d3.select("body").append("svg")
        .attr("width", 960)
        .attr("height", 500);
    
    
    // append a group for zoomable content
    var zoom_group = svg.append('g');

    // define a zoom behavior
    var zoom = d3.behavior.zoom()
        .scaleExtent([1,4]) // min-max zoom
        .on('zoom', function() {
          // whenever the user zooms,
          // modify translation and scale of the zoom group accordingly
          zoom_group.attr('transform', 'translate('+zoom.translate()+')scale('+zoom.scale()+')');
        });

    // bind the zoom behavior to the main SVG
    svg.call(zoom);
    

    zoom_group.append("g")
        .attr("transform", "translate(480,250)")
        .selectAll("text")
        .data(words)
      .enter().append("text")
        .style("font-size", function(d){ return font_size(d.count) + "px"; })
        .style("font-family", "Impact")
        .style("fill", function(d) { return color(d.polarity); })
        .attr("text-anchor", "middle")
        .attr("transform", function(d) {
            var far = 1500*(Math.random() > 0.5 ? +1 : -1);
            if(d.rotate === 0)
                return "translate("+far+",0)rotate(" + d.rotate + ")";
            else
                return "translate(0,"+far+")rotate(" + d.rotate + ")";
        })
        .text(function(d) { return d.lemma; })
      .transition().duration(2000)
        .attr("transform", function(d) {
            return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
        });
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="OpeNER - Opinion targets (Amsterdam)" />
    <meta charset="utf-8">
    <title>OpeNER - Opinion targets (Amsterdam)</title>
    <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src='layout.cloud.js'></script>
    <script src="//d3js.org/queue.v1.min.js"></script>
    <script src="//davidbau.com/encode/seedrandom-min.js"></script>
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
    <script src='index.js'></script>
</body>
</html>

index.css

html, body {
    margin: 0;
    padding: 0;
    background: white;
}

text {
    pointer-events:none;
}

layout.cloud.js

// d3.layout.cloud
// Word cloud layout by Jason Davies, http://www.jasondavies.com/word-cloud/
// Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf
(function() {
  function cloud() {
    var size = [256, 256],
        text = cloudText,
        font = cloudFont,
        fontSize = cloudFontSize,
        fontStyle = cloudFontNormal,
        fontWeight = cloudFontNormal,
        rotate = cloudRotate,
        padding = cloudPadding,
        spiral = archimedeanSpiral,
        words = [],
        timeInterval = Infinity,
        event = d3.dispatch("word", "end"),
        timer = null,
        cloud = {};

    cloud.start = function() {
      var board = zeroArray((size[0] >> 5) * size[1]),
          bounds = null,
          n = words.length,
          i = -1,
          tags = [],
          data = words.map(function(d, i) {
            d.text = text.call(this, d, i);
            d.font = font.call(this, d, i);
            d.style = fontStyle.call(this, d, i);
            d.weight = fontWeight.call(this, d, i);
            d.rotate = rotate.call(this, d, i);
            d.size = ~~fontSize.call(this, d, i);
            d.padding = padding.call(this, d, i);
            return d;
          }).sort(function(a, b) { return b.size - a.size; });

      if (timer) clearInterval(timer);
      timer = setInterval(step, 0);
      step();

      return cloud;

      function step() {
        var start = +new Date,
            d;
        while (+new Date - start < timeInterval && ++i < n && timer) {
          d = data[i];
          d.x = (size[0] * (Math.random() + .5)) >> 1;
          d.y = (size[1] * (Math.random() + .5)) >> 1;
          cloudSprite(d, data, i);
          if (d.hasText && place(board, d, bounds)) {
            tags.push(d);
            event.word(d);
            if (bounds) cloudBounds(bounds, d);
            else bounds = [{x: d.x + d.x0, y: d.y + d.y0}, {x: d.x + d.x1, y: d.y + d.y1}];
            // Temporary hack
            d.x -= size[0] >> 1;
            d.y -= size[1] >> 1;
          }
        }
        if (i >= n) {
          cloud.stop();
          event.end(tags, bounds);
        }
      }
    }

    cloud.stop = function() {
      if (timer) {
        clearInterval(timer);
        timer = null;
      }
      return cloud;
    };

    cloud.timeInterval = function(x) {
      if (!arguments.length) return timeInterval;
      timeInterval = x == null ? Infinity : x;
      return cloud;
    };

    function place(board, tag, bounds) {
      var perimeter = [{x: 0, y: 0}, {x: size[0], y: size[1]}],
          startX = tag.x,
          startY = tag.y,
          maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
          s = spiral(size),
          dt = Math.random() < .5 ? 1 : -1,
          t = -dt,
          dxdy,
          dx,
          dy;

      while (dxdy = s(t += dt)) {
        dx = ~~dxdy[0];
        dy = ~~dxdy[1];

        if (Math.min(dx, dy) > maxDelta) break;

        tag.x = startX + dx;
        tag.y = startY + dy;

        if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
            tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
        // TODO only check for collisions within current bounds.
        if (!bounds || !cloudCollide(tag, board, size[0])) {
          if (!bounds || collideRects(tag, bounds)) {
            var sprite = tag.sprite,
                w = tag.width >> 5,
                sw = size[0] >> 5,
                lx = tag.x - (w << 4),
                sx = lx & 0x7f,
                msx = 32 - sx,
                h = tag.y1 - tag.y0,
                x = (tag.y + tag.y0) * sw + (lx >> 5),
                last;
            for (var j = 0; j < h; j++) {
              last = 0;
              for (var i = 0; i <= w; i++) {
                board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
              }
              x += sw;
            }
            delete tag.sprite;
            return true;
          }
        }
      }
      return false;
    }

    cloud.words = function(x) {
      if (!arguments.length) return words;
      words = x;
      return cloud;
    };

    cloud.size = function(x) {
      if (!arguments.length) return size;
      size = [+x[0], +x[1]];
      return cloud;
    };

    cloud.font = function(x) {
      if (!arguments.length) return font;
      font = d3.functor(x);
      return cloud;
    };

    cloud.fontStyle = function(x) {
      if (!arguments.length) return fontStyle;
      fontStyle = d3.functor(x);
      return cloud;
    };

    cloud.fontWeight = function(x) {
      if (!arguments.length) return fontWeight;
      fontWeight = d3.functor(x);
      return cloud;
    };

    cloud.rotate = function(x) {
      if (!arguments.length) return rotate;
      rotate = d3.functor(x);
      return cloud;
    };

    cloud.text = function(x) {
      if (!arguments.length) return text;
      text = d3.functor(x);
      return cloud;
    };

    cloud.spiral = function(x) {
      if (!arguments.length) return spiral;
      spiral = spirals[x + ""] || x;
      return cloud;
    };

    cloud.fontSize = function(x) {
      if (!arguments.length) return fontSize;
      fontSize = d3.functor(x);
      return cloud;
    };

    cloud.padding = function(x) {
      if (!arguments.length) return padding;
      padding = d3.functor(x);
      return cloud;
    };

    return d3.rebind(cloud, event, "on");
  }

  function cloudText(d) {
    return d.text;
  }

  function cloudFont() {
    return "serif";
  }

  function cloudFontNormal() {
    return "normal";
  }

  function cloudFontSize(d) {
    return Math.sqrt(d.value);
  }

  function cloudRotate() {
    return (~~(Math.random() * 6) - 3) * 30;
  }

  function cloudPadding() {
    return 1;
  }

  // Fetches a monochrome sprite bitmap for the specified text.
  // Load in batches for speed.
  function cloudSprite(d, data, di) {
    if (d.sprite) return;
    c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
    var x = 0,
        y = 0,
        maxh = 0,
        n = data.length;
    --di;
    while (++di < n) {
      d = data[di];
      c.save();
      c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font;
      var w = c.measureText(d.text + "m").width * ratio,
          h = d.size << 1;
      if (d.rotate) {
        var sr = Math.sin(d.rotate * cloudRadians),
            cr = Math.cos(d.rotate * cloudRadians),
            wcr = w * cr,
            wsr = w * sr,
            hcr = h * cr,
            hsr = h * sr;
        w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5;
        h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
      } else {
        w = (w + 0x1f) >> 5 << 5;
      }
      if (h > maxh) maxh = h;
      if (x + w >= (cw << 5)) {
        x = 0;
        y += maxh;
        maxh = 0;
      }
      if (y + h >= ch) break;
      c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
      if (d.rotate) c.rotate(d.rotate * cloudRadians);
      c.fillText(d.text, 0, 0);
      if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, 0, 0);
      c.restore();
      d.width = w;
      d.height = h;
      d.xoff = x;
      d.yoff = y;
      d.x1 = w >> 1;
      d.y1 = h >> 1;
      d.x0 = -d.x1;
      d.y0 = -d.y1;
      d.hasText = true;
      x += w;
    }
    var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data,
        sprite = [];
    while (--di >= 0) {
      d = data[di];
      if (!d.hasText) continue;
      var w = d.width,
          w32 = w >> 5,
          h = d.y1 - d.y0;
      // Zero the buffer
      for (var i = 0; i < h * w32; i++) sprite[i] = 0;
      x = d.xoff;
      if (x == null) return;
      y = d.yoff;
      var seen = 0,
          seenRow = -1;
      for (var j = 0; j < h; j++) {
        for (var i = 0; i < w; i++) {
          var k = w32 * j + (i >> 5),
              m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0;
          sprite[k] |= m;
          seen |= m;
        }
        if (seen) seenRow = j;
        else {
          d.y0++;
          h--;
          j--;
          y++;
        }
      }
      d.y1 = d.y0 + seenRow;
      d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
    }
  }

  // Use mask-based collision detection.
  function cloudCollide(tag, board, sw) {
    sw >>= 5;
    var sprite = tag.sprite,
        w = tag.width >> 5,
        lx = tag.x - (w << 4),
        sx = lx & 0x7f,
        msx = 32 - sx,
        h = tag.y1 - tag.y0,
        x = (tag.y + tag.y0) * sw + (lx >> 5),
        last;
    for (var j = 0; j < h; j++) {
      last = 0;
      for (var i = 0; i <= w; i++) {
        if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0))
            & board[x + i]) return true;
      }
      x += sw;
    }
    return false;
  }

  function cloudBounds(bounds, d) {
    var b0 = bounds[0],
        b1 = bounds[1];
    if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0;
    if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0;
    if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1;
    if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1;
  }

  function collideRects(a, b) {
    return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y;
  }

  function archimedeanSpiral(size) {
    var e = size[0] / size[1];
    return function(t) {
      return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)];
    };
  }

  function rectangularSpiral(size) {
    var dy = 4,
        dx = dy * size[0] / size[1],
        x = 0,
        y = 0;
    return function(t) {
      var sign = t < 0 ? -1 : 1;
      // See triangular numbers: T_n = n * (n + 1) / 2.
      switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) {
        case 0:  x += dx; break;
        case 1:  y += dy; break;
        case 2:  x -= dx; break;
        default: y -= dy; break;
      }
      return [x, y];
    };
  }

  // TODO reuse arrays?
  function zeroArray(n) {
    var a = [],
        i = -1;
    while (++i < n) a[i] = 0;
    return a;
  }

  var cloudRadians = Math.PI / 180,
      cw = 1 << 11 >> 5,
      ch = 1 << 11,
      canvas,
      ratio = 1;

  if (typeof document !== "undefined") {
    canvas = document.createElement("canvas");
    canvas.width = 1;
    canvas.height = 1;
    ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2);
    canvas.width = (cw << 5) / ratio;
    canvas.height = ch / ratio;
  } else {
    // Attempt to use node-canvas.
    canvas = new Canvas(cw << 5, ch);
  }

  var c = canvas.getContext("2d"),
      spirals = {
        archimedean: archimedeanSpiral,
        rectangular: rectangularSpiral
      };
  c.fillStyle = c.strokeStyle = "red";
  c.textAlign = "center";

  if (typeof module === "object" && module.exports) module.exports = cloud;
  else (d3.layout || (d3.layout = {})).cloud = cloud;
})();