<commit_before>import Cookies from 'js-cookie'

export function fetch(method, url, req_body) {
  return new Promise((resolve, reject) => {
    const request = new XMLHttpRequest()
    request.open(method, url, true)
    request.setRequestHeader('X-CSRFToken', Cookies.get('csrftoken'))
    request.addEventListener('load', () => resolve(request.responseText))
    request.addEventListener('error', () => reject('Request Error: ', request.response))
    request.send(req_body)
  })
}

export function calculateSimilarity(item_answers, user_answers) {
    let points = 0,
        answers_count = 0,
        answers = user_answers

    // Similarity is 0% if there is no data about item answers
    if (!item_answers)
        return 0

    for (let answer_id in item_answers) {
        if (answers[answer_id] && answers[answer_id].answer) {
            let answer_points = Math.abs((answers[answer_id].answer + Number(item_answers[answer_id])) / 4)
            if (answers[answer_id].important) {
                answers_count += 2
                answer_points *= 2
            } else {
                answers_count++
            }
            points += answer_points
        }
    }

    return Math.round((points / answers_count)*100)
}

export function sortResults(results) {
    // Filter out old/inactive/renamed fractions which have 0 members.
    results.fractions = results.fractions.filter((fraction) => fraction.members_amount > 0)

    // Calculate fractions similarity
    results.fractions = results.fractions.map((fraction) => {
        fraction.similarity = calculateSimilarity(fraction.answers, results.user_answers || results.answers)
        return fraction
    })

    // Filter our not active mps (they don't have fraction)<commit_msg>Filter out MG from results #213<commit_after>import Cookies from 'js-cookie'

export function fetch(method, url, req_body) {
  return new Promise((resolve, reject) => {
    const request = new XMLHttpRequest()
    request.open(method, url, true)
    request.setRequestHeader('X-CSRFToken', Cookies.get('csrftoken'))
    request.addEventListener('load', () => resolve(request.responseText))
    request.addEventListener('error', () => reject('Request Error: ', request.response))
    request.send(req_body)
  })
}

export function calculateSimilarity(item_answers, user_answers) {
    let points = 0,
        answers_count = 0,
        answers = user_answers

    // Similarity is 0% if there is no data about item answers
    if (!item_answers)
        return 0

    for (let answer_id in item_answers) {
        if (answers[answer_id] && answers[answer_id].answer) {
            let answer_points = Math.abs((answers[answer_id].answer + Number(item_answers[answer_id])) / 4)
            if (answers[answer_id].important) {
                answers_count += 2
                answer_points *= 2
            } else {
                answers_count++
            }
            points += answer_points
        }
    }

    return Math.round((points / answers_count)*100)
}

export function sortResults(results) {
    // Filter out old/inactive/renamed fractions which have 0 members.
    results.fractions = results.fractions.filter((fraction) => fraction.members_amount > 0)
    // Filter out MF fraction, we don't want to show it in results
    results.fractions = results.fractions.filter((fraction) => fraction.short_title !== 'MG')

    // Calculate fractions similarity
    results.fractions = results.fractions.map((fraction) => {
        fraction.similarity = calculateSimilarity(fraction.answers, results.user_answers || results.answers)
        return fraction
    })

    // Filter out not active mps (they don't have fraction)
{'fraction_non_alphanumeric': 0.11038784919989152, 'fraction_numerical': 0.007323026851098454, 'mean_word_length': 2.9401709401709404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '7618', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1020, 'n_words': 252}