added in more natural queries

master
gsquire 2013-08-15 13:27:02 -07:00
parent 4a30847621
commit 1fa8219892
2 changed files with 43 additions and 18 deletions

View File

@ -6,12 +6,14 @@ use DDG::Goodie;
# My imports
use strict;
use warnings;
use Lingua::EN::Numbers::Ordinate;
use DateTime;
use Date::Calc qw(:all);
# File metadata
primary_example_queries "week current";
secondary_example_queries "week 8", "week 14 1988";
primary_example_queries "what is the current week";
secondary_example_queries "what was the 5th week of this year",
"what was the 5th week of 1944";
description "find the current week number or when a random week began";
name "Week";
code_url "https://github.com/gsquire/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Week.pm";
@ -20,18 +22,37 @@ topics "everyday", "special_interest";
attribution twitter => "garrettsquire",
github => "gsquire";
triggers start => "week";
triggers start => "what is the current", "what was the";
handle remainder => sub {
my %months = (
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December"
);
my $cur_st = " week of this year"; # For regex options
my $var_st = " week of ";
my $input = $_; # Named variables are better
my $dt = DateTime->now(time_zone => "local");
if ($input eq "current") {
return "We are in week number " . $dt->week_number;
if ($input eq "week") {
return "We are in currently in the " . ordinate($dt->week_number) .
" week of " . $dt->year();
}
elsif ($input =~ /^(\d{1,2})$/) {
elsif ($input =~ /(\d{1,2})(?:rd|nd|st|th)$cur_st/) {
my $year = $dt->year();
my $week_num = $1;
@ -40,11 +61,12 @@ handle remainder => sub {
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
return "Week $week_num started on $month-$day in $year";
return "The " . ordinate($week_num) . " week of $year began on " .
$months{$month} . " " . ordinate($day);
}
elsif ($input =~ /^(\d{1,2})\s+(\d{4})$/) {
elsif ($input =~ /(\d{1,2})(?:rd|nd|st|th)$var_st(\d{1,4})/) {
my $week_num = $1;
my $year = $2;
@ -53,8 +75,8 @@ handle remainder => sub {
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
return "Week $week_num started on $month-$day of $year";
return "The " . ordinate($week_num) . " week of $year began on " .
$months{$month} . " " . ordinate($day);
}
else {
@ -67,5 +89,3 @@ zci is_cached => 1;
zci answer_type => "week";
1;

View File

@ -15,15 +15,20 @@ ddg_goodie_test(
DDG::Goodie::Week
)],
"week current" => test_zci(qr/We are in week number \d+/),
"what is the current week" =>
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
"week 6" => test_zci(qr/Week \d+ started on \d+-\d+ in \d+/),
"what was the 5th week of this year" =>
test_zci(qr/The \d+\w+ week of \d+ began on January \d+\w+/),
"week 43 1984" => test_zci("Week 43 started on 10-22 of 1984"),
"what was the 43rd week of 1984" =>
test_zci("The 43rd week of 1984 began on October 22nd"),
"week 8 1956" => test_zci("Week 8 started on 2-20 of 1956"),
"what was the 8th week of 1956" =>
test_zci("The 8th week of 1956 began on February 20th"),
"week 21 1987" => test_zci("Week 21 started on 5-18 of 1987"),
"what was the 21st week of 1987" =>
test_zci("The 21st week of 1987 began on May 18th"),
);
done_testing;