loosen regex white space, add periods and <sup>s

master
Dylan Lloyd 2013-08-18 23:17:59 -04:00
parent c3eae2a012
commit bd8c08886b
2 changed files with 40 additions and 31 deletions

View File

@ -6,7 +6,7 @@ use DDG::Goodie;
# My imports
use strict;
use warnings;
use Lingua::EN::Numbers::Ordinate;
use Lingua::EN::Numbers::Ordinate qw/ordinate ordsuf/;
use DateTime;
use Date::Calc qw(:all);
@ -43,38 +43,40 @@ my @months = qw/
/;
handle query_raw => sub {
return unless /
what(?:'?s|\sis|\swas)\s
the\s
(current|(\d{1,2})(?:nd|th|rd|st)?)\s
return unless /^\s*
what(?:'?s|\sis|\swas)\s+
the\s+
(current|(\d{1,2})(?:nd|th|rd|st)?)\s+
week
(
\sof\s
(?:(?:the|this)\s)?
\s+of\s+
(?:(?:the|this)\s+)?
(year|\d{4})
)?\??
/x;
\s*$/x;
my $week = $1;
my $year = defined $4 ? ($4 eq 'year' ? 'current' : $4) : 'current';
return if $week =~ s/(nd|th|rd|st)$// and $week > 52;
my $dt = DateTime->now(time_zone => $loc->time_zone);
my $dt = DateTime->now(time_zone => $loc->time_zone)
if ($week eq 'current' or $year eq 'current');
if ($week eq 'current' and $year eq 'current') {
return "We are in currently in the " . ordinate($dt->week_number) .
" week of $dt->year";
' week of ' . $dt->year . '.',
html => 'We are in currently in the ' . $dt->week_number
. '<sup>' . ordsuf($dt->week_number) . '</sup>'
. ' week of ' . $dt->year . '.';
} elsif ($year eq 'current') {
$year = $dt->year();
my (undef, $month, $day) = Monday_of_Week($week, $year);
return "The " . ordinate($week) . " week of $year began on " .
"$months[--$month] " . ordinate($day);
} else {
my (undef, $month, $day) = Monday_of_Week($week, $year);
return "The " . ordinate($week) . " week of $year began on " .
"$months[--$month] " . ordinate($day);
}
my (undef, $month, $day) = Monday_of_Week($week, $year);
return "The " . ordinate($week) . " week of $year began on " .
"$months[--$month] " . ordinate($day) . '.',
html =>"The $week<sup>" . ordsuf($week) . "</sup> week of $year began on " .
"$months[$month] $day<sup>" . ordsuf($day) . '</sup>.';
};
1;

View File

@ -15,26 +15,33 @@ ddg_goodie_test(
DDG::Goodie::Week
)],
"what is the current week" =>
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
"what is the current week" => test_zci(
qr/We are in currently in the \d+\w+ week of \d+\./,
html => qr:We are in currently in the \d+<sup>\w+</sup> week of \d+\.:),
"what's the current week?" =>
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
"what's the current week? " => test_zci(
qr/We are in currently in the \d+\w+ week of \d+\./,
html => qr:We are in currently in the \d+<sup>\w+</sup> week of \d+\.:),
"whats the current week of the year" =>
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
"whats the current week of the year" => test_zci(
qr/We are in currently in the \d+\w+ week of \d+\./,
html => qr:We are in currently in the \d+<sup>\w+</sup> week of \d+\.:),
"what was the 5th week of this year" =>
test_zci(qr/The \d+\w+ week of \d+ began on January \d+\w+/),
"what was the 5th week of this year" => test_zci(
qr/The \d+\w+ week of \d+ began on January \d+\w+\./,
html => qr:The \d+<sup>\w+</sup> week of \d+ began on January \d+<sup>\w+</sup>\.:),
"what was the 43rd week of 1984" =>
test_zci("The 43rd week of 1984 began on October 22nd"),
"what was the 43rd week of 1984" => test_zci(
"The 43rd week of 1984 began on October 22nd.",
html => "The 43<sup>rd</sup> week of 1984 began on October 22<sup>nd</sup>."),
"what was the 8th week of 1956" =>
test_zci("The 8th week of 1956 began on February 20th"),
"what was the 8th week of 1956" => test_zci(
"The 8th week of 1956 began on February 20th.",
html => "The 8<sup>th</sup> week of 1956 began on February 20<sup>th</sup>."),
"what was the 21st week of 1987" =>
test_zci("The 21st week of 1987 began on May 18th"),
"what was the 21st week of 1987" => test_zci(
"The 21st week of 1987 began on May 18th.",
html => "The 21<sup>st</sup> week of 1987 began on May 18<sup>th</sup>."),
);
done_testing;