Created
December 30, 2010 16:10
-
-
Save jaymzcd/759935 to your computer and use it in GitHub Desktop.
Makes an HTML file with a canvas element and lots of fillText commands which create a sort of messy wordcloud from a file with lines and lines of sentances. Grouping is slightly dynamic based on common words.
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
| #!/usr/bin/env python2 | |
| import string | |
| import re | |
| import random | |
| EXCLUDED = ["", "\n", "a","able","about","across","after","all","almost","also","am","among","ive", "an","and","any", "im", "are","as","at","be","because","been","but","by","can","cannot","could","dear","did","do","does","either","else","ever","every","for","from","get","got","had","has","have","he","her","hers","him","his","how","however","i","if","in","into","is","it","its","just","least","let","like","likely","may","me","might","most","must","my","neither","no","nor","not","of","off","often","on","only","or","other","our","own","rather","said","say","says","she","should","since","so","some","than","that","the","their","them","then","there","these","they","this","tis","to","too","twas","us","wants","was","we","were","what","when","where","which","while","who","whom","why","will","with","would","yet","you","your"] | |
| CANVAS_SIZE = 600 | |
| HEADER = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
| <html lang="en"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> | |
| <title></title> | |
| <style type="text/css"> | |
| * { font-family: Helvetica; } | |
| span { border: solid 1px #ccc;} | |
| #canvas { } | |
| </style> | |
| </head> | |
| <body> | |
| <canvas id="canvas" width="%(size)d" height="%(size)d"></canvas> | |
| <script type="text/javascript"> | |
| var ctx= document.getElementById("canvas").getContext("2d"); | |
| ctx.fillStyle = "black"; | |
| """ % { 'size': CANVAS_SIZE } | |
| FOOTER = """ | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| texts = open('nina-sent.txt', 'r') | |
| outfile = open('/tmp/nina.html', 'w') | |
| allwords = list() | |
| def grouped_elements(words, offset, cnt=2): | |
| r = range(offset, offset+cnt) | |
| group = '' | |
| for index in r: | |
| try: | |
| group += ' '+words[index] | |
| except IndexError: | |
| pass | |
| return group | |
| for line in texts: | |
| words = re.findall('\w+', line.translate(string.maketrans("",""), string.punctuation).lower()) | |
| for index, word in enumerate(words): | |
| if word in EXCLUDED: | |
| allwords.append(grouped_elements(words, index, 4)) | |
| else: | |
| allwords.append(grouped_elements(words, index)) | |
| word_counts = list() | |
| allwords.sort() | |
| for group in set(allwords): | |
| word_counts.append((allwords.count(group), group)) | |
| word_counts.sort() | |
| random.shuffle(word_counts) | |
| outfile.write(HEADER) | |
| yoffset = 0 | |
| xoffset = 10 | |
| for wc in word_counts: | |
| color = '%02x' % random.randint(80, 170) * 3 | |
| if wc[0]>1: | |
| size = wc[0]*5.0 | |
| if size>80: | |
| size = 80 | |
| color = 'ff' + '%02x' % random.randint(0, 50)*2 | |
| if size<12: | |
| size = 10 | |
| color = '%02x' % random.randint(220, 240) * 3 | |
| yoffset += size | |
| if yoffset > CANVAS_SIZE: | |
| yoffset = 0 | |
| xoffset += 40 | |
| text = wc[1] | |
| outfile.write("""ctx.font = "%(size)spx Helvetica"; | |
| ctx.fillStyle = "#%(color)s"; | |
| ctx.fillText("%(word)s", %(xoffset)s, %(yoffset)s); | |
| \n""" % { | |
| 'size': size, | |
| 'word': text, | |
| 'count': wc[0], | |
| 'xoffset': xoffset + random.randint(0, 20) - 10, | |
| 'yoffset': yoffset, | |
| 'color': color, | |
| } | |
| ) | |
| outfile.write(FOOTER) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment