blib/lib/Dallycot/Library/Linguistics.pm

filter: Statementstmt 70.9% 88 / 124 Branchbran 35.0% 7 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 18 / 20 Podpod n/a totaltotal 67.4% 114 / 169 CC 13 SCAR 19.4
CC
13
Cov
65%
__ANON__
20.8
__ANON__
12.0
__ANON__
6.0
CRAP 6.9
« blib/lib/Dallycot/Processor.pm ↑ Summary blib/lib/Dallycot/Library.pm »
1 package Dallycot::Library::Linguistics;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Core library of useful functions for functions
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Dallycot::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Dallycot::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Dallycot::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Dallycot::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Dallycot::TextResolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use Lingua::StopWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 BEGIN {
21 9 ▶ eval {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 require Lingua::YALI::LanguageIdentifier;
23 }
24 }
25 # use Lingua::ConText;
26 9 ▶use Lingua::Sentence;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use Promises qw(deferred collect);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 9 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 9 ns 'http://www.dallycot.net/ns/linguistics/1.0#';
36  
37 #====================================================================
38 #
39 # Textual/Linguistic functions
40  
41 define 'sentences' => (
42 hold => 0,
43 arity => 1,
44 options => {}
45 ), sub {
46 0 ▶ my( $engine, $options, $text ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
47  
48 0 ▶ if(!$text -> isa('Dallycot::Value::String')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text->isa("Dallycot::Value::String")) 0 0 0
49 0 croak 'sentences expects a string text';
50 }
51  
52 0 my $splitter = Lingua::Sentence->new($text->lang);
53  
54 return Dallycot::Value::Vector->new(
55 0 map { Dallycot::Value::String->new($_) }
56 split(/\n/, $splitter->split($text->value))
57 );
58 9 };
59  
60 # define 'clinical-context' => (
61 # hold => 0,
62 # arity => 2,
63 # options => {}
64 # ), sub {
65 # my( $engine, $options, $concept, $text ) = @_;
66 #
67 # if(!$concept->isa('Dallycot::Value::String')) {
68 # croak 'context expects a string concept';
69 # }
70 # if(!$text->isa('Dallycot::Value::String')) {
71 # croak 'context expects a string text';
72 # }
73 #
74 # my $result = Lingua::ConText::applyContext(
75 # $concept->value,
76 # $text->value
77 # );
78 #
79 # return Dallycot::Value::Vector->new(
80 # map { Dallycot::Value::String -> new($_) }
81 # @{$result}
82 # );
83 # };
84  
85 define
86 'stop-words' => (
87 hold => 0,
88 arity => 1,
89 options => {}
90 ),
91 sub {
92 1 ▶ my ( $engine, $options, $language ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.3
93  
94 1 my $d = deferred;
95  
96 1 ▶ if ( !$language->isa('Dallycot::Value::String') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $language->isa('Dallycot::Value::String')) { } 0 1 1
97 0 $d->reject("stop-words expects a string argument");
98 }
99 else {
100 1 my $lang = $language->value;
101 my @words = sort
102 1 ▶ keys %{ Lingua::StopWords::getStopWords( $lang, 'UTF-8' ) || {} };
Branch: 1 branch
BranchTrueFalseTotal
Lingua::StopWords::getStopWords($lang, "UTF-8") or {} 0 1 1
103 1 $d->resolve( Dallycot::Value::Vector->new( map { Dallycot::Value::String->new( $_, $lang ) } @words ) );
104 }
105  
106 1 return $d->promise;
107 9 };
108  
109 9 my %language_codes_for_classifier = qw(
110 af afr
111 am amh
112 ar ara
113 an arg
114 az aze
115 be bel
116 bn ben
117 bs bos
118 br bre
119 bg bul
120 ca cat
121 cs ces
122 cv chv
123 co cos
124 cy cym
125 da dan
126 de deu
127 el ell
128 en eng
129 eo epo
130 et est
131 eu eus
132 fo fao
133 fa fas
134 fi fin
135 fr fra
136 fy fry
137 gd gla
138 ga gle
139 gl glg
140 gu guj
141 ht hat
142 sh hbs
143 he heb
144 hi hin
145 hr hrv
146 hu hun
147 hy hye
148 io ido
149 ia ina
150 id ind
151 is isl
152 it ita
153 jv jav
154 ja jpn
155 kn kan
156 ka kat
157 kk kaz
158 ko kor
159 ku kur
160 la lat
161 lv lav
162 li lim
163 lt lit
164 lb ltz
165 ml mal
166 mr mar
167 mk mkd
168 mg mlg
169 mn mon
170 mi mri
171 ms msa
172 my mya
173 ne nep
174 nl nld
175 nn nno
176 no nor
177 oc oci
178 os oss
179 pl pol
180 pt por
181 qu que
182 ro ron
183 ru rus
184 sk slk
185 sl slv
186 es spa
187 sq sqi
188 sr srp
189 su sun
190 sw swa
191 sv swe
192 ta tam
193 tt tat
194 te tel
195 tg tgk
196 tl tgl
197 th tha
198 tr tur
199 uk ukr
200 ur urd
201 uz uzb
202 vi vie
203 vo vol
204 wa wln
205 yi yid
206 yo yor
207 zh zho
208 );
209  
210 9 my %language_codes_from_classifier = reverse %language_codes_for_classifier;
211  
212 define
213 'build-language-classifier-languages' => (
214 hold => 0,
215 arity => 0,
216 options => {}
217 ),
218 sub {
219 8 ▶ my ($engine) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
220  
221 8 ▶ if(!defined $Lingua::YALI::LanguageIdentifier::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Lingua::YALI::LanguageIdentifier::VERSION) 0 8 8
222 0 return Dallycot::Value::Vector->new();
223 }
224  
225 8 my $d = deferred;
226  
227 $d->resolve( Dallycot::Value::Vector->new(
228 784 map { Dallycot::Value::String->new($_) }
229 976 grep { $_ }
230 976 map { $language_codes_from_classifier{$_} }
231 8 @{Lingua::YALI::LanguageIdentifier -> new -> get_available_languages}
232 ) );
233  
234 8 return $d->promise;
235 9 };
236  
237 9 define 'language-classifier-languages' => 'build-language-classifier-languages()';
238  
239 define 'classify-text-language' => (
240 hold => 0,
241 arity => 1,
242 options => { 'languages' => Dallycot::Value::Vector -> new(Dallycot::Value::String->new('en')) }
243 ), sub {
244 1 ▶ my ( $engine, $options, $text ) = @_;
Subroutine __ANON__: called CC=7 SCAR=30.3
245  
246 1 ▶ if ( !$text -> isa('Dallycot::Value::String') && !$text->isa('Dallycot::Value::URI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text->isa("Dallycot::Value::String") or $text->isa("Dallycot::Value::URI")) 0 1 1
Condition33%1/3
$text->isa("Dallycot::Value::String") or $text->isa("Dallycot::Value::URI")
l!l&&r!l&&!r
100
MC/DC0%0/2
$text->isa("Dallycot::Value::String") or $text->isa("Dallycot::Value::URI")
$text->isa("Dallycot::Value::String") $text->isa("Dallycot::Value::URI")
Truth table33%1/3
A or B
A= $text->isa("Dallycot::Value::String")B= $text->isa("Dallycot::Value::URI")
ABresult
1X1
011
000
247 0 croak "language-classify requires a String or URI as a second argument";
248 }
249 1 ▶ if( !$options->{'languages'} -> isa('Dallycot::Value::Vector') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"languages"}->isa("Dallycot::Value::Vector")) 0 1 1
250 0 croak "language-classifier's 'languages' option requires a vector of strings";
251 }
252 my @languages =
253 1 grep { $_ }
254 1 map { $language_codes_for_classifier{ $_ -> value } }
255 1 grep { $_->isa('Dallycot::Value::String') }
256 1 $options->{'languages'}->values;
257  
258 1 ▶ if(!@languages) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@languages) 0 1 1
259 0 croak "language-classifier's 'languages' option requires a vector of strings";
260 }
261 1 ▶ if(!defined $Lingua::YALI::LanguageIdentifier::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Lingua::YALI::LanguageIdentifier::VERSION) 0 1 1
262 0 return Dallycot::Value::Vector->new();
263 }
264  
265 1 my $identifier = Lingua::YALI::LanguageIdentifier->new;
266 1 $identifier->add_language($_) for @languages;
267  
268 1 given ( blessed $text ) {
269 1 when ('Dallycot::Value::String') {
270 1 my $result = $identifier->identify_string( $text->value );
271 return Dallycot::Value::String -> new(
272 $language_codes_from_classifier{
273 1 $result -> [0] -> [0]
274 }
275 );
276 }
277 0 when ('Dallycot::Value::URI') {
278 return $text -> resolve_content -> then(
279 sub {
280 0 ▶ my ($body) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
281 0 my $content = '';
282 0 given ( blessed $body ) {
283 0 when ('HTML') { # content-type: text/html
284 # we want to strip out the HTML and keep only text in the
285 # <body /> outside of <script/> tags
286 0 my $dom = Mojo::DOM->new( $body->{'value'} );
287 0 $content = $dom->find('body')->all_text;
288 }
289 0 when ('Dallycot::Value::String') { # content-type: text/plain
290 0 $content = $body->value;
291 }
292 0 when ('XML') { # content-type: text/xml (TEI, etc.)
293 0 my $dom = Mojo::DOM->new->xml(1)->parse( $body->{'value'} );
294 0 $content = $dom->all_text;
295 }
296 0 default {
297 0 croak "Unable to extract text from " . $text->{'value'};
298 }
299 }
300 0 my $worked = eval {
301 # TODO: make '4096' a tunable parameter
302 # algorithm takes a *long* time with large strings
303 0 my $result = $identifier->identify_string( substr( $content, 0, 4096 ) );
304 Dallycot::Value::String -> new(
305 $language_codes_from_classifier{
306 0 $result -> [0] -> [0]
307 }
308 );
309 };
310 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not $worked) { } 0 0 0
311 0 croak $@;
312 }
313 elsif ( !$worked ) {
314 0 croak "Unable to identify language.";
315 }
316 else {
317 0 return $worked;
318 }
319 }
320 0 );
321 }
322 0 default {
323 0 croak "language-classify requires a String or URI as a second argument";
324 }
325 }
326 9 };
327  
328 9 define 'stop-word-languages' => '<<da nl en fi fr de hu it no pt es sv ru>>';
329  
330 9 1;
« blib/lib/Dallycot/Processor.pm ↑ Summary blib/lib/Dallycot/Library.pm »