sc: refer to ventral sacs overlord by its common name
[sheet.git] / tools / mkcountries-geonames
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4
5 our $VERSION = '1.04';
6
7 my %cc;  # map of country code to info array
8 %cc = (
9         # transitional reservations
10         an => ["transitionally reserved: Netherlands Antilles", "c-na Xr", "Netherlands Antilles", 'bq cw sx'], # 2060
11         bu => ["transitionally reserved: Burma", "c-as Xr", "Burma", 'mm'], # 2039
12         cs => ["transitionally reserved: Serbia and Montenegro", "c-eu Xr", "Serbia and Montenegro", 'rs me'], # 2056
13         nt => ["transitionally reserved: Neutral Zone", "c-as Xr", "N. Zone", 'iq sa'], # 2043
14         tp => ["transitionally reserved: East Timor", "c-oc Xr", "(East Timor)", 'tl'], # 2052
15         yu => ["transitionally reserved: Yugoslavia", "c-eu Xr", "Yugoslavia", 'cs ba hr mk si'], # 2053
16         zr => ["transitionally reserved: Zaire", "c-af Xr", "Zaire", 'cd'], # 2047
17
18         # exceptional reservations
19         ac => ["exceptionally reserved: Ascension Island", "c-oc Xr", "Ascension Island", 'sh'],
20         cp => ["exceptionally reserved: Clipperton Island", "c-na Xr", "Clipperton Island"],
21         cq => ["exceptionally reserved by gb: Island of Sark", "c-eu Xr", "Island of Sark", 'gg'],
22         dg => ["exceptionally reserved: Diego Garcia", "c-as Xr", "Diego Garcia", 'io'],
23         ea => ["exceptionally reserved: Ceuta and Melilla", "c-af Xr", "Ceuta and Melilla"],
24         eu => ["exceptionally reserved: European Union", "c-eu Xr", "European Union"],
25         ez => ["exceptionally reserved: European OTC derivatives", "c-eu Xr", "Eurozone"],
26         fx => ["exceptionally reserved by fr: Metropolitan France", "c-eu Xr", "Metropolitan France", 'fr'],
27         ic => ["exceptionally reserved: Canary Islands", "c-af Xr", "Canary Islands"],
28         su => ["exceptionally reserved: former USSR", "c-eu Xr", "USSR"],
29         ta => ["exceptionally reserved: Tristan da Cunha", "c-oc Xr", "Tristan da Cunha", 'sh'],
30         uk => ["exceptionally reserved by gb: United Kingdom", "c-eu Xr", "(United Kingdom)", 'gb'],
31         un => ["exceptionally reserved: United Nations", "Xi Xr", "United Nations"],
32
33         # indeterminate reservations
34         dy => ["indeterminately reserved: Benin", "c-af Xr", "(Benin)", 'bj'],
35         ew => ["indeterminately reserved: Estonia", "c-eu Xr", "(Estonia)", 'ee'],
36         fl => ["indeterminately reserved: Liechtenstein", "c-eu Xr", "(Liechtenstein)", 'li'],
37         ja => ["indeterminately reserved: Jamaica", "c-na Xr", "(Jamaica)", 'jm'],
38         lf => ["indeterminately reserved: Libya Fezzan", "c-af Xr", "Fezzan", 'ly'],
39         pi => ["indeterminately reserved: Philippines", "c-as Xr", "(Philippines)", 'ph'],
40         ra => ["indeterminately reserved: Argentina", "c-sa Xr", "(Argentina)", 'ar'],
41         rb => ["indeterminately reserved: Botswana, Bolivia", "c-sa Xr", "(Bots...)", 'bw bo'],
42         rc => ["indeterminately reserved: Republic of China", "c-as Xr", "(RoC)", 'tw'],
43         rh => ["indeterminately reserved: Haiti", "c-na Xr", "(Haiti)", 'ht'],
44         ri => ["indeterminately reserved: Indonesia", "c-as Xr", "(Indonesia)", 'id'],
45         rl => ["indeterminately reserved: Lebanon", "c-as Xr", "(Lebanon)", 'lb'],
46         rm => ["indeterminately reserved: Madagascar", "c-af Xr", "(Madagascar)", 'mg'],
47         rn => ["indeterminately reserved: Niger", "c-af Xr", "(Niger)", 'ne'],
48         rp => ["indeterminately reserved: Philippines", "c-as Xr", "(Philippines)", 'ph'],
49         sf => ["indeterminately reserved: Finland", "c-eu Xr", "(Finland)", 'fi'],
50         wg => ["indeterminately reserved: Grenada", "c-na Xr", "(Grenada)", 'gd'],
51         wl => ["indeterminately reserved: Saint Lucia", "c-na Xr", "(Saint Luc.)", 'lc'],
52         wv => ["indeterminately reserved: Saint Vincent", "c-na Xr", "(Saint Vin.)", 'vc'],
53         yv => ["indeterminately reserved: Venezuela", "c-sa Xr", "(Venezuela)", 've'],
54
55         # WIPO, agreed not to use
56         ap => ["not used: African Regional Industrial Property Organization", "Xi", "ARIPO"], # c-af
57         bx => ["not used: Benelux Office for Intellectual Property", "Xi", "BOIP"], # c-eu
58         ef => ["not used: European Community Patent Convention", "Xi", "CPC"], # c-eu
59         em => ["not used: European Trademark Office", "Xi", "OHIM"], # c-eu
60         ep => ["not used: European Patent Organization", "Xi", "EPOrg"], # c-eu
61         ev => ["not used: Eurasian Patent Organization", "Xi", "EAPO"], # c-as
62         gc => ["not used: Gulf Patent Office", "Xi", "GCCPO"], # c-as
63         ib => ["not used: International Bureau of WIPO", "Xi", "IB"],
64         oa => ["not used: African Intellectual Property Organization", "Xi", "OAPI"], # c-af
65         wo => ["not used: World Intellectual Property Organization", "Xi", "WIPO"],
66
67         # common user-assigned
68         xa => ["user-assigned by Russia: Abkhazia", "c-az Co Xi", "Abkhazia", 'ge'], # also IC alias by CH
69         xo => ["user-assigned by Russia: South Ossetia", "c-az Co Xi", "South Ossetia", 'ge'],
70         xi => ["user-assigned for UK/EU trade: Northern Ireland", "c-eu Co Xi", "Northern Ireland", 'gb'],
71         xn => ["user-assigned by WIPO: Nordic Patent Institute", "c-eu Co Xi", "Nordic", 'dk is no se'],
72         xu => ["user-assigned by WIPO: international org UPOV", "Co Xi", "UPOV"],
73         xv => ["user-assigned by WIPO: Visegrad Patent Institute", "c-eu Co Xi", "Visegrad", 'cz hu pl sk'],
74         xx => ["user-assigned by WIPO: unknown state or other entity", "Co Xi", "other"],
75         xz => ["UN/LOCODE semantics: international waters", "Co Xi", "international"],
76         qm => ["user-assigned by ISRC: United States alternative", "c-na Co Xi", "USA"],
77         qo => ["Unicode semantics: Outlying Oceania", "c-oc Co Xi", "Oceania"],
78         qu => ["Unicode semantics: European Union deprecated reserve", "c-eu Co Xi", "EU", 'eu'],
79         zz => ["Unicode semantics: unknown or invalid territory", "Co Xi","unknown"],
80         oo => ["escape to indicate additional code", "Co Xi", "escape"],
81 );
82
83 while (<>) {
84         /^#/ and next;  # skip comments
85         my ($iso, $name, $cont, $tld) = (split /\t/)[0, 4, 8, 9];
86         my @info = ($name, "c-\L$cont");
87         $info[3] = $tld if $tld =~ s/\A\.// and $tld ne lc $iso;
88         $cc{ lc $iso } //= \@info;
89 }
90
91 $cc{io}->[2] = "Chagos Islands";
92 $cc{um}->[2] = "U.S. isl.";
93
94 for (values %cc) {
95         my $abbr = do {
96                 local $_ = $_->[2] // $_->[0];
97                 s/,.*//;
98                 s/(?<=.)\(.*\)\s*//;
99                 s/ republic\b//gi;
100                 s/ islands?\b//gi;
101                 s/\bthe //gi;
102                 s/ and / & /g and s/(?<=.)[a-z ]+//g;
103                 s/ of / /g;
104                 s/\bsa?int /st /gi;
105                 s/Un\Kited /. /gi;
106                 s/South(?:ern)? /S-/g;
107                 s/North(?:ern)? /N-/g;
108                 s/West(?:ern)? /W-/g;
109                 s/East(?:ern)? /E-/g;
110                 s/New /n./g;
111                 s/(\w)(\w+)-/$1-/g;  # strip most chars preceding dash
112                 s/(\S{4}[b-df-hj-np-tv-xz])((?<!Austr)(?!land)\w{2,})/$1./g;  # abbreviate (at consonant)
113                 $_;
114         };
115         $_->[2] = $abbr if $abbr ne $_->[0];  # short name if different
116 }
117
118 say "# automatically generated by $0";
119 use Data::Dump 'dd';
120 $Data::Dump::INDENT = '';
121 dd \%cc;
122
123 __END__
124
125 =head1 NAME
126
127 mkcountries-geonames - Create Perl include of country info from GeoNames data
128
129 =head1 SYNOPSIS
130
131         curl http://download.geonames.org/export/dump/countryInfo.txt |
132         tools/mkcountryinfo > countries.inc.pl
133