You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.ipynb 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 6,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "\n",
  13. "Scoring: ((107, 107), (1336, 1336))\n",
  14. "10000\n",
  15. "\n",
  16. "Scoring: ((414, 107), (414, 414))\n",
  17. "8616\n",
  18. "\n",
  19. "Scoring: ((414, 2056), (1648, 721))\n",
  20. "5738\n",
  21. "\n",
  22. "Scoring: ((1648, 721), (107, 414))\n",
  23. "6173\n",
  24. "\n",
  25. "Scoring: ((1336, 1648), (1028, 1648))\n",
  26. "9924\n",
  27. "\n",
  28. "Scoring: ((107, 414), (721, 721))\n",
  29. "8616\n"
  30. ]
  31. }
  32. ],
  33. "source": [
  34. "import json\n",
  35. "import copy\n",
  36. "from itertools import product\n",
  37. "from scipy import spatial\n",
  38. "\n",
  39. "config = {\n",
  40. " \"all_possible_responses\": [\n",
  41. " 107, 414, 721, 1028, 1336, 1648, 2056\n",
  42. " ],\n",
  43. " \"aspect_size\": 2,\n",
  44. " \"magic\": 10000,\n",
  45. " \"write\": True,\n",
  46. " \"file\": \"test.js\",\n",
  47. " \"delimiter\": \"-\",\n",
  48. " \"version\": \"0.1.0\"\n",
  49. "}\n",
  50. "\n",
  51. "def createPermutations(possibilities, size):\n",
  52. " return tuple(product(possibilities, repeat=size))\n",
  53. "\n",
  54. "\n",
  55. "def getAspectFromSurveys(survey_a, survey_b, size):\n",
  56. " if (survey_a.__len__() < size | survey_b.__len__() < size):\n",
  57. " raise Exception(\"Surveys must both contain more items than size\")\n",
  58. "\n",
  59. " def store(survey, length):\n",
  60. " col = []\n",
  61. " for i in range(size):\n",
  62. " val = survey.pop(0)\n",
  63. " col.append(val)\n",
  64. " return col\n",
  65. "\n",
  66. " # Take the first <size> elements from the list\n",
  67. " col_a = tuple(store(survey_a, size))\n",
  68. " col_b = tuple(store(survey_b, size))\n",
  69. "\n",
  70. " if (col_a.__len__() != size | col_b.__len__() != size ):\n",
  71. " raise Exception(\"No aspect values found in survey\")\n",
  72. "\n",
  73. " return (col_a, col_b)\n",
  74. "\n",
  75. "\n",
  76. "def scoreAspect(aspect_ab):\n",
  77. " a = aspect_ab[0]\n",
  78. " b = aspect_ab[1]\n",
  79. " return (1 - spatial.distance.cosine(a,b)) * config[\"magic\"]\n",
  80. "\n",
  81. "\n",
  82. "def prescore_matrix_from(vals):\n",
  83. " m = {}\n",
  84. " for val in vals:\n",
  85. " m[val] = []\n",
  86. " for other_val in vals:\n",
  87. " score = scoreAspect((val, other_val))\n",
  88. " adjusted_score = round(score)\n",
  89. " m[val].append(adjusted_score)\n",
  90. " return m\n",
  91. "\n",
  92. "\n",
  93. "def lookup_prescore_in(score_matrix, vals, aspect_ab):\n",
  94. " print(\"\\nScoring:\", aspect_ab)\n",
  95. " aspect_a, aspect_b = aspect_ab\n",
  96. " # Look-up using the index because\n",
  97. " # \n",
  98. " pos_b = vals.index(aspect_b)\n",
  99. " return score_matrix[aspect_a][pos_b]\n",
  100. "\n",
  101. "\n",
  102. "# !: Mutates your input\n",
  103. "def score_aspect(input_a, input_b, score_matrix, vals):\n",
  104. " aspect_ab = getAspectFromSurveys(input_a, input_b, config[\"aspect_size\"])\n",
  105. " return lookup_prescore_in(score_matrix, vals, aspect_ab)\n",
  106. "\n",
  107. "\n",
  108. "def run():\n",
  109. " # Set the keys for the look-up\n",
  110. " xy_axis_vals = createPermutations(config[\"all_possible_responses\"], config[\"aspect_size\"])\n",
  111. " m = prescore_matrix_from(xy_axis_vals)\n",
  112. "\n",
  113. " # Example:\n",
  114. " res = config[\"all_possible_responses\"]\n",
  115. " input_a = [\n",
  116. " res[0], res[0], # One aspect\n",
  117. " res[1], res[0],\n",
  118. " res[1], res[6],\n",
  119. " res[5], res[2],\n",
  120. " res[4], res[5],\n",
  121. " res[0], res[1],\n",
  122. " ]\n",
  123. " input_b = [\n",
  124. " res[4], res[4], # One aspect\n",
  125. " res[1], res[1],\n",
  126. " res[5], res[2],\n",
  127. " res[0], res[1],\n",
  128. " res[3], res[5],\n",
  129. " res[2], res[2],\n",
  130. " ]\n",
  131. " for i in range(round(input_a.__len__() / 2)):\n",
  132. " print(score_aspect(input_a, input_b, m, xy_axis_vals))\n",
  133. "\n",
  134. "\n",
  135. " if(config[\"write\"] == True):\n",
  136. " # Serializing json\n",
  137. " str_m = {}\n",
  138. " for key, value in m.items():\n",
  139. " delimiter = config[\"delimiter\"]\n",
  140. " str_key = delimiter.join([str(v) for v in key])\n",
  141. " str_m[str_key] = value\n",
  142. " str_m[\"_config\"] = config\n",
  143. " json_object = json.dumps(str_m, indent = 4)\n",
  144. " with open(config[\"file\"], \"w\") as file:\n",
  145. " # write to file\n",
  146. " file.write(json_object)\n",
  147. "run()\n"
  148. ]
  149. }
  150. ],
  151. "metadata": {
  152. "interpreter": {
  153. "hash": "a4118c1262ac97709ca0d199809af279fe9249120a4ac5f6c92359d01f3f0cd0"
  154. },
  155. "kernelspec": {
  156. "display_name": "Python 3.7.10 64-bit ('base': conda)",
  157. "language": "python",
  158. "name": "python3"
  159. },
  160. "language_info": {
  161. "codemirror_mode": {
  162. "name": "ipython",
  163. "version": 3
  164. },
  165. "file_extension": ".py",
  166. "mimetype": "text/x-python",
  167. "name": "python",
  168. "nbconvert_exporter": "python",
  169. "pygments_lexer": "ipython3",
  170. "version": "3.7.10"
  171. },
  172. "orig_nbformat": 4
  173. },
  174. "nbformat": 4,
  175. "nbformat_minor": 2
  176. }