<commit_before>package com.quizdeck.analysis.algorithms.group;

import com.quizdeck.analysis.exceptions.AnalysisClassException;
import com.quizdeck.analysis.exceptions.AnalysisConstructionException;
import com.quizdeck.analysis.exceptions.AnalysisResultsUnavailableException;
import com.quizdeck.analysis.exceptions.InsufficientDataException;
import com.quizdeck.analysis.inputs.GuessArrivalComparator;
import com.quizdeck.analysis.inputs.Question;
import com.quizdeck.analysis.inputs.Response;
import com.quizdeck.analysis.outputs.AnalysisResult;
import com.quizdeck.analysis.outputs.group.GroupNetQuizAccuracyResults;
import com.quizdeck.analysis.outputs.group.NetQuizData;
import com.quizdeck.model.database.CompleteQuiz;
import com.quizdeck.model.database.submission;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Performs accuracy analysis on each quiz in a group and calculates overall accuracy for the entire group
 *
 * Data:    NetQuizData:    numberOfQuestions
 *                          netAccuracy         (Class average)
 *
 * Stat:    "Net Accuracy"  Unweighted average
 *
 * @author Alex
 */
class GroupNetQuizAccuracyAlgorithm extends AbstractGroupAlgorithm {
    protected GroupNetQuizAccuracyAlgorithm(String groupName, List<CompleteQuiz> completedQuizzes) throws InsufficientDataException, AnalysisClassException, AnalysisConstructionException, AnalysisResultsUnavailableException {
        super(groupName, completedQuizzes);

        resultsAvailable = false;
        groupNetQuizAccuracyResults = new GroupNetQuizAccuracyResults();
    }

    @Override
    public boolean performAnalysis() {
        long totNumAvailable = 0, totNumCorrect = 0;
        for(CompleteQuiz completedQuiz : getRawCompletedQuizzes()) {
            Set<String> userNames =  completedQuiz.getSubmissions()
                                                    .stream()
                                                    .map(submission::getUserName)
                                                    .collect(Collectors.toSet());
            int numTotal = completedQuiz.getQuiz().getQuestions().size() * userNames.size();

            int numCorrect = 0;
            for(String userName : userNames) {
                //list of all responses submitted by this participant
                List<Response> responses = completedQuiz.getSubmissions()
                                                        .stream()
                                                        .filter(submission -> submission.getUserName().equals(userName))
                                                        .collect(Collectors.toList());
                for(Question question : completedQuiz.getQuiz().getQuestions()) {
                    //Was this participant's final guess correct?
                    boolean isCorrect = responses.stream()
                                                 .filter(response -> response.getQuestion().equals(question))
                                                 .map(Response::getGuesses)
                                                 .map(guesses -> {
                                                     Collections.sort(guesses, new GuessArrivalComparator(false));
                                                     return guesses.get(0);
                                                 })
                                                 .anyMatch(guess -> guess.getSelection().equals(question.getCorrectAnswer()));
                    //If the guess was correct increment the total number of correct guesses
                    if(isCorrect)
                        numCorrect++;
                }
            }

            totNumAvailable += numTotal;
            totNumCorrect += numCorrect;

            NetQuizData netQuizData = new NetQuizData();
            netQuizData.setNumberOfQuestions(completedQuiz.getQuiz().getQuestions().size());<commit_msg>Weird fix for Travis pull compile error<commit_after>package com.quizdeck.analysis.algorithms.group;

import com.quizdeck.analysis.exceptions.AnalysisClassException;
import com.quizdeck.analysis.exceptions.AnalysisConstructionException;
import com.quizdeck.analysis.exceptions.AnalysisResultsUnavailableException;
import com.quizdeck.analysis.exceptions.InsufficientDataException;
import com.quizdeck.analysis.inputs.GuessArrivalComparator;
import com.quizdeck.analysis.inputs.Question;
import com.quizdeck.analysis.inputs.Response;
import com.quizdeck.analysis.outputs.AnalysisResult;
import com.quizdeck.analysis.outputs.group.GroupNetQuizAccuracyResults;
import com.quizdeck.analysis.outputs.group.NetQuizData;
import com.quizdeck.model.database.CompleteQuiz;
import com.quizdeck.model.database.submission;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Performs accuracy analysis on each quiz in a group and calculates overall accuracy for the entire group
 *
 * Data:    NetQuizData:    numberOfQuestions
 *                          netAccuracy         (Class average)
 *
 * Stat:    "Net Accuracy"  Unweighted average
 *
 * @author Alex
 */
class GroupNetQuizAccuracyAlgorithm extends AbstractGroupAlgorithm {
    protected GroupNetQuizAccuracyAlgorithm(String groupName, List<CompleteQuiz> completedQuizzes) throws InsufficientDataException, AnalysisClassException, AnalysisConstructionException, AnalysisResultsUnavailableException {
        super(groupName, completedQuizzes);

        resultsAvailable = false;
        groupNetQuizAccuracyResults = new GroupNetQuizAccuracyResults();
    }

    @Override
    public boolean performAnalysis() {
        long totNumAvailable = 0, totNumCorrect = 0;
        for(CompleteQuiz completedQuiz : getRawCompletedQuizzes()) {
            Set<String> userNames =  completedQuiz.getSubmissions()
                                                    .stream()
                                                    .map(submission -> submission.getUserName())
                                                    .collect(Collectors.toSet());
            int numTotal = completedQuiz.getQuiz().getQuestions().size() * userNames.size();

            int numCorrect = 0;
            for(String userName : userNames) {
                //list of all responses submitted by this participant
                List<Response> responses = completedQuiz.getSubmissions()
                                                        .stream()
                                                        .filter(submission -> submission.getUserName().equals(userName))
                                                        .collect(Collectors.toList());
                for(Question question : completedQuiz.getQuiz().getQuestions()) {
                    //Was this participant's final guess correct?
                    boolean isCorrect = responses.stream()
                                                 .filter(response -> response.getQuestion().equals(question))
                                                 .map(Response::getGuesses)
                                                 .map(guesses -> {
                                                     Collections.sort(guesses, new GuessArrivalComparator(false));
                                                     return guesses.get(0);
                                                 })
                                                 .anyMatch(guess -> guess.getSelection().equals(question.getCorrectAnswer()));
                    //If the guess was correct increment the total number of correct guesses
                    if(isCorrect)
                        numCorrect++;
                }
            }

            totNumAvailable += numTotal;
            totNumCorrect += numCorrect;

            NetQuizData netQuizData = new NetQuizData();
            netQuizData.setNumberOfQuestions(completedQuiz.getQuiz().getQuestions().size());
{'fraction_non_alphanumeric': 0.07784881534411432, 'fraction_numerical': 0.0010028832894571893, 'mean_word_length': 1.8564267812388113, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7594209', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1711, 'n_words': 354}