Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from PIL import Image, ImageDraw
- import random
- import re
- import gc
- import codecs
- import operator
- wlinks = dict()
- wpos = dict()
- grid = dict()
- GRID_SIZE = 3000
- def intToRGB(x,maxa):
- if x>maxa: x = maxa
- hmaxa = maxa/3
- if x>2*hmaxa:
- red = 255
- green = (3-x/hmaxa)*255
- blue = 0
- elif x>hmaxa:
- red = (2-x/hmaxa)*255
- green = 255
- blue = 0
- else:
- red = 0
- green = 255
- blue = (1-x/hmaxa)*255
- return (int(red),int(green),int(blue))
- def main():
- lnum = 0
- ind = 0
- wlist = dict()
- wsize = dict()
- for line in codecs.open('indexes.csv','r', 'utf-8', 'strict', 20000000):
- words = line.split('\t')
- index2 = int(words[0])
- wsize[index2] = int(words[2])
- wlist[words[1]] = index2
- del words
- del line
- for line in codecs.open('mygraph2.csv','r', 'utf-8', 'strict', 20000000):
- lnum += 1
- if (lnum % 100000) == 0: print 'Line number: %i'%(lnum/100000)
- words = line.split('\t')
- del line
- words0 = words[0].strip().lower()
- words1 = words[1].strip().lower()
- del words
- words0 = wlist[words0]
- words1 = wlist[words1]
- if not words0 in wlinks:
- wlinks[words0] = set()
- x = random.randint(0,GRID_SIZE-1)
- y = random.randint(0,GRID_SIZE-1)
- wpos[words0] = (x,y)
- if not words1 in wlinks:
- wlinks[words1] = set()
- x = random.randint(0,GRID_SIZE-1)
- y = random.randint(0,GRID_SIZE-1)
- wpos[words1] = (x,y)
- wlinks[words0].add(words1)
- wlinks[words1].add(words0)
- del wlist
- # for x in xrange(0,GRID_SIZE):
- # for y in xrange(0,GRID_SIZE):
- # grid[x,y] = None
- for it in xrange(1,1000):
- print 'Iteration %i' % it
- for wordname, links in wlinks.iteritems():
- oldpos = wpos[wordname]
- x = oldpos[0]
- y = oldpos[1]
- for link in iter(links):
- x += wpos[link][0]
- y += wpos[link][1]
- x = int(x/(len(links)+1))+random.randint(-5,5)
- y = int(y/(len(links)+1))+random.randint(-5,5)
- if x>0 and x<GRID_SIZE-1 and y>0 and y<GRID_SIZE-1:
- for a in range(-1,2):
- for b in range(-1,2):
- if (x+a,y+b) in grid and grid[x+a,y+b] != None:
- x-=a*10
- y-=b*10
- if x<0: x = 0
- if y<0: y = 0
- if x>=GRID_SIZE: x = GRID_SIZE-1
- if y>=GRID_SIZE: y = GRID_SIZE-1
- wpos[wordname] = [int(x),int(y)]
- old = None
- if (x,y) in grid: old = grid[x,y]
- grid[x,y] = wordname
- grid[oldpos[0],oldpos[1]] = old
- if old != None: wpos[old] = [oldpos[0],oldpos[1]]
- if it%10 == 0:
- im = Image.new('RGB', (GRID_SIZE, GRID_SIZE), "black")
- pixels = im.load()
- for x in xrange(0,GRID_SIZE):
- for y in xrange(0,GRID_SIZE):
- if not (x,y) in grid or grid[x,y] == None:
- pixels[x,y] = (0,0,0)
- else:
- bri = wsize[grid[x,y]]
- pixels[x,y] = intToRGB(bri,100)
- im.save("output_image_"+str(it)+".png", "PNG")
- del im
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement