Last active
September 2, 2023 10:15
-
-
Save michalskop/a6f8824029d26a09e3146a578259d480 to your computer and use it in GitHub Desktop.
Create testing files for Slovak calculators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Create testing files for calcs.""" | |
# import gspread | |
import json | |
import pandas as pd | |
import random | |
import string | |
path = "calc/" | |
path1 = path + "kalkulacka/" | |
path2 = path + "pre-mladych/" | |
# GSheet | |
# gc = gspread.service_account() | |
# QUESTIONS | |
# sheetkey = "1cCuFQSUgtnbDLnAboms0LHQ4rN2jvBS1rzXDhMdMIW0" | |
# sh = gc.open_by_key(sheetkey) | |
# ws = sh.worksheet('Short50') | |
# df = pd.DataFrame(ws.get_all_records()) | |
csvname = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQnT7FwYMaQ3UlKP7YK_8LUpuhAnDS0HIP30upCF9HZZ50WUTXiLgGakLtYnNLk6BXWIULuQFayzDUX/pub?gid=663763026&single=true&output=csv" | |
df = pd.read_csv(csvname) | |
questions1 = [] | |
questions2 = [] | |
for i, row in df.iterrows(): | |
item = { | |
"id": row['uuid'], | |
"title": row['name'], | |
"statement": row['question'], | |
"detail": row['description'], | |
} | |
if i < 42: | |
questions1.append(item) | |
if i < 25: | |
questions2.append(item) | |
with open(path1 + "questions.json", 'w') as f: | |
json.dump(questions1, f, ensure_ascii=False, indent=2) | |
with open(path2 + "questions.json", 'w') as f: | |
json.dump(questions2, f, ensure_ascii=False, indent=2) | |
# ORGANIZATIONS | |
# sheetkey = "1qnlOFq7_JPIbrcVNHRTQRYobQ1usylebrn7VbCJUExw" | |
# sh = gc.open_by_key(sheetkey) | |
# ws = sh.worksheet('Sheet1') | |
# df = pd.DataFrame(ws.get_all_records()) | |
csvname = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQcDXINYp4p5v974JR1p2hrX5UWVMavWYPGTee3a_JU-rYzoEO9A3XcVNj7exgJ-t7JiEEJ7uuP0OKe/pub?gid=0&single=true&output=csv" | |
df = pd.read_csv(csvname) | |
organizations = [] | |
for i, row in df.iterrows(): | |
item = { | |
"id": row['uuid'], | |
"name": row['name'], | |
"shortName": row['abbreviation'], | |
"sortName": row['abbreviation'], | |
} | |
organizations.append(item) | |
with open(path1 + "organizations.json", 'w') as f: | |
json.dump(organizations, f, ensure_ascii=False, indent=2) | |
with open(path2 + "organizations.json", 'w') as f: | |
json.dump(organizations, f, ensure_ascii=False, indent=2) | |
# CANDIDATES | |
candidates = [] | |
for i, row in df.iterrows(): | |
item = { | |
"id": row['uuid_candidate'], | |
"reference": { | |
"id": row['uuid'], | |
"type": "organization" | |
} | |
} | |
candidates.append(item) | |
with open(path1 + "candidates.json", 'w') as f: | |
json.dump(candidates, f, ensure_ascii=False, indent=2) | |
with open(path2 + "candidates.json", 'w') as f: | |
json.dump(candidates, f, ensure_ascii=False, indent=2) | |
# ANSWERS | |
# random answers | |
def generate_random_text(num_words): | |
vowels = 'aeiouyäéíóôúyý' | |
words = [] | |
for _ in range(num_words): | |
# Generate alternating consonants and vowels | |
word = [] | |
for i in range(random.randint(4,10)): | |
if i%2 == 0: | |
word.append(random.choice(string.ascii_lowercase)) | |
else: | |
word.append(random.choice(vowels)) | |
words.append(''.join(word)) | |
return ' '.join(words) | |
r2a = { | |
-1: False, | |
0: None, | |
1: True, | |
} | |
answers1 = {} | |
answers2 = {} | |
for c in candidates: | |
i = 0 | |
answers1[c['id']] = [] | |
answers2[c['id']] = [] | |
if random.randint(0, 1) == 0: | |
expert = True | |
else: | |
expert = False | |
for q in questions1: | |
item = { | |
"questionId": q['id'], | |
"answer": r2a[random.randint(-1, 1)], | |
} | |
# add random text | |
if (random.randint(0, 5) == 0) | expert: | |
item['comment'] = generate_random_text(random.randint(10, 40)) | |
if expert: | |
item['expert'] = expert | |
answers1[c['id']].append(item) | |
if i < 25: | |
answers2[c['id']].append(item) | |
i += 1 | |
with open(path1 + "candidates-answers.json", 'w') as f: | |
json.dump(answers1, f, ensure_ascii=False, indent=2) | |
with open(path2 + "candidates-answers.json", 'w') as f: | |
json.dump(answers2, f, ensure_ascii=False, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment