2014-01-12 13:13:26 -08:00
|
|
|
package DDG::Goodie::CallingCodes;
|
2014-02-03 03:42:45 -08:00
|
|
|
# ABSTRACT: Matches country names to international calling codes
|
2014-01-12 13:13:26 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2014-01-12 13:13:26 -08:00
|
|
|
use DDG::Goodie;
|
|
|
|
use Locale::Country qw/country2code code2country/;
|
|
|
|
use Telephony::CountryDialingCodes;
|
|
|
|
|
|
|
|
zci answer_type => "calling_codes";
|
2014-09-27 06:42:57 -07:00
|
|
|
zci is_cached => 1;
|
2014-01-12 13:13:26 -08:00
|
|
|
|
|
|
|
name "CallingCodes";
|
|
|
|
description "Matches country names to international calling codes";
|
|
|
|
source "https://en.wikipedia.org/wiki/List_of_country_calling_codes#Alphabetical_listing_by_country_or_region";
|
|
|
|
code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Goodie/CallingCodes.pm";
|
|
|
|
category "geography";
|
|
|
|
topics "travel", "geography";
|
|
|
|
|
2014-10-15 07:01:43 -07:00
|
|
|
primary_example_queries "calling code 55", "dialing code brazil";
|
2014-01-12 13:13:26 -08:00
|
|
|
secondary_example_queries "dialing code +55", "country calling code 55";
|
|
|
|
|
|
|
|
attribution github => ["kablamo", "Eric Johnson"],
|
|
|
|
web => ["http://kablamo.org", "Eric Johnson"];
|
|
|
|
|
2014-10-15 07:01:43 -07:00
|
|
|
my @codewords = qw(code codes);
|
|
|
|
my @descriptors = ('calling', 'dialing', 'dial-in', 'dial in');
|
|
|
|
my @extras = qw(international country);
|
|
|
|
|
|
|
|
my @triggers;
|
|
|
|
foreach my $cw (@codewords) {
|
|
|
|
foreach my $desc (@descriptors) {
|
|
|
|
push @triggers, $desc . ' ' . $cw;
|
|
|
|
foreach my $extra (@extras) {
|
|
|
|
push @triggers, join(' ', $extra, $desc, $cw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
triggers any => @triggers;
|
2014-01-12 13:13:26 -08:00
|
|
|
|
|
|
|
handle remainder => sub {
|
|
|
|
my $query = shift;
|
|
|
|
|
|
|
|
my ($dialing_code, @countries);
|
2014-02-25 18:28:43 -08:00
|
|
|
|
2014-10-14 11:08:39 -07:00
|
|
|
my $in_number;
|
2014-01-15 13:00:48 -08:00
|
|
|
if ($query =~ /^\+?(\d+)/) {
|
2014-10-14 11:08:39 -07:00
|
|
|
$in_number = $1;
|
2014-01-15 12:33:48 -08:00
|
|
|
# $query looks like a phone number. eg +65
|
2014-10-14 11:08:39 -07:00
|
|
|
($dialing_code, @countries) = number_to_country($in_number);
|
|
|
|
} elsif ($query =~ /^\w+/) {
|
2014-01-15 12:33:48 -08:00
|
|
|
# $query looks like a country name or country code. eg Brazil or Br
|
|
|
|
($dialing_code, @countries) = country_to_calling_code($query);
|
2014-01-12 13:13:26 -08:00
|
|
|
}
|
|
|
|
|
2014-11-21 07:16:08 -08:00
|
|
|
return unless $dialing_code && @countries && (defined $countries[0]);
|
2014-01-12 13:13:26 -08:00
|
|
|
|
2014-10-14 11:08:39 -07:00
|
|
|
$dialing_code = '+' . $dialing_code;
|
|
|
|
my $country_list = list2string(@countries);
|
2014-01-12 13:13:26 -08:00
|
|
|
|
2014-10-14 11:08:39 -07:00
|
|
|
return $dialing_code . ' is the international calling code for ' . $country_list . '.',
|
|
|
|
structured_answer => {
|
|
|
|
input => [$in_number ? $dialing_code : $country_list],
|
2015-01-08 23:56:33 -08:00
|
|
|
operation => 'International calling code',
|
2014-10-14 11:08:39 -07:00
|
|
|
result => ($in_number ? $country_list : $dialing_code),
|
|
|
|
};
|
2014-01-12 13:13:26 -08:00
|
|
|
};
|
|
|
|
|
2014-01-15 12:33:48 -08:00
|
|
|
# Convert a list of country names to a single human readable English string.
|
2014-01-12 13:13:26 -08:00
|
|
|
sub list2string {
|
|
|
|
my @countries = @_;
|
|
|
|
my $string;
|
|
|
|
|
2014-03-02 13:27:03 -08:00
|
|
|
$_ =~ s/^The /the / for @countries;
|
|
|
|
|
2014-01-12 13:13:26 -08:00
|
|
|
if (scalar @countries == 1) {
|
|
|
|
$string = $countries[0];
|
|
|
|
}
|
|
|
|
elsif (scalar @countries == 2) {
|
|
|
|
$string = $countries[0] . " and " . $countries[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $last = pop @countries;
|
|
|
|
$string = join ', ', @countries;
|
|
|
|
$string .= ", and " . $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2014-01-15 12:33:48 -08:00
|
|
|
# Deduce the country from a number.
|
|
|
|
sub number_to_country {
|
2014-01-12 13:13:26 -08:00
|
|
|
my $number = shift;
|
|
|
|
|
|
|
|
my $telephony = Telephony::CountryDialingCodes->new;
|
|
|
|
my @country_codes = $telephony->country_codes($number);
|
|
|
|
my $dialing_code = $telephony->extract_dialing_code($number);
|
|
|
|
my @countries = map { code2country($_) } @country_codes;
|
|
|
|
|
|
|
|
return ($dialing_code, @countries);
|
|
|
|
}
|
|
|
|
|
2014-01-15 12:33:48 -08:00
|
|
|
# Deduce the calling code from a country name or country code.
|
|
|
|
sub country_to_calling_code {
|
2014-01-12 13:13:26 -08:00
|
|
|
my $country = shift;
|
|
|
|
|
|
|
|
# clean up
|
2014-01-15 13:00:48 -08:00
|
|
|
$country =~ s/^to\s+//;
|
2014-01-12 13:13:26 -08:00
|
|
|
$country =~ s/^for\s+//;
|
2014-01-29 17:58:47 -08:00
|
|
|
$country =~ s/^in\s+//;
|
2014-01-12 13:13:26 -08:00
|
|
|
$country =~ s/^the\s+//;
|
2014-02-25 18:28:43 -08:00
|
|
|
$country =~ s/\s+\+?\d+$//; # remove trailing phone code. for queries
|
|
|
|
# like 'calling code for brazil +55'
|
2014-01-12 13:13:26 -08:00
|
|
|
|
|
|
|
my $country_code = country2code($country);
|
|
|
|
|
|
|
|
# if we didn't find a country code, maybe $country is a country_code
|
|
|
|
$country_code = $country unless $country_code;
|
|
|
|
|
|
|
|
$country = code2country($country_code);
|
|
|
|
|
|
|
|
my $telephony = Telephony::CountryDialingCodes->new;
|
|
|
|
my $dialing_code = $telephony->dialing_code($country_code);
|
|
|
|
|
|
|
|
return ($dialing_code, $country);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|