forked from arielf/weight-loss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifestyle-csv2vw
executable file
·41 lines (32 loc) · 1.01 KB
/
lifestyle-csv2vw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/perl -w
#
# Generate a VW training-set from our raw data CSV
#
use Scalar::Util qw(looks_like_number);
my $SepPat = qr{(?:\s*,\s*|\s+)};
my $Interactive = -t STDOUT;
my $prev_weight;
while (<>) {
# Skip comments or header-line
next if (/^[#A-Za-z]/);
chomp;
# Windows line endings, just in case...
tr/\015//d;
my ($date, $weight, @factors) = split($SepPat);
next unless ((defined $date) and $date =~ /^\d/);
# Only generate a training set if everything is defined and
# we have a prior day weight to compare to
unless ((defined $weight) and looks_like_number($weight)) {
$weight = '' unless (defined $weight);
if ($Interactive) {
warn "$ARGV:$. weight: '$weight' is not a number - line ignored\n";
}
undef $prev_weight;
next;
}
if ((defined $prev_weight) && scalar(@factors) > 0) {
my $gain = sprintf('%.2f', $weight - $prev_weight);
print "$gain | @factors\n";
}
$prev_weight = $weight;
}