Skip to content

Instantly share code, notes, and snippets.

@cbeer
Created February 11, 2013 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbeer/4758114 to your computer and use it in GitHub Desktop.
Save cbeer/4758114 to your computer and use it in GitHub Desktop.
Term Colocation d3 Cluster Graph
  • Put source text in src.txt
  • Run munge.rb
  • Copy content into index.html
[["licensed", "to", "the"], ["to", "the", "apache"], ["the", "apache", "software"], ["apache", "software", "foundation"], ["software", "foundation", "asf"], ["foundation", "asf", "under"], ["asf", "under", "one"], ["under", "one", "or"], ["one", "or", "more"], ["or", "more", "contributor"], ["more", "contributor", "license"], ["contributor", "license", "agreements"], ["license", "agreements", "see"], ["agreements", "see", "the"], ["see", "the", "notice"], ["the", "notice", "file"], ["notice", "file", "distributed"], ["file", "distributed", "with"], ["distributed", "with", "this"], ["with", "this", "work"], ["this", "work", "for"], ["work", "for", "additional"], ["for", "additional", "information"], ["additional", "information", "regarding"], ["information", "regarding", "copyright"], ["regarding", "copyright", "ownership"], ["copyright", "ownership", "the"], ["ownership", "the", "asf"], ["the", "asf", "licenses"], ["asf", "licenses", "this"], ["licenses", "this", "file"], ["this", "file", "to"], ["file", "to", "you"], ["to", "you", "under"], ["you", "under", "the"], ["under", "the", "apache"], ["the", "apache", "license"], ["apache", "license", "version"], ["license", "version", "the"], ["version", "the", "license"], ["the", "license", "you"], ["license", "you", "may"], ["you", "may", "not"], ["may", "not", "use"], ["not", "use", "this"], ["use", "this", "file"], ["this", "file", "except"], ["file", "except", "in"], ["except", "in", "compliance"], ["in", "compliance", "with"], ["compliance", "with", "the"], ["with", "the", "license"], ["the", "license", "you"], ["license", "you", "may"], ["you", "may", "obtain"], ["may", "obtain", "a"], ["obtain", "a", "copy"], ["a", "copy", "of"], ["copy", "of", "the"], ["of", "the", "license"], ["the", "license", "at"], ["license", "at", "httpwwwapacheorglicenseslicense"], ["at", "httpwwwapacheorglicenseslicense", "unless"], ["httpwwwapacheorglicenseslicense", "unless", "required"], ["unless", "required", "by"], ["required", "by", "applicable"], ["by", "applicable", "law"], ["applicable", "law", "or"], ["law", "or", "agreed"], ["or", "agreed", "to"], ["agreed", "to", "in"], ["to", "in", "writing"], ["in", "writing", "software"], ["writing", "software", "distributed"], ["software", "distributed", "under"], ["distributed", "under", "the"], ["under", "the", "license"], ["the", "license", "is"], ["license", "is", "distributed"], ["is", "distributed", "on"], ["distributed", "on", "an"], ["on", "an", "as"], ["an", "as", "is"], ["as", "is", "basis"], ["is", "basis", "without"], ["basis", "without", "warranties"], ["without", "warranties", "or"], ["warranties", "or", "conditions"], ["or", "conditions", "of"], ["conditions", "of", "any"], ["of", "any", "kind"], ["any", "kind", "either"], ["kind", "either", "express"], ["either", "express", "or"], ["express", "or", "implied"], ["or", "implied", "see"], ["implied", "see", "the"], ["see", "the", "license"], ["the", "license", "for"], ["license", "for", "the"], ["for", "the", "specific"], ["the", "specific", "language"], ["specific", "language", "governing"], ["language", "governing", "permissions"], ["governing", "permissions", "and"], ["permissions", "and", "limitations"], ["and", "limitations", "under"], ["limitations", "under", "the"], ["under", "the", "license"], ["the", "license", null], ["license", null, null]]
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
var w = 960,
h = 1000;
var cluster = d3.layout.cluster()
.size([h, w - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(40, 0)");
var root = [{"name":"licensed","children":[{"name":"to","children":[{"name":"the"}]}]},{"name":"to","children":[{"name":"the","children":[{"name":"apache"}]},{"name":"you","children":[{"name":"under"}]},{"name":"in","children":[{"name":"writing"}]}]},{"name":"the","children":[{"name":"apache","children":[{"name":"software"},{"name":"license"}]},{"name":"notice","children":[{"name":"file"}]},{"name":"asf","children":[{"name":"licenses"}]},{"name":"license","children":[{"name":"you"},{"name":"at"},{"name":"is"},{"name":"for"},{"name":null}]},{"name":"specific","children":[{"name":"language"}]}]},{"name":"apache","children":[{"name":"software","children":[{"name":"foundation"}]},{"name":"license","children":[{"name":"version"}]}]},{"name":"software","children":[{"name":"foundation","children":[{"name":"asf"}]},{"name":"distributed","children":[{"name":"under"}]}]},{"name":"foundation","children":[{"name":"asf","children":[{"name":"under"}]}]},{"name":"asf","children":[{"name":"under","children":[{"name":"one"}]},{"name":"licenses","children":[{"name":"this"}]}]},{"name":"under","children":[{"name":"one","children":[{"name":"or"}]},{"name":"the","children":[{"name":"apache"},{"name":"license"}]}]},{"name":"one","children":[{"name":"or","children":[{"name":"more"}]}]},{"name":"or","children":[{"name":"more","children":[{"name":"contributor"}]},{"name":"agreed","children":[{"name":"to"}]},{"name":"conditions","children":[{"name":"of"}]},{"name":"implied","children":[{"name":"see"}]}]},{"name":"more","children":[{"name":"contributor","children":[{"name":"license"}]}]},{"name":"contributor","children":[{"name":"license","children":[{"name":"agreements"}]}]},{"name":"license","children":[{"name":"agreements","children":[{"name":"see"}]},{"name":"version","children":[{"name":"the"}]},{"name":"you","children":[{"name":"may"}]},{"name":"at","children":[{"name":"httpwwwapacheorglicenseslicense"}]},{"name":"is","children":[{"name":"distributed"}]},{"name":"for","children":[{"name":"the"}]},{"name":null,"children":[{"name":null}]}]},{"name":"agreements","children":[{"name":"see","children":[{"name":"the"}]}]},{"name":"see","children":[{"name":"the","children":[{"name":"notice"},{"name":"license"}]}]},{"name":"notice","children":[{"name":"file","children":[{"name":"distributed"}]}]},{"name":"file","children":[{"name":"distributed","children":[{"name":"with"}]},{"name":"to","children":[{"name":"you"}]},{"name":"except","children":[{"name":"in"}]}]},{"name":"distributed","children":[{"name":"with","children":[{"name":"this"}]},{"name":"under","children":[{"name":"the"}]},{"name":"on","children":[{"name":"an"}]}]},{"name":"with","children":[{"name":"this","children":[{"name":"work"}]},{"name":"the","children":[{"name":"license"}]}]},{"name":"this","children":[{"name":"work","children":[{"name":"for"}]},{"name":"file","children":[{"name":"to"},{"name":"except"}]}]},{"name":"work","children":[{"name":"for","children":[{"name":"additional"}]}]},{"name":"for","children":[{"name":"additional","children":[{"name":"information"}]},{"name":"the","children":[{"name":"specific"}]}]},{"name":"additional","children":[{"name":"information","children":[{"name":"regarding"}]}]},{"name":"information","children":[{"name":"regarding","children":[{"name":"copyright"}]}]},{"name":"regarding","children":[{"name":"copyright","children":[{"name":"ownership"}]}]},{"name":"copyright","children":[{"name":"ownership","children":[{"name":"the"}]}]},{"name":"ownership","children":[{"name":"the","children":[{"name":"asf"}]}]},{"name":"licenses","children":[{"name":"this","children":[{"name":"file"}]}]},{"name":"you","children":[{"name":"under","children":[{"name":"the"}]},{"name":"may","children":[{"name":"not"},{"name":"obtain"}]}]},{"name":"version","children":[{"name":"the","children":[{"name":"license"}]}]},{"name":"may","children":[{"name":"not","children":[{"name":"use"}]},{"name":"obtain","children":[{"name":"a"}]}]},{"name":"not","children":[{"name":"use","children":[{"name":"this"}]}]},{"name":"use","children":[{"name":"this","children":[{"name":"file"}]}]},{"name":"except","children":[{"name":"in","children":[{"name":"compliance"}]}]},{"name":"in","children":[{"name":"compliance","children":[{"name":"with"}]},{"name":"writing","children":[{"name":"software"}]}]},{"name":"compliance","children":[{"name":"with","children":[{"name":"the"}]}]},{"name":"obtain","children":[{"name":"a","children":[{"name":"copy"}]}]},{"name":"a","children":[{"name":"copy","children":[{"name":"of"}]}]},{"name":"copy","children":[{"name":"of","children":[{"name":"the"}]}]},{"name":"of","children":[{"name":"the","children":[{"name":"license"}]},{"name":"any","children":[{"name":"kind"}]}]},{"name":"at","children":[{"name":"httpwwwapacheorglicenseslicense","children":[{"name":"unless"}]}]},{"name":"httpwwwapacheorglicenseslicense","children":[{"name":"unless","children":[{"name":"required"}]}]},{"name":"unless","children":[{"name":"required","children":[{"name":"by"}]}]},{"name":"required","children":[{"name":"by","children":[{"name":"applicable"}]}]},{"name":"by","children":[{"name":"applicable","children":[{"name":"law"}]}]},{"name":"applicable","children":[{"name":"law","children":[{"name":"or"}]}]},{"name":"law","children":[{"name":"or","children":[{"name":"agreed"}]}]},{"name":"agreed","children":[{"name":"to","children":[{"name":"in"}]}]},{"name":"writing","children":[{"name":"software","children":[{"name":"distributed"}]}]},{"name":"is","children":[{"name":"distributed","children":[{"name":"on"}]},{"name":"basis","children":[{"name":"without"}]}]},{"name":"on","children":[{"name":"an","children":[{"name":"as"}]}]},{"name":"an","children":[{"name":"as","children":[{"name":"is"}]}]},{"name":"as","children":[{"name":"is","children":[{"name":"basis"}]}]},{"name":"basis","children":[{"name":"without","children":[{"name":"warranties"}]}]},{"name":"without","children":[{"name":"warranties","children":[{"name":"or"}]}]},{"name":"warranties","children":[{"name":"or","children":[{"name":"conditions"}]}]},{"name":"conditions","children":[{"name":"of","children":[{"name":"any"}]}]},{"name":"any","children":[{"name":"kind","children":[{"name":"either"}]}]},{"name":"kind","children":[{"name":"either","children":[{"name":"express"}]}]},{"name":"either","children":[{"name":"express","children":[{"name":"or"}]}]},{"name":"express","children":[{"name":"or","children":[{"name":"implied"}]}]},{"name":"implied","children":[{"name":"see","children":[{"name":"the"}]}]},{"name":"specific","children":[{"name":"language","children":[{"name":"governing"}]}]},{"name":"language","children":[{"name":"governing","children":[{"name":"permissions"}]}]},{"name":"governing","children":[{"name":"permissions","children":[{"name":"and"}]}]},{"name":"permissions","children":[{"name":"and","children":[{"name":"limitations"}]}]},{"name":"and","children":[{"name":"limitations","children":[{"name":"under"}]}]},{"name":"limitations","children":[{"name":"under","children":[{"name":"the"}]}]}];
var nodes = cluster.nodes(root.filter(function(x) { return x["name"] == "to"})[0]),
links = cluster.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
d3.select(self.frameElement).style("height", h + "px");
</script>
</body>
</html>
require 'json'
src = File.read('src.txt')
words = src.split(' ').map { |x| x.gsub(/[^A-Za-z]/, '').downcase }.reject { |x| x.empty? }
zzz = words.each_with_index.inject(Hash.new) { |memo, (x, i)| memo[x] ||= { "name" => x, "children" => {} }; memo[x]["children"][words[i+1]] ||= { "name" => words[i+1], "children" => {} }; memo[x]["children"][words[i+1]]["children"][words[i+2]] = { "name" => words[i+2] }; memo }
print zzz.values.map { |x| { "name" => x["name"], "children" => x["children"].values.map { |x| { "name" => x["name"], "children" => x["children"].values } } } }.to_json
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment