Guest User

Untitled

a guest
Apr 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. import json
  2.  
  3. color_data = json.loads(open("xkcd.json").read())
  4.  
  5. def hex_to_int(s):
  6. s = s.lstrip("#")
  7. return int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)
  8.  
  9. import math
  10. def distance(coord1, coord2):
  11. return math.sqrt(sum([(i - j)**2 for i, j in zip(coord1, coord2)]))
  12. distance([10, 1], [5, 2])
  13. 5.0990195135927845
  14.  
  15. def subtractv(coord1, coord2):
  16. return [c1 - c2 for c1, c2 in zip(coord1, coord2)]
  17. subtractv([10, 1], [5, 2])
  18. [5, -1]
  19.  
  20. def closest(space, coord, n=10):
  21. closest = []
  22. for key in sorted(space.keys(),
  23. key=lambda x: distance(coord, space[x]))[:n]:
  24. closest.append(key)
  25. return closest
  26.  
  27. def addv(coord1, coord2):
  28. return [c1 + c2 for c1, c2 in zip(coord1, coord2)]
  29. addv([10, 1], [5, 2])
  30. [15, 3]
  31.  
  32. def meanv(coords):
  33. sumv = [0] * len(coords[0])
  34. for item in coords:
  35. for i in range(len(item)):
  36. sumv[i] += item[i]
  37. mean = [0] * len(sumv)
  38. for i in range(len(sumv)):
  39. mean[i] = float(sumv[i]) / len(coords)
  40. return mean
  41. meanv([[0, 1], [2, 2], [4, 3]])
  42. [2.0, 2.0]
  43.  
  44. def closest(space, coord, n=50):
  45. closest = []
  46. for key in sorted(space.keys(),
  47. key=lambda x: distance(coord, space[x]))[:n]:
  48. closest.append(key)
  49. return closest
  50.  
  51. colors = dict()
  52. for item in color_data['colors']:
  53. colors[item["color"]] = hex_to_int(item["hex"])
  54.  
  55. closest(colors, colors['blue'])
  56. ['blue',
  57. 'vibrant blue',
  58. 'electric blue',
  59. 'azul',
  60. 'blue blue',
  61. 'vivid blue',
  62. 'bright blue',
  63. 'cerulean blue',
  64. 'rich blue',
  65. 'true blue',
  66. 'deep sky blue',
  67. 'sapphire',
  68. 'pure blue',
  69. 'ultramarine blue',
  70. 'strong blue',
  71. 'light royal blue',
  72. 'cerulean',
  73. 'primary blue',
  74. 'mid blue',
  75. 'medium blue',
  76. 'clear blue',
  77. 'water blue',
  78. 'bluish',
  79. 'nice blue',
  80. 'warm blue',
  81. 'cobalt blue',
  82. 'windows blue',
  83. 'ocean blue',
  84. 'peacock blue',
  85. 'royal blue',
  86. 'indigo blue',
  87. 'blue with a hint of purple',
  88. 'cobalt',
  89. 'blurple',
  90. 'lightish blue',
  91. 'ultramarine',
  92. 'royal',
  93. 'sea blue',
  94. 'azure',
  95. 'blue purple',
  96. 'cornflower blue',
  97. 'french blue',
  98. 'dusk blue',
  99. 'dodger blue',
  100. 'flat blue',
  101. 'darkish blue',
  102. 'purpley blue',
  103. 'light navy',
  104. 'teal blue',
  105. 'deep sea blue',
  106. 'ocean',
  107. 'purpleish blue',
  108. 'muted blue',
  109. 'purple/blue',
  110. 'bluey purple',
  111. 'denim blue',
  112. 'purple blue',
  113. 'violet blue',
  114. 'blueberry',
  115. 'dark sky blue',
  116. 'light navy blue',
  117. 'iris',
  118. 'twilight blue',
  119. 'burple',
  120. 'ugly blue',
  121. 'cool blue',
  122. 'purplish blue',
  123. 'dark periwinkle',
  124. 'prussian blue',
  125. 'dusky blue',
  126. 'denim',
  127. 'blue/purple',
  128. 'purply blue',
  129. 'dull blue',
  130. 'blue violet',
  131. 'dirty blue',
  132. 'bluish purple',
  133. 'light indigo',
  134. 'dark cyan',
  135. 'deep aqua',
  136. 'turquoise blue',
  137. 'twilight',
  138. 'greenish blue',
  139. 'cadet blue',
  140. 'bluegreen',
  141. 'off blue',
  142. 'stormy blue',
  143. 'deep turquoise',
  144. 'marine blue',
  145. 'cornflower',
  146. 'soft blue',
  147. 'teal',
  148. 'dark aquamarine',
  149. 'blue/green',
  150. 'petrol',
  151. 'dusty blue',
  152. 'metallic blue',
  153. 'dusk',
  154. 'dark aqua',
  155. 'indigo']
  156.  
  157. import spacy
  158. nlp = spacy.load('en')
  159.  
  160. doc = nlp(open("scarletletter.txt").read().decode('utf8'))
  161. wallpaper_colors = [colors[word.lower_] for word in doc if word.lower_ in colors]
  162. avg_color = meanv(wallpaper_colors)
  163. closest(colors, avg_color)
  164. ---------------------------------------------------------------------------
  165. AttributeError Traceback (most recent call last)
  166. <ipython-input-23-16ee8b2c468b> in <module>()
  167. 1
  168. ----> 2 doc = nlp(open("scarletletter.txt").read().decode('utf8'))
  169. 3 wallpaper_colors = [colors[word.lower_] for word in doc if word.lower_ in colors]
  170. 4 avg_color = meanv(wallpaper_colors)
  171. 5 closest(colors, avg_color)
  172.  
  173. AttributeError: 'str' object has no attribute 'decode'
  174.  
  175.  
  176. scarlet= """\The young woman was tall, with a figure of perfect elegance on a
  177. large scale. She had dark and abundant hair, so glossy that it
  178. threw off the sunshine with a gleam; and a face which, besides
  179. being beautiful from regularity of feature and richness of
  180. complexion, had the impressiveness belonging to a marked brow
  181. and deep black eyes. She was ladylike, too, after the manner of
  182. the feminine gentility of those days; characterised by a certain
  183. state and dignity, rather than by the delicate, evanescent, and
  184. indescribable grace which is now recognised as its indication.
  185. And never had Hester Prynne appeared more ladylike, in the
  186. antique interpretation of the term, than as she issued from the
  187. prison. Those who had before known her, and had expected to
  188. behold her dimmed and obscured by a disastrous cloud, were
  189. astonished, and even startled, to perceive how her beauty shone
  190. out, and made a halo of the misfortune and ignominy in which she
  191. was enveloped. It may be true that, to a sensitive observer,
  192. there was some thing exquisitely painful in it. Her attire,
  193. which indeed, she had wrought for the occasion in prison, and
  194. had modelled much after her own fancy, seemed to express the
  195. attitude of her spirit, the desperate recklessness of her mood,
  196. by its wild and picturesque peculiarity. But the point which
  197. drew all eyes, and, as it were, transfigured the wearer--so that
  198. both men and women who had been familiarly acquainted with
  199. Hester Prynne were now impressed as if they beheld her for the
  200. first time--was that SCARLET LETTER, so fantastically
  201. embroidered and illuminated upon her bosom. It had the effect of
  202. a spell, taking her out of the ordinary relations with humanity,
  203. and enclosing her in a sphere by herself.
  204. She hath good skill at her needle, that's certain, remarked
  205. one of her female spectators; but did ever a woman, before this
  206. brazen hussy, contrive such a way of showing it? Why, gossips,
  207. what is it but to laugh in the faces of our godly magistrates,
  208. and make a pride out of what they, worthy gentlemen, meant for a
  209. punishment?
  210. It were well, muttered the most iron-visaged of the old dames,
  211. if we stripped Madame Hester's rich gown off her dainty
  212. shoulders; and as for the red letter which she hath stitched so
  213. curiously, I'll bestow a rag of mine own rheumatic flannel to
  214. make a fitter one!
  215. Oh, peace, neighbours--peace! whispered their youngest
  216. companion; do not let her hear you! Not a stitch in that
  217. embroidered letter but she has felt it in her heart.
  218. The grim beadle now made a gesture with his staff. Make way,
  219. good people--make way, in the King's name! cried he. Open a
  220. passage; and I promise ye, Mistress Prynne shall be set where
  221. man, woman, and child may have a fair sight of her brave apparel
  222. from this time till an hour past meridian. A blessing on the
  223. righteous colony of the Massachusetts, where iniquity is dragged
  224. out into the sunshine! Come along, Madame Hester, and show your
  225. scarlet letter in the market-place!
  226. """
  227.  
Add Comment
Please, Sign In to add comment