| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "Scoring: ((107, 107), (1336, 1336))\n",
- "10000\n",
- "\n",
- "Scoring: ((414, 107), (414, 414))\n",
- "8616\n",
- "\n",
- "Scoring: ((414, 2056), (1648, 721))\n",
- "5738\n",
- "\n",
- "Scoring: ((1648, 721), (107, 414))\n",
- "6173\n",
- "\n",
- "Scoring: ((1336, 1648), (1028, 1648))\n",
- "9924\n",
- "\n",
- "Scoring: ((107, 414), (721, 721))\n",
- "8616\n"
- ]
- }
- ],
- "source": [
- "import json\n",
- "import copy\n",
- "from itertools import product\n",
- "from scipy import spatial\n",
- "\n",
- "config = {\n",
- " \"all_possible_responses\": [\n",
- " 107, 414, 721, 1028, 1336, 1648, 2056\n",
- " ],\n",
- " \"aspect_size\": 2,\n",
- " \"magic\": 10000,\n",
- " \"write\": True,\n",
- " \"file\": \"test.js\",\n",
- " \"delimiter\": \"-\",\n",
- " \"version\": \"0.1.0\"\n",
- "}\n",
- "\n",
- "def createPermutations(possibilities, size):\n",
- " return tuple(product(possibilities, repeat=size))\n",
- "\n",
- "\n",
- "def getAspectFromSurveys(survey_a, survey_b, size):\n",
- " if (survey_a.__len__() < size | survey_b.__len__() < size):\n",
- " raise Exception(\"Surveys must both contain more items than size\")\n",
- "\n",
- " def store(survey, length):\n",
- " col = []\n",
- " for i in range(size):\n",
- " val = survey.pop(0)\n",
- " col.append(val)\n",
- " return col\n",
- "\n",
- " # Take the first <size> elements from the list\n",
- " col_a = tuple(store(survey_a, size))\n",
- " col_b = tuple(store(survey_b, size))\n",
- "\n",
- " if (col_a.__len__() != size | col_b.__len__() != size ):\n",
- " raise Exception(\"No aspect values found in survey\")\n",
- "\n",
- " return (col_a, col_b)\n",
- "\n",
- "\n",
- "def scoreAspect(aspect_ab):\n",
- " a = aspect_ab[0]\n",
- " b = aspect_ab[1]\n",
- " return (1 - spatial.distance.cosine(a,b)) * config[\"magic\"]\n",
- "\n",
- "\n",
- "def prescore_matrix_from(vals):\n",
- " m = {}\n",
- " for val in vals:\n",
- " m[val] = []\n",
- " for other_val in vals:\n",
- " score = scoreAspect((val, other_val))\n",
- " adjusted_score = round(score)\n",
- " m[val].append(adjusted_score)\n",
- " return m\n",
- "\n",
- "\n",
- "def lookup_prescore_in(score_matrix, vals, aspect_ab):\n",
- " print(\"\\nScoring:\", aspect_ab)\n",
- " aspect_a, aspect_b = aspect_ab\n",
- " # Look-up using the index because\n",
- " # \n",
- " pos_b = vals.index(aspect_b)\n",
- " return score_matrix[aspect_a][pos_b]\n",
- "\n",
- "\n",
- "# !: Mutates your input\n",
- "def score_aspect(input_a, input_b, score_matrix, vals):\n",
- " aspect_ab = getAspectFromSurveys(input_a, input_b, config[\"aspect_size\"])\n",
- " return lookup_prescore_in(score_matrix, vals, aspect_ab)\n",
- "\n",
- "\n",
- "def run():\n",
- " # Set the keys for the look-up\n",
- " xy_axis_vals = createPermutations(config[\"all_possible_responses\"], config[\"aspect_size\"])\n",
- " m = prescore_matrix_from(xy_axis_vals)\n",
- "\n",
- " # Example:\n",
- " res = config[\"all_possible_responses\"]\n",
- " input_a = [\n",
- " res[0], res[0], # One aspect\n",
- " res[1], res[0],\n",
- " res[1], res[6],\n",
- " res[5], res[2],\n",
- " res[4], res[5],\n",
- " res[0], res[1],\n",
- " ]\n",
- " input_b = [\n",
- " res[4], res[4], # One aspect\n",
- " res[1], res[1],\n",
- " res[5], res[2],\n",
- " res[0], res[1],\n",
- " res[3], res[5],\n",
- " res[2], res[2],\n",
- " ]\n",
- " for i in range(round(input_a.__len__() / 2)):\n",
- " print(score_aspect(input_a, input_b, m, xy_axis_vals))\n",
- "\n",
- "\n",
- " if(config[\"write\"] == True):\n",
- " # Serializing json\n",
- " str_m = {}\n",
- " for key, value in m.items():\n",
- " delimiter = config[\"delimiter\"]\n",
- " str_key = delimiter.join([str(v) for v in key])\n",
- " str_m[str_key] = value\n",
- " str_m[\"_config\"] = config\n",
- " json_object = json.dumps(str_m, indent = 4)\n",
- " with open(config[\"file\"], \"w\") as file:\n",
- " # write to file\n",
- " file.write(json_object)\n",
- "run()\n"
- ]
- }
- ],
- "metadata": {
- "interpreter": {
- "hash": "a4118c1262ac97709ca0d199809af279fe9249120a4ac5f6c92359d01f3f0cd0"
- },
- "kernelspec": {
- "display_name": "Python 3.7.10 64-bit ('base': conda)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.7.10"
- },
- "orig_nbformat": 4
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|