Skip to content

Instantly share code, notes, and snippets.

@hepplerj
Last active August 29, 2015 14:07
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 hepplerj/373f59d91bd101d5d5c2 to your computer and use it in GitHub Desktop.
Save hepplerj/373f59d91bd101d5d5c2 to your computer and use it in GitHub Desktop.
Tree Diagram
{
"name": "x",
"children": [
{
"name": "i",
"children": [
{
"name": "n",
"children": [
{
"name": "q",
"children": [
{
"name": "i",
"children": [
{
"name": "n",
"children": [
{
"name": "g",
"children": [
{
"name": "n",
"children": [
{
"name": "i",
"children": [
{
"name": "a",
"children": [
{
"name": "n",
"children": [
{
"name": "1",
"children": []
},
{
"name": "_",
"children": []
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"name": "1",
"children": [
{
"name": "q",
"children": [
{
"name": "i",
"children": [
{
"name": "n",
"children": [
{
"name": "g",
"children": [
{
"name": "4",
"children": [
{
"name": "n",
"children": [
{
"name": "i",
"children": [
{
"name": "a",
"children": [
{
"name": "n",
"children": [
{
"name": "1",
"children": []
}
]
}
]
}
]
},
{
"name": "3",
"children": []
}
]
}
]
}
]
}
]
}
]
},
{
"name": "n",
"children": [
{
"name": "2",
"children": []
}
]
}
]
}
]
},
{
"name": "_",
"children": [
{
"name": "q",
"children": [
{
"name": "n",
"children": [
{
"name": "2",
"children": []
}
]
}
]
}
]
}
]
}
]
},
{
"name": "1",
"children": [
{
"name": "q",
"children": [
{
"name": "i",
"children": [
{
"name": "n",
"children": [
{
"name": "g",
"children": [
{
"name": "4",
"children": [
{
"name": "n",
"children": [
{
"name": "i",
"children": [
{
"name": "a",
"children": [
{
"name": "n",
"children": [
{
"name": "_",
"children": []
}
]
}
]
}
]
},
{
"name": "3",
"children": []
}
]
}
]
}
]
}
]
}
]
},
{
"name": "n",
"children": [
{
"name": "2",
"children": []
}
]
}
]
}
]
},
{
"name": "_",
"children": [
{
"name": "q",
"children": [
{
"name": "i",
"children": [
{
"name": "n",
"children": [
{
"name": "g",
"children": [
{
"name": "4",
"children": [
{
"name": "n",
"children": [
{
"name": "i",
"children": [
{
"name": "a",
"children": [
{
"name": "n",
"children": [
{
"name": "_",
"children": []
}
]
}
]
}
]
},
{
"name": "3",
"children": []
}
]
}
]
}
]
}
]
}
]
},
{
"name": "n",
"children": [
{
"name": "2",
"children": []
}
]
}
]
}
]
}
]
}
xin_qn2
x_qing4n3
x_qing4nian_
x_qn2
xinqingnian_
xinqingnian1
xin1qn2
xin1qing4n3
xin1qing4nian1
x1qing4n3
x1qing4nian_
x1qn2
<!DOCTYPE html>
<meta charset="utf-8">
<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>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 800,
height = 550;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
d3.json("data.json", function(error, root) {
var nodes = cluster.nodes(root),
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", height + "px");
</script>
#!/usr/bin/python
from collections import defaultdict
import json
def tree():
return defaultdict(tree)
nested = defaultdict(tree)
words = open("single.txt").read().splitlines()
words_output = open("out_single.json", "wb")
for word in words:
node = nested
for char in word:
node = node[char.lower()]
def convert(d):
return dict((key, convert(value)) for (key,value) in d.iteritems()) if isinstance(d, defaultdict) else d
def format(d):
children = []
for (key, value) in d.iteritems():
children += [{"name":key, "children":format(value)}]
return children
words_output.write(json.dumps(format(convert(nested))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment