[betweenness]all_annual_genre.py 2.23 KB
#-*-encoding utf-8 -*-
import matplotlib.pyplot as plt
import networkx as nx
from openpyxl import load_workbook


movie_genre_list = []
movie_audience_list = []
movie_edge_list = []
movie_graph_list = []
movie_check_list = []
max_audience=-1
load_wb = load_workbook("Movie_data/movie_top500.xlsx", data_only=True)
load_ws = load_wb['Sheet1']


def make_edge(movielist):
    edge_list = []
    for i in range(0,len(movielist)):
            for j in range(0, len(movielist)):
                if i == j:
                    break
                else:
                    edge_list.append((movielist[i],movielist[j]))
    return edge_list





for i in range(7,507,1):
    distributor_name='L'+str(i)
    genre_name='M'+str(i)
    name3=(str(load_ws[distributor_name].value) +','+ str(load_ws[genre_name].value)).split(',')
    movie_audience_list.append(int(load_ws['F'+str(i)].value))
    movie_genre_list.append(name3)


for i in range(0, 500):
    if movie_genre_list[i] in movie_check_list:
       movie_audience_list[i]=movie_audience_list[i]+movie_audience_list[movie_check_list.index(movie_genre_list[i])]
       movie_audience_list[movie_check_list.index(movie_genre_list[i])]=movie_audience_list[i]
       movie_check_list.append(movie_genre_list[i])
    else:
        movie_check_list.append(movie_genre_list[i])
    if movie_audience_list[i] > max_audience:
        max_audience=movie_audience_list[i]
        

for i in range(0, 500):
    movie_graph = nx.Graph()
    movie_edge = make_edge(movie_genre_list[i])    
    movie_edge_list.append(movie_edge)
    movie_graph.add_nodes_from(movie_genre_list[i])
    movie_graph.add_edges_from(movie_edge_list[i], weight = 1-(movie_audience_list[i]/(max_audience+1)))
    movie_graph_list.append(movie_graph)


movie_total = nx.Graph()

for i in range(0,500):
    movie_total = nx.compose(movie_total, movie_graph_list[i])

degree = movie_total.degree()
nx.write_gexf(movie_total, "Gephi/[betweenness]all_annual_genre.gexf")




plt.figure(figsize=(20,20))
pos = nx.spring_layout(movie_total, iterations = 10)
nx.draw(movie_total, pos, alpha = 1, line_color= 'black', linewidths = 0, width = 0.05 ,node_size = 100, node_color = 'w', with_labels = True, font_family='NanumGothic', font_color='black', font_size=8)
plt.show()