<commit_before>require 'utils'

module FecResults
  class Congress

    # given a year and an optional chamber ('house' or 'senate') and state ('ar', 'az', etc.) 
    # retrieves election results that fit the criteria
    def new(year, options={})
      send("process_#{year}")
    end

    def process_2012(options)
      results = []
      url = FecResults::CONGRESS_URLS['2012']
      t = RemoteTable.new(url, :sheet => "2012 US House & Senate Resuts")
      rows = t.entries
      # optional filtering
      rows = rows.select{|r| r['D'] == options[:chamber]} if options[:chamber]
      rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]

      rows.each do |candidate|
        c = {:year => 2012}
        next if candidate['CANDIDATE NAME (Last)'].blank?
        next if candidate['D'].blank?
        # find the office_type
        if candidate['FEC ID#'].first != 'n'
          c[:chamber] = candidate['FEC ID#'].first
        elsif candidate['D'].first == 'S'
          c[:chamber] = "S"
        else
          c[:chamber] = 'H'
        end
        c[:state] = candidate['STATE ABBREVIATION']
        c[:district] = candidate['D']
        c[:party] = candidate['PARTY']
        c[:incumbent] = candidate['(I)'] == '(I)' ? true : false
        c[:fec_id] = candidate['FEC ID#']
        c[:candidate_first] = candidate['CANDIDATE NAME (First)']
        c[:candidate_last] = candidate['CANDIDATE NAME (Last)']
        c[:candidate_name] = candidate['CANDIDATE NAME']

        c = update_vote_tallies(c, candidate)
        c = update_general_runoff(c, candidate) if c[:state] == 'LA'
        c = update_combined_totals(c, candidate) if ['CT', 'NY', 'SC'].include?(c[:state])

        c[:general_winner] = candidate['GE WINNER INDICATOR'] == "W" ? true : false unless c[:general_pct].nil?
        results << c
      end
      Result.create_congress(results)
    end

    def update_vote_tallies(c, candidate)
      if candidate['PRIMARY VOTES'] == 'Unopposed'
        c[:primary_unopposed] = true
        c[:primary_votes] = nil
        c[:primary_pct] = 100.0
      else
        c[:primary_unopposed] = false
        c[:primary_votes] = candidate['PRIMARY VOTES'].to_i
        c[:primary_pct] = candidate['PRIMARY %'].to_f*100.0
      end

      if candidate['RUNOFF VOTES'].blank?
        c[:runoff_votes] = nil
        c[:runoff_pct] = nil
      else
        c[:runoff_votes] = candidate['RUNOFF VOTES'].to_i
        c[:runoff_pct] = candidate['RUNOFF %'].to_f*100.0
      end

      if candidate['GENERAL VOTES '] == 'Unopposed'
        c[:general_unopposed] = true
        c[:general_votes] = nil
        c[:general_pct] = 100.0
      elsif candidate['GENERAL VOTES '].blank?
        c[:general_unopposed] = false
        c[:general_votes] = nil<commit_msg>put optional filters at the end of processing<commit_after>require 'rubygems'
require 'remote_table'

module FecResults
  class Congress

    def initialize(params={})
      params.each_pair do |k,v|
       instance_variable_set("@#{k}", v)
      end
    end

    # given a year and an optional chamber ('house' or 'senate') and state ('ar', 'az', etc.) 
    # retrieves election results that fit the criteria
    def process(year, options={})
      send("process_#{year.to_s}(#{options})")
    end

    def process_2012(options={})
      results = []
      url = FecResults::CONGRESS_URLS['2012']
      t = RemoteTable.new(url, :sheet => "2012 US House & Senate Resuts")
      rows = t.entries
      rows.each do |candidate|
        c = {:year => 2012}
        next if candidate['CANDIDATE NAME (Last)'].blank?
        next if candidate['D'].blank?
        # find the office_type
        if candidate['FEC ID#'].first != 'n'
          c[:chamber] = candidate['FEC ID#'].first
        elsif candidate['D'].first == 'S'
          c[:chamber] = "S"
        else
          c[:chamber] = 'H'
        end
        c[:state] = candidate['STATE ABBREVIATION']
        c[:district] = candidate['D']
        c[:party] = candidate['PARTY']
        c[:incumbent] = candidate['(I)'] == '(I)' ? true : false
        c[:fec_id] = candidate['FEC ID#']
        c[:candidate_first] = candidate['CANDIDATE NAME (First)']
        c[:candidate_last] = candidate['CANDIDATE NAME (Last)']
        c[:candidate_name] = candidate['CANDIDATE NAME']

        c = update_vote_tallies(c, candidate)
        c = update_general_runoff(c, candidate) if c[:state] == 'LA'
        c = update_combined_totals(c, candidate) if ['CT', 'NY', 'SC'].include?(c[:state])

        c[:general_winner] = candidate['GE WINNER INDICATOR'] == "W" ? true : false unless c[:general_pct].nil?

        results << c
      end
      results = results.select{|r| r[:chamber] == options[:chamber]} if options[:chamber]
      results = results.select{|r| r[:state] == options[:state]} if options[:state]
      Result.create_congress(results)
    end

    def update_vote_tallies(c, candidate)
      if candidate['PRIMARY VOTES'] == 'Unopposed'
        c[:primary_unopposed] = true
        c[:primary_votes] = nil
        c[:primary_pct] = 100.0
      else
        c[:primary_unopposed] = false
        c[:primary_votes] = candidate['PRIMARY VOTES'].to_i
        c[:primary_pct] = candidate['PRIMARY %'].to_f*100.0
      end

      if candidate['RUNOFF VOTES'].blank?
        c[:runoff_votes] = nil
        c[:runoff_pct] = nil
      else
        c[:runoff_votes] = candidate['RUNOFF VOTES'].to_i
        c[:runoff_pct] = candidate['RUNOFF %'].to_f*100.0
      end

      if candidate['GENERAL VOTES '] == 'Unopposed'
        c[:general_unopposed] = true
        c[:general_votes] = nil
        c[:general_pct] = 100.0
      elsif candidate['GENERAL VOTES '].blank?
        c[:general_unopposed] = false
        c[:general_votes] = nil
{'fraction_non_alphanumeric': 0.14987885081343025, 'fraction_numerical': 0.01107649705780547, 'mean_word_length': 2.6926517571884983, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2945168', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1890, 'n_words': 430}