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