Permalink
Browse files

Merge branch 'master' of https://github.com/bruntonspall/fthackday

  • Loading branch information...
2 parents 0d2783d + c523553 commit 879db3b0bf630546d45d1d5c7e8427525e71c72d @bruntonspall committed May 24, 2012
Showing with 432 additions and 3 deletions.
  1. +7 −3 app/controllers/Application.scala
  2. +42 −0 app/views/cloud.scala.html
  3. +1 −0 app/views/main.scala.html
  4. +1 −0 conf/routes
  5. +381 −0 public/javascripts/cloud.js
@@ -61,12 +61,12 @@ object Application extends Controller {
}}
- val stopwords = """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|shall"""
+ val stopwords = """-|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|shall"""
def allWords = Action {
val headlineWords = allStories map { stories =>
val allTheWords = stories.flatMap {s => s.headline.split(" ")}
- val interestingWords = allTheWords.filterNot(w => stopwords.contains(w))
+ val interestingWords = allTheWords.filterNot(w => stopwords.contains(w.toLowerCase))
val wordsWithCount: Map[String, Int] = interestingWords.foldLeft(Map[String, Int]()){ (m: Map[String, Int], s: String) =>
m + (s -> (m.getOrElse(s, 0) + 1))
@@ -75,10 +75,14 @@ object Application extends Controller {
wordsWithCount.keys.toList.sortBy(s => wordsWithCount.get(s)).reverse
}
Async {
- headlineWords map {w => Ok(w.mkString(", "))}
+ headlineWords map {w => Ok(toJson(w))}
}
}
+ def wordCloud = Action {
+ Ok(views.html.cloud())
+ }
+
def colour_for(source: String):String = source match {
case "FT" => "pink"
case "GU" => "blue"
@@ -0,0 +1,42 @@
+@main("hot words!!!") {
+
+<div class="container">
+ <div class="row">
+ <div class="push2 span8">
+ <div id="myvis"></div>
+ </div>
+ </div>
+</div>
+<script>
+
+ var fontSize = d3.scale.log().range([10, 100]);
+
+ $.getJSON("/allwords", function(results) {
+ d3.layout.cloud().size([960, 600])
+ .words(results.map(function(d) {
+ return {text: d, size: 10 + Math.random() * 90};
+ }))
+ .rotate(function() { return ~~(Math.random() * 5) * 30 - 60; })
+ .fontSize(function(d) { return d.size; })
+ .on("end", draw)
+ .start();
+ })
+
+ function draw(words) {
+ d3.select("#myvis").append("svg")
+ .attr("width", 960)
+ .attr("height", 600)
+ .append("g")
+ .attr("transform", "translate(150,150)")
+ .selectAll("text")
+ .data(words)
+ .enter().append("text")
+ .style("font-size", function(d) { return d.size + "px"; })
+ .attr("text-anchor", "middle")
+ .attr("transform", function(d) {
+ return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
+ })
+ .text(function(d) { return d.text; });
+ }
+</script>
+}
@@ -9,6 +9,7 @@
<script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/d3.v2.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/tim-lite.min.js")" type="text/javascript"></script>
+ <script src="@routes.Assets.at("javascripts/cloud.js")" type="text/javascript"></script>
<link rel="stylesheet" href="@routes.Assets.at("stylesheets/bootstrap.css")">
<script src="http://static.simile.mit.edu/timeline/api-2.3.0/timeline-api.js?bundle=true" type="text/javascript"></script>
View
@@ -12,6 +12,7 @@ GET /stories/gu controllers.Application.stats_gu
GET /stories/wp controllers.Application.stats_wp
GET /stories controllers.Application.stats
GET /allwords controllers.Application.allWords
+GET /stories/wordcloud controllers.Application.wordCloud
GET /stories/timeline.json controllers.Application.stories_timeline_js
Oops, something went wrong.

0 comments on commit 879db3b

Please sign in to comment.