"""Download the dictionary and filter those which are 3 letters long and contains only letters up to J, 9th letter of English alphabet. Then we generate numbers from 1 to 9 Then we merge these two into one, and also prepare the decoding. The final csv can be read into a Task item in the DB """ from string import ascii_lowercase import requests import random import csv ########### BLOCK: PARAMS ############################################################## BASE_PATH = './data/' NUM_TASKS = 20 allowed_letters = ascii_lowercase[:10] ############ END OF: PARAMS ############################################################# def prepare_words(): """Download dictionary and select words""" word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain" response = requests.get(word_site) WORDS = response.content.splitlines() let3ws = [i.decode("utf-8") for i in WORDS if len(i) == 3] corrs = [i for i in let3ws if set(i).issubset(set(allowed_letters))] random.shuffle(corrs) with open(BASE_PATH + 'words.txt', 'w') as f: for item in corrs: f.write(f"{item}\n") def make_numbers(): with open(BASE_PATH + 'numbers.csv', 'w') as csvfile: numwriter = csv.writer(csvfile, delimiter=',') for i in range(NUM_TASKS): numwriter.writerow(random.sample(range(0, 10), 10)) def dec(word, digs): """return correct answer for a given word and set of numbers""" assert len(digs) == len(allowed_letters), 'Wrong dick size' task_dict = dict(zip(allowed_letters, digs)) correct_answer = ''.join([task_dict[i] for i in word]) return correct_answer def create_tasks(): """merge numbers and words and create correct answers and write all that into a final file""" with open(BASE_PATH + 'numbers.csv') as f: numbers = list(csv.reader(f))[:NUM_TASKS] with open(BASE_PATH + 'words.txt') as f: words = list(csv.reader(f))[:NUM_TASKS] words = [i[0] for i in words] numswords = zip(words, numbers) answers = [dec(word, d) for word, d in numswords] tasks = zip(answers,words, numbers) with open(BASE_PATH + 'tasks.csv', 'w') as csvfile: numwriter = csv.writer(csvfile, delimiter=',') for i,j,l in tasks: numwriter.writerow([i]+[j] +l) if __name__ == "__main__": create_tasks()