<commit_before>#!/usr/bin/env perl -w

use strict;
use warnings;

use common::sense;

use Spreadsheet::ParseXLSX;
use Spreadsheet::ParseExcel;
use Text::Iconv;
use XML::Entities;
use String::CamelCase qw(camelize);
use Time::Piece;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Excel;
use DateTime::Format::Natural;
use DateTime::Format::ISO8601;
use Text::Levenshtein::XS qw(distance);
use List::MoreUtils qw(first_index all);
use Algorithm::Diff qw(traverse_sequences);
use DBI;
use JSON::XS qw(encode_json);

use Carp;
use Getopt::Long;
use Pod::Usage;
use Config::Any;

use Log::Log4perl qw(:easy);
Log::Log4perl->init(\ <<'EOT');
  log4perl.category = DEBUG, Screen
  log4perl.appender.Screen = \
      Log::Log4perl::Appender::ScreenColoredLevels
  log4perl.appender.Screen.layout = \
      Log::Log4perl::Layout::PatternLayout
  log4perl.appender.Screen.layout.ConversionPattern = \
      %p %F{1} %L> %m %n
EOT

my $help = 0;
my $file;
my $study;
my $overwrite;
my $config = 'import.yml';
my $sheet = "Sheet1";
my $database;
my $user;
my $password;

GetOptions(
  'help|?' => \$help,
  'config=s' => \$config,
) or die("Error in command line arguments\n");

if (! -e $config) {
  die("Can't find config file: $config");
}

my $logger = get_logger();
$logger->info("Reading config file: $config");
my $cfg = Config::Any->load_files({files => [$config], use_ext => 1});
$cfg = $cfg->[0];
my ($filename) = keys %$cfg;
$cfg = $cfg->{$filename};

pod2usage(1) if $help;

my $context = {};

foreach my $file (@{$cfg->{files}}) {
  extract($context, $cfg, $file);
}
merge_data($context, $cfg);
write_data($context, $cfg);

sub parse_date {
  my ($cfg, $string) = @_;
  my @formats = (
    '%e/%b/%Y %H:%M',
    '%e-%b-%Y',
    '%e/%b/%Y',
    '%e/%m/%Y',
    '%e-%b-%y',
    '%e/%b/%y',
    '%e %b %Y',
    '%Y-%m-%d'
  );
  push @formats, $cfg->{force_date_format} if (exists($cfg->{force_date_format}));

  $string =~ s{ +}{}g;

  ## Apply some crappy fixes
  $string =~ s{\bsept\b}{sep}i;

  my $date = undef;
  foreach my $format (@formats) {
    eval {
      ## I really hate this!
      local $SIG{__WARN__} = sub { };
      $date = Time::Piece->strptime($string, $format);
    };
    if (defined($date)) {

      if ($date->year() < 1990) {
        $date = undef;
        next;
      }

      ## See if we are almost the same as the original
      my $reformatted = $date->strftime($format);

      if (distance($string, $reformatted) >= 10) {
        $date = undef;
        next;
      }

      return $date->datetime();
    }
  }

  return;
}

sub camelHeader {
  my ($string) = @_;
  $string =~ s{\s+}{ }g;
  $string =~ s{\b(?:the|of|in|to|by|and|for)\b}{}ig;
  $string =~ s{\([^\)]+\)}{}g;
  $string =~ s{\s+$}{};
  $string =~ s{ +}{_}g;
  $string =~ s{\#}{Num}g;
  $string =~ s{[<>/\_:-]}{}g;
  $string =~ s{\?}{}g;
  $string =~ s{\&gt;}{}g;<commit_msg>Fixed date handling for ages. and with spaces in<commit_after>#!/usr/bin/env perl -w

use strict;
use warnings;

use common::sense;

use Spreadsheet::ParseXLSX;
use Spreadsheet::ParseExcel;
use Text::Iconv;
use XML::Entities;
use String::CamelCase qw(camelize);
use Time::Piece;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Excel;
use DateTime::Format::Natural;
use DateTime::Format::ISO8601;
use Text::Levenshtein::XS qw(distance);
use List::MoreUtils qw(first_index all);
use Algorithm::Diff qw(traverse_sequences);
use DBI;
use JSON::XS qw(encode_json);

use Carp;
use Getopt::Long;
use Pod::Usage;
use Config::Any;

use Log::Log4perl qw(:easy);
Log::Log4perl->init(\ <<'EOT');
  log4perl.category = DEBUG, Screen
  log4perl.appender.Screen = \
      Log::Log4perl::Appender::ScreenColoredLevels
  log4perl.appender.Screen.layout = \
      Log::Log4perl::Layout::PatternLayout
  log4perl.appender.Screen.layout.ConversionPattern = \
      %p %F{1} %L> %m %n
EOT

my $help = 0;
my $file;
my $study;
my $overwrite;
my $config = 'import.yml';
my $sheet = "Sheet1";
my $database;
my $user;
my $password;

GetOptions(
  'help|?' => \$help,
  'config=s' => \$config,
) or die("Error in command line arguments\n");

if (! -e $config) {
  die("Can't find config file: $config");
}

my $logger = get_logger();
$logger->info("Reading config file: $config");
my $cfg = Config::Any->load_files({files => [$config], use_ext => 1});
$cfg = $cfg->[0];
my ($filename) = keys %$cfg;
$cfg = $cfg->{$filename};

pod2usage(1) if $help;

my $context = {};

foreach my $file (@{$cfg->{files}}) {
  extract($context, $cfg, $file);
}
merge_data($context, $cfg);
write_data($context, $cfg);

sub parse_date {
  my ($cfg, $string) = @_;
  my @formats = (
    '%e/%b/%Y %H:%M',
    '%e-%b-%Y',
    '%e/%b/%Y',
    '%e/%m/%Y',
    '%e-%b-%y',
    '%e/%b/%y',
    '%e %b %Y',
    '%Y-%m-%d'
  );
  push @formats, $cfg->{force_date_format} if (exists($cfg->{force_date_format}));

  $string =~ s{ +}{ }g;

  ## Apply some crappy fixes
  $string =~ s{\bsept\b}{sep}i;

  my $date = undef;
  foreach my $format (@formats) {
    eval {
      ## I really hate this!
      local $SIG{__WARN__} = sub { };
      $date = Time::Piece->strptime($string, $format);
    };
    if (defined($date)) {

      ## See if we are almost the same as the original
      my $reformatted = $date->strftime($format);

      if (distance($string, $reformatted) >= 10) {
        $date = undef;
        next;
      }

      return $date->datetime();
    }
  }

  return;
}

sub camelHeader {
  my ($string) = @_;
  $string =~ s{\s+}{ }g;
  $string =~ s{\b(?:the|of|in|to|by|and|for)\b}{}ig;
  $string =~ s{\([^\)]+\)}{}g;
  $string =~ s{\s+$}{};
  $string =~ s{ +}{_}g;
  $string =~ s{\#}{Num}g;
  $string =~ s{[<>/\_:-]}{}g;
  $string =~ s{\?}{}g;
  $string =~ s{\&gt;}{}g;
{'fraction_non_alphanumeric': 0.21990902729181247, 'fraction_numerical': 0.008047585724282715, 'mean_word_length': 3.659331703341483, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 40, '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': '3669638', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2240, 'n_words': 513}