| 1 | package Lingua::Identify::Any; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $DATE = '2019-07-12'; # DATE | |||||||||||||||||||
| 4 | 1 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010_001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Log::ger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(detect_text_language); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | our %SPEC; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | our @BACKENDS = ( | |||||||||||||||||||
| 17 | 'Lingua::Identify::CLD', | ||||||||||||||||||||
| 18 | 'Lingua::Identify', | ||||||||||||||||||||
| 19 | 'WebService::DetectLanguage', | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | $SPEC{detect_text_language} = { | ||||||||||||||||||||
| 23 | 1 | v => 1.1, | |||||||||||||||||||
| 24 | summary => 'Detect language of text using one of '. | ||||||||||||||||||||
| 25 | 'several available backends', | ||||||||||||||||||||
| 26 | description => <<'_', | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | Backends will be tried in order. When a backend is not available, or when it | ||||||||||||||||||||
| 29 | fails to detect the language, the next backend will be tried. Currently | ||||||||||||||||||||
| 30 | supported backends: | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | * Lingua::Identify::CLD | ||||||||||||||||||||
| 33 | * Lingua::Identify | ||||||||||||||||||||
| 34 | * WebService::DetectLanguage (only when `try_remote_backends` is set to true) | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | _ | ||||||||||||||||||||
| 37 | args => { | ||||||||||||||||||||
| 38 | text => { | ||||||||||||||||||||
| 39 | schema => 'str*', | ||||||||||||||||||||
| 40 | req => 1, | ||||||||||||||||||||
| 41 | pos => 0, | ||||||||||||||||||||
| 42 | cmdline_src => 'stdin_or_file', | ||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||
| 44 | backends => { | ||||||||||||||||||||
| 45 | schema => ['array*', of=>['str*', in=>\@BACKENDS]], | ||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||
| 47 | try_remote_backends => { | ||||||||||||||||||||
| 48 | schema => 'bool*', | ||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||
| 50 | dlcom_api_key => { | ||||||||||||||||||||
| 51 | summary => 'API key for detectlanguage.com', | ||||||||||||||||||||
| 52 | description => <<'_', | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Only required if you use WebService::DetectLanguage backend. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | _ | ||||||||||||||||||||
| 57 | schema => 'str*', | ||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||
| 60 | result => { | ||||||||||||||||||||
| 61 | schema => 'hash', | ||||||||||||||||||||
| 62 | description => <<'_', | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | Status: will return 200 status if detection is successful. Otherwise, will | ||||||||||||||||||||
| 65 | return 400 if a specified backend is unknown/unsupported, or 500 if detection | ||||||||||||||||||||
| 66 | has failed. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Payload: a hash with the following keys: `backend` (the backend name used to | ||||||||||||||||||||
| 69 | produce the result), `lang_code` (str, 2-letter ISO language code), `confidence` | ||||||||||||||||||||
| 70 | (float), `is_reliable` (bool). | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | _ | ||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||
| 75 | 0 | ▶ | sub detect_text_language { | ||||||||||||||||||
|
Subroutine
detect_text_language: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||
| 76 | 0 | my %args = @_; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | my $backends = $args{backends} // | ||||||||||||||||||||
| 79 | ($ENV{PERL_LINGUA_IDENTIFY_ANY_BACKENDS} ? | ||||||||||||||||||||
| 80 | 0 | ▶ | [split /\s*,\s*/, $ENV{PERL_LINGUA_IDENTIFY_ANY_BACKENDS}] : undef); | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$args{"backends"} // ($ENV{"PERL_LINGUA_IDENTIFY_ANY_BACKENDS"} ? [split(/\s*,\s*/, $ENV{"PERL_LINGUA_IDENTIFY_ANY_BACKENDS"}, 0)] : undef)
MC/DC0%0/1
$args{"backends"} // ($ENV{"PERL_LINGUA_IDENTIFY_ANY_BACKENDS"} ? [split(/\s*,\s*/, $ENV{"PERL_LINGUA_IDENTIFY_ANY_BACKENDS"}, 0)] : undef)
$args{"backends"}
Truth table0%0/2
A
A= $args{"backends"}
| |||||||||||||||||||||
| 81 | my $try_remote_backends = $args{try_remote_backends} // | ||||||||||||||||||||
| 82 | 0 | ▶ | $ENV{PERL_LINGUA_IDENTIFY_ANY_TRY_REMOTE_BACKENDS}; | ||||||||||||||||||
|
Condition0%0/3
$args{"try_remote_backends"} // $ENV{"PERL_LINGUA_IDENTIFY_ANY_TRY_REMOTE_BACKENDS"}
MC/DC0%0/2
$args{"try_remote_backends"} // $ENV{"PERL_LINGUA_IDENTIFY_ANY_TRY_REMOTE_BACKENDS"}
$args{"try_remote_backends"}
$ENV{"PERL_LINGUA_IDENTIFY_ANY_TRY_REMOTE_BACKENDS"}
Truth table0%0/3
A // B
A= $args{"try_remote_backends"}B= $ENV{"PERL_LINGUA_IDENTIFY_ANY_TRY_REMOTE_BACKENDS"}
| |||||||||||||||||||||
| 83 | my $dlcom_api_key = $args{dlcom_api_key} // | ||||||||||||||||||||
| 84 | 0 | ▶ | $ENV{PERL_LINGUA_IDENTIFY_ANY_DLCOM_API_KEY}; | ||||||||||||||||||
|
Condition0%0/3
$args{"dlcom_api_key"} // $ENV{"PERL_LINGUA_IDENTIFY_ANY_DLCOM_API_KEY"}
MC/DC0%0/2
$args{"dlcom_api_key"} // $ENV{"PERL_LINGUA_IDENTIFY_ANY_DLCOM_API_KEY"}
$args{"dlcom_api_key"}
$ENV{"PERL_LINGUA_IDENTIFY_ANY_DLCOM_API_KEY"}
Truth table0%0/3
A // B
A= $args{"dlcom_api_key"}B= $ENV{"PERL_LINGUA_IDENTIFY_ANY_DLCOM_API_KEY"}
| |||||||||||||||||||||
| 85 | 0 | ▶ | my @backends = ( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 'Lingua::Identify::CLD', | ||||||||||||||||||||
| 87 | 'Lingua::Identify', | ||||||||||||||||||||
| 88 | ($try_remote_backends ? ('WebService::LanguageDetect') : ()), | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | 0 | ▶ | @backends = @$backends if $backends; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | my $res = [500, "No backend was tried", {}, { | |||||||||||||||||||
| 93 | 'func.attempted_backends' => [], | ||||||||||||||||||||
| 94 | }]; | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | BACKEND: | ||||||||||||||||||||
| 97 | 0 | for my $backend (@backends) { | |||||||||||||||||||
| 98 | 0 | ▶ | if ($backend eq 'Lingua::Identify::CLD') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 99 | 0 | eval { require Lingua::Identify::CLD; 1 }; | |||||||||||||||||||
| 100 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | log_debug "Skipping backend 'Lingua::Identify::CLD' because module is not available: $@"; | |||||||||||||||||||
| 102 | 0 | next BACKEND; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 0 | my $cld = Lingua::Identify::CLD->new; | |||||||||||||||||||
| 105 | 0 | my @lang = $cld->identify($args{text}); | |||||||||||||||||||
| 106 | 0 | push @{$res->[3]{'func.attempted_backends'}}, 'Lingua::Identify::CLD'; | |||||||||||||||||||
| 107 | 0 | ▶ | if (!@lang) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | log_debug "Backend 'Lingua::Identify::CLD' failed to detect language"; | |||||||||||||||||||
| 109 | 0 | next BACKEND; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 0 | $res->[0] = 200; | |||||||||||||||||||
| 112 | 0 | $res->[1] = "OK"; | |||||||||||||||||||
| 113 | 0 | $res->[2] = { | |||||||||||||||||||
| 114 | backend => 'Lingua::Identify::CLD', | ||||||||||||||||||||
| 115 | lang_code => $lang[1], | ||||||||||||||||||||
| 116 | confidence => $lang[2] / 100, | ||||||||||||||||||||
| 117 | is_reliable => $lang[3], | ||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||
| 119 | 0 | goto RETURN_RES; | |||||||||||||||||||
| 120 | # XXX put the other less probable language to func.* | ||||||||||||||||||||
| 121 | } elsif ($backend eq 'Lingua::Identify') { | ||||||||||||||||||||
| 122 | 0 | eval { require Lingua::Identify; 1 }; | |||||||||||||||||||
| 123 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | log_debug "Skipping backend 'Lingua::Identify' because module is not available: $@"; | |||||||||||||||||||
| 125 | 0 | next BACKEND; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | 0 | my @bres = Lingua::Identify::langof($args{text}); | |||||||||||||||||||
| 128 | 0 | push @{$res->[3]{'func.attempted_backends'}}, 'Lingua::Identify'; | |||||||||||||||||||
| 129 | 0 | ▶ | if (!@bres) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 0 | log_debug "Backend 'Lingua::Identify' failed to detect language, trying the next backend"; | |||||||||||||||||||
| 131 | 0 | next BACKEND; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 0 | $res->[0] = 200; | |||||||||||||||||||
| 134 | 0 | $res->[1] = "OK"; | |||||||||||||||||||
| 135 | 0 | $res->[2] = { | |||||||||||||||||||
| 136 | backend => 'Lingua::Identify', | ||||||||||||||||||||
| 137 | lang_code => $bres[0], | ||||||||||||||||||||
| 138 | confidence => $bres[1], | ||||||||||||||||||||
| 139 | is_reliable => 1, | ||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||
| 141 | 0 | goto RETURN_RES; | |||||||||||||||||||
| 142 | # XXX put the other less probable language to func.* | ||||||||||||||||||||
| 143 | } elsif ($backend eq 'WebService::DetectLanguage') { | ||||||||||||||||||||
| 144 | 0 | eval { require WebService::DetectLanguage; 1 }; | |||||||||||||||||||
| 145 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | log_debug "Skipping backend 'WebService::DetectLanguage' because module is not available: $@"; | |||||||||||||||||||
| 147 | 0 | next BACKEND; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | 0 | ▶ | $dlcom_api_key or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 0 | log_warn "Backend 'WebService::DetectLanguage' cannot be used, API key (dlcom_api_key) not provided, trying the next backend"; | |||||||||||||||||||
| 151 | 0 | next BACKEND; | |||||||||||||||||||
| 152 | }; | ||||||||||||||||||||
| 153 | 0 | my $api = WebService::DetectLanguage->new(key => $dlcom_api_key); | |||||||||||||||||||
| 154 | 0 | my @possib = $api->detect($args{text}); | |||||||||||||||||||
| 155 | 0 | push @{$res->[3]{'func.attempted_backends'}}, 'WebService::DetectLanguage'; | |||||||||||||||||||
| 156 | 0 | ▶ | if (!@possib) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 0 | log_debug "Backend 'WebService::DetectLanguage' failed to detect language, trying the next backend"; | |||||||||||||||||||
| 158 | 0 | next BACKEND; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | 0 | $res->[0] = 200; | |||||||||||||||||||
| 161 | 0 | $res->[1] = "OK"; | |||||||||||||||||||
| 162 | 0 | $res->[2] = { | |||||||||||||||||||
| 163 | backend => 'WebService::DetectLanguage', | ||||||||||||||||||||
| 164 | lang_code => $possib[0]->language->code, | ||||||||||||||||||||
| 165 | confidence_raw => $possib[0]->confidence, # not a range/percentage, the more text is being fed, the higher the confidence | ||||||||||||||||||||
| 166 | confidence => undef, | ||||||||||||||||||||
| 167 | is_reliable => $possib[0]->is_reliable, | ||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||
| 169 | 0 | goto RETURN_RES; | |||||||||||||||||||
| 170 | # XXX put the other less probable language to func.* | ||||||||||||||||||||
| 171 | } else { | ||||||||||||||||||||
| 172 | 0 | $res->[0] = 400; | |||||||||||||||||||
| 173 | 0 | $res->[1] = "Unknown/unsupported backend '$backend'"; | |||||||||||||||||||
| 174 | 0 | goto RETURN_RES; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | $res->[1] = 'No backends were able to detect the language' | ||||||||||||||||||||
| 179 | 0 | ▶ | if @{ $res->[3]{'func.attempted_backends'} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | 0 | RETURN_RES: | |||||||||||||||||||
| 181 | $res; | ||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 1 | 1; | |||||||||||||||||||
| 185 | # ABSTRACT: Detect language of text using one of several available backends | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||