<commit_before>"""

Defines reusable functions often called from views.py

"""
from datetime import datetime
import json

from oppgavegen.latex_translator import remove_pm_and_add_parenthesis, add_phantom_minus
from oppgavegen.models import Template, Tag
from oppgavegen.view_logic.answer_checker import check_answer
from oppgavegen.generation_folder.calculate_parse_solution import parse_solution, calculate_array, parse_answer
from oppgavegen.generation_folder.fill_in import get_values_from_position
from oppgavegen.utility.utility import after_equal_sign, replace_words, replace_variables_from_array
from oppgavegen.generation_folder.template_validation import template_validation


def make_edit_context_dict(template_id):
    """Returns context dict for use on the edit page"""
    templatetags = []
    q = Template.objects.get(pk=template_id)
    for t in q.tags.all():
        templatetags.append(t.name)
    calculation_references = q.calculation_ref
    question_text = q.question_text_latex
    solution = q.solution_latex
    answer = q.answer_latex
    choices = q.choices_latex
    conditions = q.conditions_latex
    fill_in = q.fill_in_latex
    random_domain = q.random_domain
    unchanged_ref = q.unchanged_ref
    dictionary = q.dictionary
    used_variables = q.used_variables
    tags = json.dumps(templatetags)
    difficulty_m = q.difficulty_multiple
    difficulty_b = q.difficulty_blanks
    name = q.name
    context_dict = {'template_id': template_id, 'answer': answer, 'solution': solution,
                    'question_text': question_text, 'calculation_references': calculation_references,
                    'choices': choices, 'conditions': conditions, 'fill_in': fill_in,
                    'random_domain': random_domain, 'unchanged_ref': unchanged_ref,
                    'dictionary': dictionary, 'used_variables': used_variables,
                    'tags': tags, 'margin_of_error': q.margin_of_error, 'disallowed': q.disallowed,
                    'required': q.required, 'difficulty': q.difficulty, 'difficulty_blanks': difficulty_b,
                    'difficulty_multiple': difficulty_m, 'name': name}
    return context_dict


def make_answer_context_dict(form_values):
    """Returns context dict for use on the answer page"""
    user_answer = form_values['user_answer']
    template_type = form_values['template_type']
    template_specific = form_values['template_specific']
    q = Template.objects.get(pk=form_values['primary_key'])
    variable_dictionary = form_values['variable_dictionary'].split('§')
    replacing_words = form_values['replacing_words']
    random_domain = q.random_domain

    if template_type != 'blanks':
        answer = replace_variables_from_array(variable_dictionary, q.answer.replace('\\\\', '\\'))
        answer = add_phantom_minus(answer)
    else:
        answer = get_values_from_position(template_specific, q.solution.replace('\\\\', '\\'))
        answer = replace_variables_from_array(variable_dictionary, answer)

    original_user_answer = user_answer.replace('§', '\\text{ og }')
    answer = remove_pm_and_add_parenthesis(answer)
    answer = parse_answer(answer, random_domain)
    answer = answer.replace('`', '')
    answer = answer.split('§')
    solution = str(q.question_text.replace('\\\\', '\\')) + "§" + str(q.solution.replace('\\\\', '\\'))
    solution = add_phantom_minus(solution)
    solution = replace_variables_from_array(variable_dictionary, solution)
    solution = remove_pm_and_add_parenthesis(solution)
    solution = parse_solution(solution, random_domain)
    if len(replacing_words) > 0:<commit_msg>replace , with . in user answer.<commit_after>"""

Defines reusable functions often called from views.py

"""
from datetime import datetime
import json

from oppgavegen.latex_translator import remove_pm_and_add_parenthesis, add_phantom_minus
from oppgavegen.models import Template, Tag
from oppgavegen.view_logic.answer_checker import check_answer
from oppgavegen.generation_folder.calculate_parse_solution import parse_solution, calculate_array, parse_answer
from oppgavegen.generation_folder.fill_in import get_values_from_position
from oppgavegen.utility.utility import after_equal_sign, replace_words, replace_variables_from_array
from oppgavegen.generation_folder.template_validation import template_validation


def make_edit_context_dict(template_id):
    """Returns context dict for use on the edit page"""
    templatetags = []
    q = Template.objects.get(pk=template_id)
    for t in q.tags.all():
        templatetags.append(t.name)
    calculation_references = q.calculation_ref
    question_text = q.question_text_latex
    solution = q.solution_latex
    answer = q.answer_latex
    choices = q.choices_latex
    conditions = q.conditions_latex
    fill_in = q.fill_in_latex
    random_domain = q.random_domain
    unchanged_ref = q.unchanged_ref
    dictionary = q.dictionary
    used_variables = q.used_variables
    tags = json.dumps(templatetags)
    difficulty_m = q.difficulty_multiple
    difficulty_b = q.difficulty_blanks
    name = q.name
    context_dict = {'template_id': template_id, 'answer': answer, 'solution': solution,
                    'question_text': question_text, 'calculation_references': calculation_references,
                    'choices': choices, 'conditions': conditions, 'fill_in': fill_in,
                    'random_domain': random_domain, 'unchanged_ref': unchanged_ref,
                    'dictionary': dictionary, 'used_variables': used_variables,
                    'tags': tags, 'margin_of_error': q.margin_of_error, 'disallowed': q.disallowed,
                    'required': q.required, 'difficulty': q.difficulty, 'difficulty_blanks': difficulty_b,
                    'difficulty_multiple': difficulty_m, 'name': name}
    return context_dict


def make_answer_context_dict(form_values):
    """Returns context dict for use on the answer page"""
    user_answer = form_values['user_answer']
    user_answer = user_answer.replace(',','.')
    user_answer = user_answer.replace('..', ',')
    template_type = form_values['template_type']
    template_specific = form_values['template_specific']
    q = Template.objects.get(pk=form_values['primary_key'])
    variable_dictionary = form_values['variable_dictionary'].split('§')
    replacing_words = form_values['replacing_words']
    random_domain = q.random_domain

    if template_type != 'blanks':
        answer = replace_variables_from_array(variable_dictionary, q.answer.replace('\\\\', '\\'))
        answer = add_phantom_minus(answer)
    else:
        answer = get_values_from_position(template_specific, q.solution.replace('\\\\', '\\'))
        answer = replace_variables_from_array(variable_dictionary, answer)

    original_user_answer = user_answer.replace('§', '\\text{ og }')
    answer = remove_pm_and_add_parenthesis(answer)
    answer = parse_answer(answer, random_domain)
    answer = answer.replace('`', '')
    answer = answer.split('§')
    solution = str(q.question_text.replace('\\\\', '\\')) + "§" + str(q.solution.replace('\\\\', '\\'))
    solution = add_phantom_minus(solution)
    solution = replace_variables_from_array(variable_dictionary, solution)
    solution = remove_pm_and_add_parenthesis(solution)
    solution = parse_solution(solution, random_domain)
    if len(replacing_words) > 0:
{'fraction_non_alphanumeric': 0.10469068660774983, 'fraction_numerical': 0.00027192386131883074, 'mean_word_length': 4.176636171710063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6797134', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2152, 'n_words': 427}