Skip to content

Instantly share code, notes, and snippets.

@Fulgen301
Created July 31, 2018 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fulgen301/ad0779fc0f3e32c068ea9c58ad3d64ca to your computer and use it in GitHub Desktop.
Save Fulgen301/ad0779fc0f3e32c068ea9c58ad3d64ca to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json, sys
from hashlib import md5
import numpy as np
import matplotlib.pyplot as plot
from collections import defaultdict
vwa = "/home/tokgeo/Downloads/vwa.json"
vwa_options = "/home/tokgeo/Downloads/vwa_options.json"
def loadDump(path : str) -> dict:
with open(path, "r") as fobj:
return json.loads(fobj.read().splitlines()[8])
def getQuestionForHash(hash : str) -> str:
for i in vwa_options:
if md5(i["question"].encode("utf-8")).hexdigest() == hash:
return i["question"]
raise ValueError("No corresponding question found")
vwa = loadDump(vwa)
vwa_options = loadDump(vwa_options)
for i in range(len(vwa_options)):
vwa_options[i] = {"question" : vwa_options[i]["question"], "answers" : vwa_options[i]["allowed_answers"].split("|")}
count = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: 0)))
for i in range(len(vwa)):
answers = []
for block in filter(None, vwa[i]["answers"].split("|")):
question, answer = block.split("::")
question = getQuestionForHash(question)
answer = answer.split(";")
for a in answer:
count[question][a][vwa[i]["age"]] += 1
answers.append({"question" : question, "answer" : answer})
vwa[i]["answers"] = answers
colors = ["b", "r", "g"]
for question, answers in count.items():
fig, ax = plot.subplots()
bar_width = 0.35
index = np.arange(len(range(13, 18 + 1)))
for answer, ages in answers.items():
values = list(ages.values())
print(values)
try:
plot.bar(index, values, bar_width, alpha=1.0, color=colors[list(answers.keys()).index(answer) % len(colors)], label=answer)
except:
continue
plot.xlabel("Alter")
plot.ylabel("Anzahl")
plot.title(question)
plot.xticks(index + bar_width / 2, list(map(str, range(13, 18 + 1))))
plot.legend()
#plot.tight_layout()
plot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment