Skip to content

Instantly share code, notes, and snippets.

@nanki
Created August 17, 2012 09:52
Show Gist options
  • Save nanki/3377562 to your computer and use it in GitHub Desktop.
Save nanki/3377562 to your computer and use it in GitHub Desktop.
generate .gml from space-separated-values.
#!/usr/bin/env ruby
i = 0
hash = {}
edges = ''
ARGF.each do |line|
id1, id2s = line.strip.split /\s/
id2s.split(",").each do |id2|
hash[id1] ||= i += 1
hash[id2] ||= i += 1
next if id1 == id2
edges.concat <<EOF
edge [
source #{hash[id1]}
target #{hash[id2]}
]
EOF
end
end
puts "graph ["
hash.keys.each do |node|
print <<EOF
node [
id #{hash[node]}
label "#{node}"
]
EOF
end
print edges
puts "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment