<commit_before>require 'gradesheet'
require 'audit'
require 'spreadsheet'

class AnswerSummarySpreadsheet < Spreadsheet
  def initialize(assn)
    @sheets = []
    @course = assn.course
    @assn = assn

    sheet = Sheet.new("Answers")
    labels, weight, @users = create_name_columns(@course, sheet)
    sheet.freeze_panes(sheet.header_rows.length + 1, 0)
    @sheets.push(sheet)

    repeats = assn.try(:review_count) || 1
    questions = @assn.flattened_questions
    (1..repeats).each do
      questions.each_with_index do |q, i|
        type = case q["type"]
               when "Numeric"
                 "Number"
               else
                 "String"
               end
        if q["name"]
          labels.push(Cell.new(q["name"]))
        else
          labels.push(Cell.new("Question #{i + 1}"))
        end
        weight.push(Cell.new(q["weight"]))
      end
    end

    subs_for_grading = UsedSub.where(assignment: assn).includes(:submission).to_a
    subs_by_user = subs_for_grading.map{|sfg| [sfg.user_id, sfg.submission]}.to_h
    @users.each_with_index do |u, i|
      sub = subs_by_user[u.id]
      next if sub.nil?
      sub.load_answers_related_files
      sub.answers.each do |_, answers|
        questions.zip(answers).each_with_index do |(q, a), qnum|
          response = case q["type"]
          when "Numeric"
            q["main"].to_f<commit_msg>load answers more uniformly<commit_after>require 'gradesheet'
require 'audit'
require 'spreadsheet'

class AnswerSummarySpreadsheet < Spreadsheet
  def initialize(assn)
    @sheets = []
    @course = assn.course
    @assn = assn

    sheet = Sheet.new("Answers")
    labels, weight, @users = create_name_columns(@course, sheet)
    sheet.freeze_panes(sheet.header_rows.length + 1, 0)
    @sheets.push(sheet)

    repeats = assn.try(:review_count) || 1
    questions = @assn.flattened_questions
    (1..repeats).each do
      questions.each_with_index do |q, i|
        type = case q["type"]
               when "Numeric"
                 "Number"
               else
                 "String"
               end
        if q["name"]
          labels.push(Cell.new(q["name"]))
        else
          labels.push(Cell.new("Question #{i + 1}"))
        end
        weight.push(Cell.new(q["weight"]))
      end
    end

    subs_for_grading = UsedSub.where(assignment: assn).includes(submission: [:upload]).to_a
    subs_by_user = subs_for_grading.map{|sfg| [sfg.user_id, sfg.submission]}.to_h
    @users.each_with_index do |u, i|
      sub = subs_by_user[u.id]
      next if sub.nil?
      answers = YAML.load(File.open(sub.upload.submission_path))
      sub.answers.each do |_, answers|
        questions.zip(answers).each_with_index do |(q, a), qnum|
          response = case q["type"]
          when "Numeric"
            q["main"].to_f
{'fraction_non_alphanumeric': 0.12650602409638553, 'fraction_numerical': 0.003543586109142452, 'mean_word_length': 2.34478672985782, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2480919', 'n_tokens_mistral': 973, 'n_tokens_neox': 946, 'n_words': 182}