Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

generate.ipynb 5.4KB

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