CallingCodes: use structured answer.

master
Matt Miller 2014-10-14 20:08:39 +02:00
parent 462df71a20
commit c960433d78
2 changed files with 341 additions and 57 deletions

View File

@ -81,23 +81,27 @@ handle remainder => sub {
my ($dialing_code, @countries);
my $in_number;
if ($query =~ /^\+?(\d+)/) {
$in_number = $1;
# $query looks like a phone number. eg +65
($dialing_code, @countries) = number_to_country($1);
}
elsif ($query =~ /^\w+/) {
($dialing_code, @countries) = number_to_country($in_number);
} elsif ($query =~ /^\w+/) {
# $query looks like a country name or country code. eg Brazil or Br
($dialing_code, @countries) = country_to_calling_code($query);
}
return unless $dialing_code && @countries;
my $answer = "+" . $dialing_code;
$answer .= " is the international calling code for ";
$answer .= list2string(@countries);
$answer .= ".";
$dialing_code = '+' . $dialing_code;
my $country_list = list2string(@countries);
return $answer;
return $dialing_code . ' is the international calling code for ' . $country_list . '.',
structured_answer => {
input => [$in_number ? $dialing_code : $country_list],
operation => 'international calling code',
result => ($in_number ? $country_list : $dialing_code),
};
};
# Convert a list of country names to a single human readable English string.

View File

@ -12,55 +12,335 @@ zci is_cached => 1;
my $txt = "is the international calling code for";
ddg_goodie_test(
[qw( DDG::Goodie::CallingCodes )],
# Example queries
"calling code 55" => test_zci("+55 $txt Brazil."),
"dialing code brazil" => test_zci("+55 $txt Brazil."),
"dialing code +55" => test_zci("+55 $txt Brazil."),
"country calling code 55" => test_zci("+55 $txt Brazil."),
# Other working queries.
"calling code for the moon" => undef,
"calling code +3boop" => undef,
"calling code for the sudan" => test_zci("+249 $txt Sudan."),
"calling code for vatican" => test_zci("+379 $txt the Holy See (Vatican City State)."),
"379 calling code vatican" => test_zci("+379 $txt the Holy See (Vatican City State)."),
"vatican calling code 379" => test_zci("+379 $txt the Holy See (Vatican City State)."),
"international calling code brazil" => test_zci("+55 $txt Brazil."),
"calling code 55 Brazil" => test_zci("+55 $txt Brazil."),
"calling code for Brazil +55" => test_zci("+55 $txt Brazil."),
"calling code +55 for Brazil" => test_zci("+55 $txt Brazil."),
"Brazil calling code +55" => test_zci("+55 $txt Brazil."),
"+55 calling code for Brazil" => test_zci("+55 $txt Brazil."),
"calling code 65" => test_zci("+65 $txt Singapore."),
"calling code +65" => test_zci("+65 $txt Singapore."),
"dialing code +65" => test_zci("+65 $txt Singapore."),
"dial-in code +65" => test_zci("+65 $txt Singapore."),
"calling code for Singapore" => test_zci("+65 $txt Singapore."),
"international dial-in codes for Singapore" => test_zci("+65 $txt Singapore."),
"dialing code Singapore" => test_zci("+65 $txt Singapore."),
"dialing code for Singapore" => test_zci("+65 $txt Singapore."),
"dialing code for UK" => test_zci("+44 $txt the United Kingdom."),
"calling code for the UK" => test_zci("+44 $txt the United Kingdom."),
"uk calling code" => test_zci("+44 $txt the United Kingdom."),
"gb calling code" => test_zci("+44 $txt the United Kingdom."),
"calling code for antigua" => test_zci("+1 $txt Antigua and Barbuda."),
"calling code for barbuda" => test_zci("+1 $txt Antigua and Barbuda."),
"calling code for trinidad" => test_zci("+1 $txt Trinidad and Tobago."),
"calling code for tobago" => test_zci("+1 $txt Trinidad and Tobago."),
"dialing code for US" => test_zci("+1 $txt the United States."),
"calling code for america" => test_zci("+1 $txt the United States."),
"calling code +1" => test_zci("+1 $txt Antigua and Barbuda, Anguilla, American Samoa, Barbados, Bermuda, the Bahamas, Canada, Dominica, the Dominican Republic, Grenada, Guam, Jamaica, Saint Lucia, Saint Kitts and Nevis, the Cayman Islands, the Northern Mariana Islands, Montserrat, Puerto Rico, Turks and Caicos Islands, Trinidad and Tobago, the United States, Saint Vincent and the Grenadines, the British Virgin Islands, and the US Virgin Islands."),
"calling code +7" => test_zci("+7 $txt the Russian Federation and Kazakhstan."),
"calling code for russia" => test_zci("+7 $txt the Russian Federation."),
"dial in code to the Russian Federation" => test_zci("+7 $txt the Russian Federation."),
"calling code for south korea" => test_zci("+82 $txt the Republic of Korea."),
"calling code for the republic of korea" => test_zci("+82 $txt the Republic of Korea."),
"calling code for north korea" => test_zci("+850 $txt the Democratic People's Republic of Korea."),
"calling code for the democratic people's republic of korea" => test_zci("+850 $txt the Democratic People's Republic of Korea."),
# Properly negative
'country code for the netherlands' => undef,
'country code for north korea' => undef,
'country code +850' => undef,
[qw( DDG::Goodie::CallingCodes )],
# Example queries
"calling code 55" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
"dialing code brazil" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['Brazil'],
operation => 'international calling code',
result => '+55'
}
),
"dialing code +55" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
"country calling code 55" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
# Other working queries.
"calling code for the sudan" => test_zci(
"+249 $txt Sudan.",
structured_answer => {
input => ['Sudan'],
operation => 'international calling code',
result => '+249'
}
),
"calling code for vatican" => test_zci(
"+379 $txt the Holy See (Vatican City State).",
structured_answer => {
input => ['the Holy See (Vatican City State)'],
operation => 'international calling code',
result => '+379'
}
),
"379 calling code vatican" => test_zci(
"+379 $txt the Holy See (Vatican City State).",
structured_answer => {
input => ['+379'],
operation => 'international calling code',
result => 'the Holy See (Vatican City State)'
}
),
"vatican calling code 379" => test_zci(
"+379 $txt the Holy See (Vatican City State).",
structured_answer => {
input => ['the Holy See (Vatican City State)'],
operation => 'international calling code',
result => '+379'
}
),
"country dial in code brazil" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['Brazil'],
operation => 'international calling code',
result => '+55'
}
),
"calling code 55 Brazil" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
"calling code for Brazil +55" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['Brazil'],
operation => 'international calling code',
result => '+55'
}
),
"calling code +55 for Brazil" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
"Brazil calling code +55" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['Brazil'],
operation => 'international calling code',
result => '+55'
}
),
"+55 calling code for Brazil" => test_zci(
"+55 $txt Brazil.",
structured_answer => {
input => ['+55'],
operation => 'international calling code',
result => 'Brazil'
}
),
"calling code 65" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['+65'],
operation => 'international calling code',
result => 'Singapore'
}
),
"calling code +65" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['+65'],
operation => 'international calling code',
result => 'Singapore'
}
),
"dialing code +65" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['+65'],
operation => 'international calling code',
result => 'Singapore'
}
),
"international dial-in code +65" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['+65'],
operation => 'international calling code',
result => 'Singapore'
}
),
"calling code for Singapore" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['Singapore'],
operation => 'international calling code',
result => '+65'
}
),
"country calling code for Singapore" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['Singapore'],
operation => 'international calling code',
result => '+65'
}
),
"dialing code Singapore" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['Singapore'],
operation => 'international calling code',
result => '+65'
}
),
"dialing code for Singapore" => test_zci(
"+65 $txt Singapore.",
structured_answer => {
input => ['Singapore'],
operation => 'international calling code',
result => '+65'
}
),
"dialing code for UK" => test_zci(
"+44 $txt the United Kingdom.",
structured_answer => {
input => ['the United Kingdom'],
operation => 'international calling code',
result => '+44'
}
),
"calling code for the UK" => test_zci(
"+44 $txt the United Kingdom.",
structured_answer => {
input => ['the United Kingdom'],
operation => 'international calling code',
result => '+44'
}
),
"uk calling code" => test_zci(
"+44 $txt the United Kingdom.",
structured_answer => {
input => ['the United Kingdom'],
operation => 'international calling code',
result => '+44'
}
),
"gb calling code" => test_zci(
"+44 $txt the United Kingdom.",
structured_answer => {
input => ['the United Kingdom'],
operation => 'international calling code',
result => '+44'
}
),
"calling code for antigua" => test_zci(
"+1 $txt Antigua and Barbuda.",
structured_answer => {
input => ['Antigua and Barbuda'],
operation => 'international calling code',
result => '+1'
}
),
"calling code for barbuda" => test_zci(
"+1 $txt Antigua and Barbuda.",
structured_answer => {
input => ['Antigua and Barbuda'],
operation => 'international calling code',
result => '+1'
}
),
"calling code for trinidad" => test_zci(
"+1 $txt Trinidad and Tobago.",
structured_answer => {
input => ['Trinidad and Tobago'],
operation => 'international calling code',
result => '+1'
}
),
"calling code for tobago" => test_zci(
"+1 $txt Trinidad and Tobago.",
structured_answer => {
input => ['Trinidad and Tobago'],
operation => 'international calling code',
result => '+1'
}
),
"dialing code for US" => test_zci(
"+1 $txt the United States.",
structured_answer => {
input => ['the United States'],
operation => 'international calling code',
result => '+1'
}
),
"calling code for america" => test_zci(
"+1 $txt the United States.",
structured_answer => {
input => ['the United States'],
operation => 'international calling code',
result => '+1'
}
),
"dial in code +1" => test_zci(
"+1 $txt Antigua and Barbuda, Anguilla, American Samoa, Barbados, Bermuda, the Bahamas, Canada, Dominica, the Dominican Republic, Grenada, Guam, Jamaica, Saint Lucia, Saint Kitts and Nevis, the Cayman Islands, the Northern Mariana Islands, Montserrat, Puerto Rico, Turks and Caicos Islands, Trinidad and Tobago, the United States, Saint Vincent and the Grenadines, the British Virgin Islands, and the US Virgin Islands.",
structured_answer => {
input => ['+1'],
operation => 'international calling code',
result => qr/^Antigua.* US Virgin Islands$/,
}
),
"calling code +7" => test_zci(
"+7 $txt the Russian Federation and Kazakhstan.",
structured_answer => {
input => ['+7'],
operation => 'international calling code',
result => 'the Russian Federation and Kazakhstan'
}
),
"calling code for russia" => test_zci(
"+7 $txt the Russian Federation.",
structured_answer => {
input => ['the Russian Federation'],
operation => 'international calling code',
result => '+7'
}
),
"dial in code to the Russian Federation" => test_zci(
"+7 $txt the Russian Federation.",
structured_answer => {
input => ['the Russian Federation'],
operation => 'international calling code',
result => '+7'
}
),
"calling code for south korea" => test_zci(
"+82 $txt the Republic of Korea.",
structured_answer => {
input => ['the Republic of Korea'],
operation => 'international calling code',
result => '+82'
}
),
"calling code for the republic of korea" => test_zci(
"+82 $txt the Republic of Korea.",
structured_answer => {
input => ['the Republic of Korea'],
operation => 'international calling code',
result => '+82'
}
),
"calling code for north korea" => test_zci(
"+850 $txt the Democratic People's Republic of Korea.",
structured_answer => {
input => ['the Democratic People\'s Republic of Korea'],
operation => 'international calling code',
result => '+850'
}
),
"calling code for the democratic people's republic of korea" => test_zci(
"+850 $txt the Democratic People's Republic of Korea.",
structured_answer => {
input => ['the Democratic People\'s Republic of Korea'],
operation => 'international calling code',
result => '+850'
}
),
# Properly negative
'country code for the netherlands' => undef,
'country code for north korea' => undef,
'country code +850' => undef,
"calling code for the moon" => undef,
"calling code +3boop" => undef,
);
done_testing;