| 1 | package Wiktionary::Parser::Language; | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # map language name to language code | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | ▶ | use Locale::Codes::Language qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use Locale::Codes::Constants; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use Text::Unidecode qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | my $NAME_MAP; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 99 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 17 | 99 | my $class = shift; | |||||||||||||||||||
| 18 | 99 | my %args = @_; | |||||||||||||||||||
| 19 | 99 | my $self = bless \%args, $class; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 99 | $self->_build_name_map(); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 99 | return $self; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 99 | ▶ | sub _build_name_map { | ||||||||||||||||||
|
Subroutine
_build_name_map: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 27 | 99 | my $self = shift; | |||||||||||||||||||
| 28 | 99 | ▶ | return if $NAME_MAP; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # other language name variations used on wiktionary. | ||||||||||||||||||||
| 31 | # mapped to language names used in Locale::Codes::Language | ||||||||||||||||||||
| 32 | 2 | my %name_map = ( | |||||||||||||||||||
| 33 | 'min nan' => 'Min Nan Chinese', | ||||||||||||||||||||
| 34 | 'slovene' => 'Slovenian', | ||||||||||||||||||||
| 35 | 'rapa nui' => 'Rapanui', | ||||||||||||||||||||
| 36 | 'acholi' => 'Acoli', | ||||||||||||||||||||
| 37 | 'west frisian' => 'Western Frisian', | ||||||||||||||||||||
| 38 | 'romansch' => 'romansh', | ||||||||||||||||||||
| 39 | 'romanche' => 'romansh', | ||||||||||||||||||||
| 40 | 'mandarin' => 'Mandarin Chinese', | ||||||||||||||||||||
| 41 | 'ojibwe' => 'Ojibwa', | ||||||||||||||||||||
| 42 | 'khmer' => 'Central Khmer', | ||||||||||||||||||||
| 43 | 'tuvan' => 'Tuvinian', | ||||||||||||||||||||
| 44 | 'sotho' => 'Southern Sotho', | ||||||||||||||||||||
| 45 | 'buryat' => 'Buriat', | ||||||||||||||||||||
| 46 | 'azeri' => 'Azerbaijani', | ||||||||||||||||||||
| 47 | 'taos' => 'Northern Tiwa', | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | my @all_names = Locale::Codes::Language::all_language_names(); | |||||||||||||||||||
| 51 | 2 | my @all_names_3 = Locale::Codes::Language::all_language_names(Locale::Codes::Constants::LOCALE_LANG_ALPHA_3); | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 2 | for my $name (@all_names,@all_names_3) { | |||||||||||||||||||
| 54 | 16454 | ▶ | next unless $name =~ m/\(/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 584 | my $sanitized_name = lc $name; | |||||||||||||||||||
| 56 | 584 | $sanitized_name =~ s/\s*\(.+$//; | |||||||||||||||||||
| 57 | 584 | $name_map{$sanitized_name} = $name; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | $NAME_MAP = \%name_map; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # languages without ISO codes used on wiktionary. | ||||||||||||||||||||
| 64 | # codes here either come from wiktionary itself, or linguist list | ||||||||||||||||||||
| 65 | 5 | ▶ | sub custom_codes { | ||||||||||||||||||
|
Subroutine
custom_codes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 66 | # http://en.wikipedia.org/wiki/Category:Languages_without_ISO_639-3_code_but_with_Linguist_List_code | ||||||||||||||||||||
| 67 | return { | ||||||||||||||||||||
| 68 | 5 | 'jerriais' => 'roa-jer', # roa-jer in wiktionary, fra-jer in linguist list... | |||||||||||||||||||
| 69 | 'tarantino' => 'roa-tar', | ||||||||||||||||||||
| 70 | 'elfdalian' => 'qer', | ||||||||||||||||||||
| 71 | 'cyrillic' => 'cyrl', # ISO 15924 | ||||||||||||||||||||
| 72 | 'translingual' => 'translingual', | ||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 124 | ▶ | sub get_sanitized_language_name { | ||||||||||||||||||
|
Subroutine
get_sanitized_language_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | 124 | my $self = shift; | |||||||||||||||||||
| 78 | 124 | my $name = lc shift; | |||||||||||||||||||
| 79 | 124 | my $unidecoded_name = Text::Unidecode::unidecode($name); | |||||||||||||||||||
| 80 | 124 | return $unidecoded_name; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # return the code for the given language | ||||||||||||||||||||
| 84 | 124 | ▶ | sub language2code { | ||||||||||||||||||
|
Subroutine
language2code: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 85 | 124 | my $self = shift; | |||||||||||||||||||
| 86 | 124 | my $language_name = $self->get_sanitized_language_name(shift); | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 124 | my $code = Locale::Codes::Language::language2code( | |||||||||||||||||||
| 89 | $language_name, | ||||||||||||||||||||
| 90 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_2, | ||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 124 | ▶ | return $code if $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | $code = Locale::Codes::Language::language2code( | ||||||||||||||||||||
| 96 | 19 | $NAME_MAP->{$language_name}, | |||||||||||||||||||
| 97 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_2, | ||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 19 | ▶ | return $code if $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 17 | $code = Locale::Codes::Language::language2code( | |||||||||||||||||||
| 105 | $language_name, | ||||||||||||||||||||
| 106 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_3, | ||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 17 | ▶ | return $code if $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | $code = Locale::Codes::Language::language2code( | ||||||||||||||||||||
| 112 | 4 | $NAME_MAP->{$language_name}, | |||||||||||||||||||
| 113 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_3, | ||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 4 | ▶ | return $code if $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 2 | $code = $self->custom_codes()->{$language_name}; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 2 | ▶ | return $code if $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | #print STDERR "$language_name has no code\n"; | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | return $language_name; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 45 | ▶ | sub code2language { | ||||||||||||||||||
|
Subroutine
code2language: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 128 | 45 | my $self = shift; | |||||||||||||||||||
| 129 | 45 | my $code = shift; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 45 | my $name; | |||||||||||||||||||
| 132 | 45 | ▶ | if (length($code) == 2) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 133 | 27 | $name = Locale::Codes::Language::code2language( | |||||||||||||||||||
| 134 | $code, | ||||||||||||||||||||
| 135 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_2, | ||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||
| 137 | } elsif (length($code) == 3) { | ||||||||||||||||||||
| 138 | 15 | $name = Locale::Codes::Language::code2language( | |||||||||||||||||||
| 139 | $code, | ||||||||||||||||||||
| 140 | Locale::Codes::Constants::LOCALE_LANG_ALPHA_3, | ||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 45 | ▶ | return $name if $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 3 | my %custom_names = reverse %{$self->custom_codes()}; | |||||||||||||||||||
| 147 | 3 | ▶ | return $custom_names{$code} || $name; | ||||||||||||||||||
|
Condition33%1/3
$custom_names{$code} || $name
MC/DC0%0/2
$custom_names{$code} || $name
$custom_names{$code}
$name
Truth table33%1/3
A || B
A= $custom_names{$code}B= $name
| |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 3 | 1; | |||||||||||||||||||