Merge pull request #640 from mwmiller/threat_level_midnight

TwelveOclock: add new instant answer to help with 12 am/pm
master
Zaahir Moolla 2014-10-07 11:48:47 -04:00
commit f9a51459a2
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,63 @@
package DDG::Goodie::TwelveOclock;
# ABSTRACT: Determine whether 12:00 is midnight or noon.
use DDG::Goodie;
zci answer_type => "twelve_oclock";
zci is_cached => 1;
primary_example_queries "is 12:00am noon?", "is 1200pm midnight?";
secondary_example_queries "when is noon?", "when is midnight?";
description "Succinct explanation of what this instant answer does";
name "TwelveOclock";
code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/TwelveOclock.pm";
category "reference";
topics "everyday";
attribution github => ['https://github.com/duckduckgo', 'duckduckgo'];
# Triggers
triggers any => "midnight", "noon";
# 12am; 12:00PM; when is noon?; when is midnight
my $question_re = qr/(?:\b12(?:00|:00)?\s?(?<mer>[ap]m)\b|^when is (?<q>noon|midnight)\??$)/;
my %answers = (
am => 'midnight',
pm => 'noon',
);
%answers = (%answers, reverse %answers); # Point both ways to answer either direction.
# Handle statement
handle query => sub {
my $query = lc shift;
$query =~ s/\.//g; # Strip any dots.
return unless ($query =~ $question_re);
my $included_mer = $+{'mer'};
my $meridian = $included_mer || $answers{$+{'q'}}; # No included meridian implies straight-forward question.
my $to_show = $answers{$meridian};
return unless $to_show;
my $guess_result = '';
if ($included_mer) {
# If they included a meridian with their 12 o'clock, we need to figure out if they were guessing.
my $noon = ($query =~ qr/\bnoon\b/);
my $midnight = ($query =~ qr/\bmidnight\b/);
# It's only a guess if they mention only one or the other.
my $guess = ($noon && !$midnight) ? 'noon' : ($midnight && !$noon) ? 'midnight' : '';
# If they guessed, we need to answer the question they asked.
$guess_result = (!$guess) ? '' : ($guess eq $to_show) ? 'Yes, ' : 'No, ';
}
my $answer = $guess_result . '12:00' . $meridian . ' is ' . $to_show . '.';
return $answer, html => format_html($answer);
};
sub format_html {
my ($answer) = @_;
return "<div class='zci--twelveoclock text--primary'>" . $answer . "</div>";
}
1;

View File

@ -0,0 +1,6 @@
.zci--answer .zci--twelveoclock {
font-size: 1.5em;
font-weight: 300;
padding-top: .25em;
padding-bottom: .25em;
}

41
t/TwelveOclock.t Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "twelve_oclock";
zci is_cached => 1;
my @noon = ('12:00pm is noon.', html => qr/12:00pm is noon./);
my @correct_noon = ('Yes, 12:00pm is noon.', html => qr/Yes, 12:00pm is noon./);
my @wrong_noon = ('No, 12:00pm is noon.', html => qr/No, 12:00pm is noon./);
my @midnight = ('12:00am is midnight.', html => qr/12:00am is midnight./);
my @correct_midnight = ('Yes, 12:00am is midnight.', html => qr/Yes, 12:00am is midnight./);
my @wrong_midnight = ('No, 12:00am is midnight.', html => qr/No, 12:00am is midnight./);
ddg_goodie_test([qw(
DDG::Goodie::TwelveOclock
)
],
'is 1200a.m. noon' => test_zci(@wrong_midnight),
'is 1200pm noon?' => test_zci(@correct_noon),
'is 12:00 am midnight' => test_zci(@correct_midnight),
'is 12:00 pm midnight?' => test_zci(@wrong_noon),
'is 12:00 p.m. midnight?' => test_zci(@wrong_noon),
'is 12:00 AM midnight?' => test_zci(@correct_midnight),
'noon is 12:00 p.m.' => test_zci(@correct_noon),
'midnight is 12 AM' => test_zci(@correct_midnight),
'is 12:00P.M. midnight or noon?' => test_zci(@noon),
'is 12am noon or midnight' => test_zci(@midnight),
'when is midnight' => test_zci(@midnight),
'when is noon?' => test_zci(@noon),
'threat level midnight' => undef,
'12 midnight' => undef,
'midnight movies' => undef,
'when is the midnight showing?' => undef,
'when is noon in Jakarta?' => undef,
);
done_testing;