Created
May 6, 2017 08:07
-
-
Save dcorking/baff82a5674abef5ea2db1b04b52482f to your computer and use it in GitHub Desktop.
interactive cluster graph (spider web) visualization of TMDb movie recommendations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" | |
For a workspace (aka playground) | |
Works in a Moose 6.0 image | |
dcorking, 2017, public domain Creative Commons CC0 | |
" | |
path := '/Users/dcorking/workspace/tmdb-ruby-play/nodes.csv'. | |
stream := FileStream readOnlyFileNamed: path. | |
string := stream contents. | |
stream close. | |
nodes_table := RTTabTable new input: string usingDelimiter: $,. | |
path := '/Users/dcorking/workspace/tmdb-ruby-play/edges.csv'. | |
stream := FileStream readOnlyFileNamed: path. | |
string := stream contents. | |
stream close. | |
edges_table := RTTabTable new input: string usingDelimiter: $,. | |
"This works better if nodes is a dictionary". | |
nodeDict := Dictionary new. | |
"nodeDict at: 9999 put: 'Monsters Inc'." | |
nodes_table values do: [ :row | nodeDict at: ((row at: 2) asInteger) put: (row at: 1)]. | |
edges_table values do: [ :row | nodeDict at: ((row at: 3) asInteger) put: (row at: 1)]. | |
nodeDict. | |
edgesArray := edges_table values collect: [ :edge | | |
from_id := (edge at: 2) asInteger. | |
to_id := (edge at: 3) asInteger. | |
fromTitle := nodeDict at: from_id. | |
toTitle := nodeDict at: to_id. | |
{ fromTitle . toTitle } | |
]. | |
nonUniqueEdges := edgesArray collect: [ :row | (row at: 1) -> (row at: 2) ]. | |
c := RTMondrian new. | |
c shape circle | |
color: Color red trans. | |
c nodes: nodeDict. | |
c layout cluster. | |
c. | |
c edges source: nonUniqueEdges connectFrom: #key to: #value. | |
c layout cluster. | |
c. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dcorking, 2017, public domain Creative Commons CC0 | |
require 'themoviedb-api' | |
require 'csv' | |
Tmdb::Api.key ENV['TMDB_KEY'] | |
similars = Tmdb::Movie.similar(155).results | |
def edge_row_content(from_movie, to_movie) | |
[ to_movie.title, from_movie.id, to_movie.id ] | |
end | |
def node_row_content(movie) | |
[ movie.title, movie.id ] | |
end | |
edges_array = [] | |
nodes_array = [] | |
similars.each do |from_movie| | |
grandchildren = | |
Tmdb::Movie.similar(from_movie.id).results | |
edges_array << grandchildren.collect{ |to_movie| edge_row_content(from_movie, to_movie) } | |
nodes_array << node_row_content(from_movie) | |
end | |
flat_edges = edges_array.flatten(1) | |
CSV.open('edge.csv', 'w') do |csv| | |
flat_edges.each do |row| | |
csv << row | |
end | |
end | |
CSV.open('nodes.csv', 'w') do |csv| | |
nodes_array.each do |row| | |
csv << row | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment