| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "Scoring: ((1, 1), (7, 7))\n",
- "1515\n",
- "\n",
- "Scoring: ((2, 1), (2, 2))\n",
- "9000\n",
- "\n",
- "Scoring: ((2, 7), (6, 3))\n",
- "4343\n",
- "\n",
- "Scoring: ((6, 3), (1, 2))\n",
- "4901\n",
- "\n",
- "Scoring: ((5, 6), (4, 6))\n",
- "9000\n",
- "\n",
- "Scoring: ((1, 2), (3, 3))\n",
- "7764\n",
- "\n",
- "Scoring: ((1, 1), (1, 1))\n",
- "10000\n"
- ]
- }
- ],
- "source": [
- "import json\n",
- "import copy\n",
- "from itertools import product\n",
- "from scipy import spatial\n",
- "import math\n",
- "import numpy\n",
- "\n",
- " \n",
- "f = open('./config.json')\n",
- "from_json_config = json.load(f)\n",
- "config = {}\n",
- "config[\"all_possible_responses\"] = from_json_config[\"scoreVals\"]\n",
- "config[\"magic\"] = from_json_config[\"magic\"]\n",
- "config[\"aspect_size\"] = 2\n",
- "config[\"write\"]= True\n",
- "config[\"file\"] = \"../generated/prescore_matrix.json\"\n",
- "config[\"delimiter\"] = \"-\"\n",
- "config[\"version\"] = \"0.1.0\"\n",
- "# max distance = distance between top left and bottom right indices in matrix\n",
- "config[\"max_euclidean_distance\"] = math.sqrt(\n",
- " math.pow(len(config[\"all_possible_responses\"])-1, 2) +\n",
- " math.pow(len(config[\"all_possible_responses\"])-1, 2)\n",
- " ) \n",
- "f.close()\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",
- "def euclideanDistance(aspect_ab):\n",
- " a = numpy.array(aspect_ab[0])\n",
- " b = numpy.array(aspect_ab[1])\n",
- " return numpy.linalg.norm(a - b)\n",
- "\n",
- "def prescore_matrix_from(vals):\n",
- " m = {}\n",
- " for val in vals:\n",
- " m[val] = []\n",
- " for other_val in vals:\n",
- " distance = euclideanDistance((val,other_val))\n",
- " score = (config[\"max_euclidean_distance\"] - distance) * config[\"magic\"] + 1515\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",
- " res[0], res[0],\n",
- " ]\n",
- " input_b = [\n",
- " res[6], res[6], # 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",
- " res[0], res[0],\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, values in m.items():\n",
- " delimiter = config[\"delimiter\"]\n",
- " str_key = delimiter.join([str(v) for v in key])\n",
- " str_m[str_key] = [int(val) for val in values]\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": {
- "kernelspec": {
- "display_name": "Python 3.10.6 64-bit",
- "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.10.6"
- },
- "orig_nbformat": 4,
- "vscode": {
- "interpreter": {
- "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
- }
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|