Created
December 11, 2014 21:02
-
-
Save pkpp1233/7f187177e225914e2dd8 to your computer and use it in GitHub Desktop.
Blockspring wordcloud.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var blockspring = require('blockspring'); | |
var d3 = global.d3 = require('d3'); | |
var fs = require('fs'); | |
var cloud = require('d3.layout.cloud'); | |
blockspring.define(function(request, response){ | |
var text = request.params.text, | |
width = request.params.width, | |
height = request.params.height, | |
rotate_words = request.params.rotate_flag, | |
unique_word_counts = {}; | |
if (!text){ | |
response.addErrorOutput("Please enter some text", "Couldn't parse the your text input for words."); | |
response.end(); | |
return; | |
} | |
var wordmap = processText(text); | |
var svg = d3.select('body').html('').append('svg') | |
.attr('xmlns', 'http://www.w3.org/2000/svg') | |
.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink'); | |
drawCloud(wordmap, width, height, svg, rotate_words, writeSvgToFile); | |
// write svg to file and add to response. | |
function writeSvgToFile(body){ | |
var svg_string = body.node().innerHTML; | |
fs.writeFile('graph.svg', svg_string, function (err) { | |
if (err) throw err; | |
response.addFileOutput("image", "graph.svg", function(){ | |
response.end() | |
}); | |
}); | |
} | |
}); | |
function processText(text){ | |
var unique_word_counts = {}, | |
exclude_words = "i,me,my,myself,we,us,our,ours,ourselves,you,your,yours,yourself,yourselves,he,him,his,himself,she,her,hers,herself,it,its,itself,they,them,their,theirs,themselves,what,which,who,whom,whose,this,that,these,those,am,is,are,was,were,be,been,being,have,has,had,having,do,does,did,doing,will,would,should,can,could,ought,i'm,you're,he's,she's,it's,we're,they're,i've,you've,we've,they've,i'd,you'd,he'd,she'd,we'd,they'd,i'll,you'll,he'll,she'll,we'll,they'll,isn't,aren't,wasn't,weren't,hasn't,haven't,hadn't,doesn't,don't,didn't,won't,wouldn't,shan't,shouldn't,can't,cannot,couldn't,mustn't,let's,that's,who's,what's,here's,there's,when's,where's,why's,how's,a,an,the,and,but,if,or,because,as,until,while,of,at,by,for,with,about,against,between,into,through,during,before,after,above,below,to,from,up,upon,down,in,out,on,off,over,under,again,further,then,once,here,there,when,where,why,how,all,any,both,each,few,more,most,other,some,such,no,nor,not,only,own,same,so,than,too,very,say,says,said,poop,shall"; | |
var tokenized_text = text.split(/[ '\-\(\)\*":;\[\]|{},.!?]+/); | |
tokenized_text.forEach(function(raw_word){ | |
var word = raw_word.toLowerCase(); | |
if (word != "" && exclude_words.indexOf(word)==-1 && word.length>1){ | |
word in unique_word_counts ? | |
unique_word_counts[word]++ : | |
(unique_word_counts[word] = 1 + Math.random()); | |
} | |
}); | |
var wordmap = d3.entries(unique_word_counts).sort(function(a,b){ | |
return b.value - a.value; | |
}); | |
return wordmap; | |
} | |
function drawCloud(wordmap, width, height, svg, rotate_words, callback){ | |
var max = Math.min(width / 5, height / 5, 100), | |
font_size = d3.scale.linear() | |
.domain([ 1, d3.max( wordmap, function(d) { return d.value; })]) | |
.range([max / 10, max]), | |
fill = d3.scale.category20(); | |
cloud().size([width, height]) | |
.words(wordmap) | |
.timeInterval(20) | |
.padding(3) | |
.spiral("rectangular") | |
.fontSize(function(d) { return font_size(d.value); }) | |
.font("Impact") | |
.text(function(d) { return d.key; }) | |
.rotate(function() { | |
return rotate_words ? (~~(Math.random() * 2) * 90) : 0; | |
}) | |
.on("end", function(words){ | |
cloud().stop(); | |
svg | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + [width >> 1, height >> 1] + ")") | |
.selectAll("text") | |
.data(words) | |
.enter().append("text") | |
.style("font-family", "Impact") | |
.style("font-size", function(d) { return font_size(d.value) + "px"; }) | |
.style("fill", function(d, i) { return fill(i); }) | |
.attr("text-anchor", "middle") | |
.attr("transform", function(d) { | |
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; | |
}) | |
.text(function(d) { return d.key; }); | |
callback(d3.select('body')); | |
}) | |
.start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment