Animals Decision Tree in Dot

Here we read the text file database from Animals and render it as a tree using Coffeescript to produce Dot.

input = text.split "\n" dot = [] quote = (string) -> "\"#{string.replace /(\w+ \w+) /g, '$1\\n'}\"" node = (path)-> query = input.shift() if query[0] == 'a' dot.push "#{path} [label=#{quote query} shape=box]" else dot.push "#{path} [label=#{quote query} fillcolor=gold]" dot.push "#{path} -> #{node(path+'y')} [label=yes]" dot.push "#{path} -> #{node(path+'n')} [label=no]" path node('r') fs = require 'fs' fs.writeFile '', "digraph animals { node [style=filled] #{dot.join("\n")} }"

This program writes a text file which we render as SVG using Graphviz. expand