Created
April 13, 2018 01:20
-
-
Save laurenrace/6cb36ae22041c1f9bc3487ed9cc3b7f2 to your computer and use it in GitHub Desktop.
assignment 6
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
import json | |
color_data = json.loads(open("xkcd.json").read()) | |
def hex_to_int(s): | |
s = s.lstrip("#") | |
return int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16) | |
import math | |
def distance(coord1, coord2): | |
return math.sqrt(sum([(i - j)**2 for i, j in zip(coord1, coord2)])) | |
distance([10, 1], [5, 2]) | |
5.0990195135927845 | |
def subtractv(coord1, coord2): | |
return [c1 - c2 for c1, c2 in zip(coord1, coord2)] | |
subtractv([10, 1], [5, 2]) | |
[5, -1] | |
def closest(space, coord, n=10): | |
closest = [] | |
for key in sorted(space.keys(), | |
key=lambda x: distance(coord, space[x]))[:n]: | |
closest.append(key) | |
return closest | |
| |
def addv(coord1, coord2): | |
return [c1 + c2 for c1, c2 in zip(coord1, coord2)] | |
addv([10, 1], [5, 2]) | |
[15, 3] | |
def meanv(coords): | |
sumv = [0] * len(coords[0]) | |
for item in coords: | |
for i in range(len(item)): | |
sumv[i] += item[i] | |
mean = [0] * len(sumv) | |
for i in range(len(sumv)): | |
mean[i] = float(sumv[i]) / len(coords) | |
return mean | |
meanv([[0, 1], [2, 2], [4, 3]]) | |
[2.0, 2.0] | |
def closest(space, coord, n=50): | |
closest = [] | |
for key in sorted(space.keys(), | |
key=lambda x: distance(coord, space[x]))[:n]: | |
closest.append(key) | |
return closest | |
colors = dict() | |
for item in color_data['colors']: | |
colors[item["color"]] = hex_to_int(item["hex"]) | |
closest(colors, colors['blue']) | |
['blue', | |
'vibrant blue', | |
'electric blue', | |
'azul', | |
'blue blue', | |
'vivid blue', | |
'bright blue', | |
'cerulean blue', | |
'rich blue', | |
'true blue', | |
'deep sky blue', | |
'sapphire', | |
'pure blue', | |
'ultramarine blue', | |
'strong blue', | |
'light royal blue', | |
'cerulean', | |
'primary blue', | |
'mid blue', | |
'medium blue', | |
'clear blue', | |
'water blue', | |
'bluish', | |
'nice blue', | |
'warm blue', | |
'cobalt blue', | |
'windows blue', | |
'ocean blue', | |
'peacock blue', | |
'royal blue', | |
'indigo blue', | |
'blue with a hint of purple', | |
'cobalt', | |
'blurple', | |
'lightish blue', | |
'ultramarine', | |
'royal', | |
'sea blue', | |
'azure', | |
'blue purple', | |
'cornflower blue', | |
'french blue', | |
'dusk blue', | |
'dodger blue', | |
'flat blue', | |
'darkish blue', | |
'purpley blue', | |
'light navy', | |
'teal blue', | |
'deep sea blue', | |
'ocean', | |
'purpleish blue', | |
'muted blue', | |
'purple/blue', | |
'bluey purple', | |
'denim blue', | |
'purple blue', | |
'violet blue', | |
'blueberry', | |
'dark sky blue', | |
'light navy blue', | |
'iris', | |
'twilight blue', | |
'burple', | |
'ugly blue', | |
'cool blue', | |
'purplish blue', | |
'dark periwinkle', | |
'prussian blue', | |
'dusky blue', | |
'denim', | |
'blue/purple', | |
'purply blue', | |
'dull blue', | |
'blue violet', | |
'dirty blue', | |
'bluish purple', | |
'light indigo', | |
'dark cyan', | |
'deep aqua', | |
'turquoise blue', | |
'twilight', | |
'greenish blue', | |
'cadet blue', | |
'bluegreen', | |
'off blue', | |
'stormy blue', | |
'deep turquoise', | |
'marine blue', | |
'cornflower', | |
'soft blue', | |
'teal', | |
'dark aquamarine', | |
'blue/green', | |
'petrol', | |
'dusty blue', | |
'metallic blue', | |
'dusk', | |
'dark aqua', | |
'indigo'] | |
import spacy | |
nlp = spacy.load('en') | |
| |
doc = nlp(open("scarletletter.txt").read().decode('utf8')) | |
wallpaper_colors = [colors[word.lower_] for word in doc if word.lower_ in colors] | |
avg_color = meanv(wallpaper_colors) | |
closest(colors, avg_color) | |
--------------------------------------------------------------------------- | |
AttributeError Traceback (most recent call last) | |
<ipython-input-23-16ee8b2c468b> in <module>() | |
1 | |
----> 2 doc = nlp(open("scarletletter.txt").read().decode('utf8')) | |
3 wallpaper_colors = [colors[word.lower_] for word in doc if word.lower_ in colors] | |
4 avg_color = meanv(wallpaper_colors) | |
5 closest(colors, avg_color) | |
AttributeError: 'str' object has no attribute 'decode' | |
scarlet= """\The young woman was tall, with a figure of perfect elegance on a | |
large scale. She had dark and abundant hair, so glossy that it | |
threw off the sunshine with a gleam; and a face which, besides | |
being beautiful from regularity of feature and richness of | |
complexion, had the impressiveness belonging to a marked brow | |
and deep black eyes. She was ladylike, too, after the manner of | |
the feminine gentility of those days; characterised by a certain | |
state and dignity, rather than by the delicate, evanescent, and | |
indescribable grace which is now recognised as its indication. | |
And never had Hester Prynne appeared more ladylike, in the | |
antique interpretation of the term, than as she issued from the | |
prison. Those who had before known her, and had expected to | |
behold her dimmed and obscured by a disastrous cloud, were | |
astonished, and even startled, to perceive how her beauty shone | |
out, and made a halo of the misfortune and ignominy in which she | |
was enveloped. It may be true that, to a sensitive observer, | |
there was some thing exquisitely painful in it. Her attire, | |
which indeed, she had wrought for the occasion in prison, and | |
had modelled much after her own fancy, seemed to express the | |
attitude of her spirit, the desperate recklessness of her mood, | |
by its wild and picturesque peculiarity. But the point which | |
drew all eyes, and, as it were, transfigured the wearer--so that | |
both men and women who had been familiarly acquainted with | |
Hester Prynne were now impressed as if they beheld her for the | |
first time--was that SCARLET LETTER, so fantastically | |
embroidered and illuminated upon her bosom. It had the effect of | |
a spell, taking her out of the ordinary relations with humanity, | |
and enclosing her in a sphere by herself. | |
| |
She hath good skill at her needle, that's certain, remarked | |
one of her female spectators; but did ever a woman, before this | |
brazen hussy, contrive such a way of showing it? Why, gossips, | |
what is it but to laugh in the faces of our godly magistrates, | |
and make a pride out of what they, worthy gentlemen, meant for a | |
punishment? | |
| |
It were well, muttered the most iron-visaged of the old dames, | |
if we stripped Madame Hester's rich gown off her dainty | |
shoulders; and as for the red letter which she hath stitched so | |
curiously, I'll bestow a rag of mine own rheumatic flannel to | |
make a fitter one! | |
| |
Oh, peace, neighbours--peace! whispered their youngest | |
companion; do not let her hear you! Not a stitch in that | |
embroidered letter but she has felt it in her heart. | |
| |
The grim beadle now made a gesture with his staff. Make way, | |
good people--make way, in the King's name! cried he. Open a | |
passage; and I promise ye, Mistress Prynne shall be set where | |
man, woman, and child may have a fair sight of her brave apparel | |
from this time till an hour past meridian. A blessing on the | |
righteous colony of the Massachusetts, where iniquity is dragged | |
out into the sunshine! Come along, Madame Hester, and show your | |
scarlet letter in the market-place! | |
""" | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment