<commit_before>module Quandl
class Data
module Babelfisher
  
  extend ActiveSupport::Concern
  
  def headers=(value)
    @headers = value
  end
  def headers
    @headers ||= data_array.respond_to?(:headers) ? data_array.headers : nil
  end
  def headers?
    headers.present?
  end

  protected
  
  def clean(data)
    # Quandl::Data is already clean, but to avoid errors extract internal array
    return data.data_array if data.is_a?(Quandl::Data)
    # Quandl::Babelfish::Data is already clean
    return data if data.is_a?(Quandl::Babelfish::Data)
    # Return empty array if given empty string, nil, etc.
    return [] if data.blank?
    # otherwise parse and clean
    data = CSV.parse( data ) if data.is_a?(String)
    data = Quandl::Babelfish.clean( data )
    data
  end
  
end
end
end<commit_msg>add cleaned? cleaned! for tracking whether or not a Quandl::Data instance has already been cleaned. #clean will short circuit if the data is already cleaned<commit_after>module Quandl
class Data
module Babelfisher
  
  extend ActiveSupport::Concern
  
  def headers=(value)
    @headers = value
  end
  def headers
    @headers ||= data_array.respond_to?(:headers) ? data_array.headers : nil
  end
  def headers?
    headers.present?
  end
  
  def cleaned
    @cleaned
  end
  def cleaned=(value)
    @cleaned = (value == true)
  end
  def cleaned?
    cleaned == true
  end

  protected
  
  def clean(data)
    t1 = Time.now
    # skip cleaning if already clean
    return data if data.kind_of?(Array) && cleaned?
    # Quandl::Data is already clean, but to avoid errors extract internal array
    return data.data_array if data.kind_of?(Quandl::Data)
    # Quandl::Babelfish::Data is already clean
    return data if data.kind_of?(Quandl::Babelfish::Data)
    # Return empty array if given empty string, nil, etc.
    return [] if data.blank?
    # String needs conversion to array
    data = Format.csv_to_array( data ) if data.kind_of?(String)
    # Hash needs conversion to array
    data = Format.hash_to_array( data ) if data.kind_of?(Hash)
    # Array needs cleaning
    data = Quandl::Babelfish.clean( data )
    # mark data as clean
    cleaned!
    # return data
    data
  ensure
    puts "clean #{t1.elapsed_ms} #{data.count}" if t1.elapsed > 0.01
  end

  def cleaned!
    self.cleaned = true
  end
  
end
end
end
{'fraction_non_alphanumeric': 0.0880718255664814, 'fraction_numerical': 0.0025651988029072254, 'mean_word_length': 3.1637010676156585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4798725', 'n_tokens_mistral': 757, 'n_tokens_neox': 753, 'n_words': 242}