<commit_before>#
# Typhoeus encodes arrays as hashes {'0' => v0, '1' => v1, .., 'n' => vN }
#
# To fix this in your Rails server your should:
# in Gemfile:
#     gem 'typhoeus'
#
# in application_controller.rb:
#
#    require 'typhoeus/server_helpers/array_decoder'
#    class ApplicationController < ActionController::Base
#      include Typhoeus::ServerHelpers::ArrayDecoder
#      before_filter :decode_typhoeus_arrays
#    end
#
module Typhoeus
  module ServerHelpers
    module ArrayDecoder

      ##
      # For a cleaner usage in your Rails Controller
      #
      # @example
      #     class ApplicationController
      #       before_filter :decode_typhoeus_arrays
      #       ....
      #
      def decode_typhoeus_arrays
        deep_decode!(params)
      end

      # Recursively decodes Typhoeus encoded arrays in given hash.
      #
      # @param hash [Hash]. This hash will be modified!
      #
      # @author <NAME>
      # @version 0.5.4
      #
      # @return [Hash] decoded array
      def deep_decode!(hash)
        return hash unless hash.is_a?(Hash)
        hash.each_pair do |key,value|
          if value.is_a?(Hash)
            deep_decode!(value)
            hash[key] = value.decode_typhoeus_array
          end
        end
        hash
      end

      def deep_decode(hash)
        deep_decode!(hash.dup)
      end
    end
  end
end

# Add Hash#is_typhoeus_array? and Hash#decode_typhoeus_array methods
class Hash

  # Checks if hash is an Array encoded as a hash.
  # Specifically will check for the hash to have this form: {'0' => v0, '1' => v1, .., 'n' => vN }
  # @return [TrueClass]
  def im_an_array_typhoeus_encoded?
    return false if self.empty?
    self.keys.sort == (0...self.keys.size).map{|i|i.to_s}
  end

  # If the hash is an array encoded by typhoeus an array is returned
  # else the self is returned
  #
  # @see im_an_array_typhoeus_encoded?
  #
  # @return [Array/Hash]
  def decode_typhoeus_array
    if self.im_an_array_typhoeus_encoded?
      Hash[self.sort].values
    else
      self
    end
  end
end<commit_msg>decode arrays encoded as hashes back to arrays without patching Hash<commit_after>#
# Typhoeus encodes arrays as hashes {'0' => v0, '1' => v1, .., 'n' => vN }
#
# To fix this in your Rails server your should:
# in Gemfile:
#     gem 'typhoeus'
#
# in application_controller.rb:
#
#    require 'typhoeus/server_helpers/array_decoder'
#    class ApplicationController < ActionController::Base
#      include Typhoeus::ServerHelpers::ArrayDecoder
#      before_filter :decode_typhoeus_arrays
#    end
#
module Typhoeus
  module ServerHelpers
    module ArrayDecoder

      ##
      # For a cleaner usage in your Rails Controller
      #
      # @example
      #     class ApplicationController
      #       before_filter :decode_typhoeus_arrays
      #       ....
      #
      def decode_typhoeus_arrays
        deep_decode!(params)
      end

      # Recursively decodes Typhoeus encoded arrays in given hash.
      #
      # @param hash [Hash]. This hash will be modified!
      #
      # @author <NAME>
      # @version 0.5.4
      #
      # @return [Hash] decoded array
      def deep_decode!(hash)
        return hash unless hash.is_a?(Hash)
        hash.each_pair do |key,value|
          if value.is_a?(Hash)
            deep_decode!(value)
            hash[key] = decode_typhoeus_array(value)
          end
        end
        hash
      end

      def deep_decode(hash)
        deep_decode!(hash.dup)
      end

      private

      # Checks if hash is an Array encoded as a hash.
      # Specifically will check for the hash to have this form: {'0' => v0, '1' => v1, .., 'n' => vN }
      # @param hash [Hash]
      # @return [TrueClass]
      def is_typhoeus_encoded_array?(hash)
        return false if hash.empty?
        hash.keys.sort == (0...hash.keys.size).map{|i|i.to_s}
      end

      # If the hash is an array encoded by typhoeus an array is returned
      # else the self is returned
      #
      # @see im_an_array_typhoeus_encoded?
      # @param hash [Hash]
      #
      # @return [Array/Hash]
      def decode_typhoeus_array(hash)
        if is_typhoeus_encoded_array?(hash)
          Hash[hash.sort].values
        else
          hash
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09254498714652956, 'fraction_numerical': 0.00560878709978967, 'mean_word_length': 2.215627347858753, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '2548716', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1379, 'n_words': 378}