<commit_before>require 'peoplefinder'
require 'forwardable'

class Peoplefinder::EmailAddress
  extend Forwardable
  def_delegators :@mail_address, :domain, :address, :local, :to_s

  def initialize(string, valid_login_domains = nil)
    @raw_address = string
    @valid_login_domains = valid_login_domains || default_valid_login_domains
    begin
      @mail_address = Mail::Address.new(string)
    rescue Mail::Field::ParseError
      @parse_error = true
    end
  end

  def valid_domain?
    valid_login_domains.any? do |pattern|
      domain_matches_pattern?(pattern, domain)
    end
  end

  def valid_format?
    return false if @parse_error
    return false unless domain && address == @raw_address
    return false unless domain.match(/^\S+$/)
    domain_dot_elements = domain.split(/\./)
    return false unless domain_dot_elements.size > 1 && domain_dot_elements.all?(&:present?)

    true
  end

  def valid_address?
    valid_format? && valid_domain?
  end

  def inferred_last_name
    capitalise(local.split('.')[(multipart_local? ? 1 : 0)])
  end

  def inferred_first_name
    capitalise(local.split('.')[0]) if multipart_local?
  end

  def multipart_local?
    local.split('.').length > 1
  end

private

  attr_reader :valid_login_domains

  def default_valid_login_domains
    Rails.configuration.try(:valid_login_domains) || []
  end

  def capitalise(word)
    word.downcase.to_s.gsub(/\b('?\S)/u) do
      (Regexp.last_match[1]).upcase
    end
  end

  def domain_matches_pattern?(pattern, domain)
    case pattern
    when String
      domain == pattern
    when Regexp
      domain =~ pattern
    end
  end
end
<commit_msg>Remove unnecessary begin/end<commit_after>require 'peoplefinder'
require 'forwardable'

class Peoplefinder::EmailAddress
  extend Forwardable
  def_delegators :@mail_address, :domain, :address, :local, :to_s

  def initialize(string, valid_login_domains = nil)
    @raw_address = string
    @valid_login_domains = valid_login_domains || default_valid_login_domains
    @mail_address = Mail::Address.new(string)
  rescue Mail::Field::ParseError
    @parse_error = true
  end

  def valid_domain?
    valid_login_domains.any? do |pattern|
      domain_matches_pattern?(pattern, domain)
    end
  end

  def valid_format?
    return false if @parse_error
    return false unless domain && address == @raw_address
    return false unless domain.match(/^\S+$/)
    domain_dot_elements = domain.split(/\./)
    return false unless domain_dot_elements.size > 1 && domain_dot_elements.all?(&:present?)

    true
  end

  def valid_address?
    valid_format? && valid_domain?
  end

  def inferred_last_name
    capitalise(local.split('.')[(multipart_local? ? 1 : 0)])
  end

  def inferred_first_name
    capitalise(local.split('.')[0]) if multipart_local?
  end

  def multipart_local?
    local.split('.').length > 1
  end

private

  attr_reader :valid_login_domains

  def default_valid_login_domains
    Rails.configuration.try(:valid_login_domains) || []
  end

  def capitalise(word)
    word.downcase.to_s.gsub(/\b('?\S)/u) do
      (Regexp.last_match[1]).upcase
    end
  end

  def domain_matches_pattern?(pattern, domain)
    case pattern
    when String
      domain == pattern
    when Regexp
      domain =~ pattern
    end
  end
end

{'fraction_non_alphanumeric': 0.10063849194283978, 'fraction_numerical': 0.0036485253876558225, 'mean_word_length': 3.6338028169014085, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6874752', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1048, 'n_words': 238}