Result logic altered and leap counter improved

master
ettienne 2015-07-17 12:49:50 +00:00
parent 0c47a9b026
commit 643f569ed8
1 changed files with 34 additions and 36 deletions

View File

@ -46,6 +46,7 @@ sub search_leaps {
$cyear += $direction;
}
push @years, ($cyear);
$direction *= 4 if abs($direction) == 1;
$cyear += $direction;
}
return @years;
@ -74,28 +75,43 @@ sub format_year {
return "$cyear";
}
}
# formats the result that should be returned
sub format_result {
my ($plaintext, $title, $subtitle) = @_;
return $plaintext,
structured_answer => {
id => "leap_year",
name => "Answer",
data => {
title => $title || $plaintext,
subtitle => $subtitle
},
templates => {
group => "text",
moreAt => 0
}
}
}
handle remainder => sub {
my $year = (localtime)[5] + 1900;
my ($plaintext, $title, $subtitle);
my @result;
if ($_ =~ /(last|previous) ([0-9][0-9]?)$/i) {
my @years = search_leaps($2, -1, 0, $year);
@years = map(format_year, @years);
my $pretty_years = join(', ', @years);
$plaintext = "The last $2 leap years were $pretty_years";
$title = $pretty_years;
$subtitle = "The last $2 leap years";
@result = format_result("The last $2 leap years were $pretty_years", $pretty_years, "The last $2 leap years");
} elsif ($_ =~ /(next|future) ([0-9][0-9]?)$/i) {
my @years = search_leaps($2, 1, 0, $year);
@years = map(format_year, @years);
my $pretty_years = join(', ', @years);
$plaintext = "The $1 $2 leap years will be $pretty_years";
$title = $pretty_years;
$subtitle = "The $1 $2 leap years";
@result = format_result("The $1 $2 leap years will be $pretty_years", $pretty_years, "The $1 $2 leap years");
} elsif ($_ =~ /^(after|before) ([0-9]+) ?(ad|bce|bc|ce)?$/) {
my $cyear = $2;
@ -112,33 +128,27 @@ handle remainder => sub {
my $pretty_years = join(', ', @years);
my $pretty_year = format_year($cyear);
$plaintext = "The 5 leap years $direction $pretty_year are $pretty_years";
$title = $pretty_years;
$subtitle = "The 5 leap years $direction $pretty_year";
@result = format_result("The 5 leap years $direction $pretty_year are $pretty_years",
$pretty_years,
"The 5 leap years $direction $pretty_year");
} elsif ($_ =~ /(next|future|upcoming)$/i) {
my ($nyear) = search_leaps(1, 1, 0, $year);
$nyear = format_year($nyear);
$plaintext = "$nyear will be the $1 leap year";
$title = $nyear;
$subtitle = "The $1 leap year";
@result = format_result("$nyear will be the $1 leap year", $nyear, "The $1 leap year");
} elsif ($_ =~ /(latest|last|previous)$/i) {
my ($pyear) = search_leaps(1, -1, 0, $year);
$pyear = format_year($pyear);
$plaintext = "$pyear was the $1 leap year";
$title = $pyear;
$subtitle = "The $1 leap year";
@result = format_result("$pyear was the $1 leap year", $pyear, "The $1 leap year");
} elsif ($_ =~ /(most recent)$/i) {
my ($ryear) = search_leaps(1, -1, 1, $year);
$ryear = format_year($ryear);
$plaintext = "$ryear is the $1 leap year";
$title = $ryear;
$subtitle = "The $1 leap year";
@result = format_result("$ryear is the $1 leap year", $ryear, "The $1 leap year");
} elsif($_ =~ /^(was|is|will) ([0-9]+) ?(ad|bce|bc|ce)?( be)? a$/i) {
my $cyear = $2;
@ -149,39 +159,27 @@ handle remainder => sub {
my $tense = find_tense($cyear, $year);
if(isleap($cyear)) {
$plaintext = "Yes! $fyear $is_tense{$tense} a leap year";
@result = format_result("Yes! $fyear $is_tense{$tense} a leap year");
} else {
$plaintext = "No. $fyear $is_not_tense{$tense} a leap year"
@result = format_result("No. $fyear $is_not_tense{$tense} a leap year");
}
} elsif($_ =~ /^is it( now | currently)? a|are we in a$/i) {
my $fyear = format_year($year);
if(isleap($year)) {
$plaintext = "Yes! $fyear is a leap year";
@result = format_result("Yes! $fyear is a leap year");
} else {
$plaintext = "No. $fyear is not a leap year",
@result = format_result("No. $fyear is not a leap year"),
}
}
return $plaintext,
structured_answer => {
id => "leap_year",
name => "Answer",
data => {
title => $title || $plaintext,
subtitle => $subtitle
},
templates => {
group => "text",
moreAt => 0
}
}
return @result;
};
1;