Created
October 6, 2010 16:43
-
-
Save rafapolo/613653 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python | |
# -*- coding: latin1 -*- | |
# author: rafael polo | |
# created_at: 06.out 3am | |
print "============" | |
print "Carregando libs..." | |
import re | |
try: | |
import matplotlib.pyplot as plt | |
except: | |
raise | |
import networkx as nx | |
# Abre livro | |
arquivo = '1.txt' | |
print "Abrindo %s..." % arquivo | |
livro = open(arquivo, 'r').read().decode("utf-8").lower() | |
# Retira espaços extras, pontos e vírgulas. | |
p = re.compile(r'\s+') | |
livro_limpo = p.sub(' ', livro) | |
livro_limpo = livro_limpo.replace(',', '') | |
livro_limpo = livro_limpo.replace(';', '') | |
livro_limpo = livro_limpo.replace('.', '') | |
# Separa palavras | |
palavras = livro_limpo.split(" ") | |
count = len(palavras) | |
print "%s palavras." % count | |
print "Gerando Grafo..." | |
G=nx.Graph() | |
palavra_anterior = "" | |
for palavra in palavras: | |
if (palavra != ""): | |
if (G.has_node(palavra)): | |
peso = G.node[palavra]['peso'] | |
peso += 1 | |
size = peso*200 | |
G.add_node(palavra, peso=peso, size=size) | |
else: | |
G.add_node(palavra, peso=1, size=200) | |
if (palavra_anterior != ""): | |
G.add_edge(palavra_anterior, palavra) | |
palavra_anterior = palavra | |
#nx.draw_networkx_nodes(G,pos=nx.spring_layout(G)) | |
print "Renderizando..." | |
nx.draw(G, node_size=[G.node[n]['size'] for n in G.nodes()]) | |
print "Ok" | |
print "============" | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment