-
Notifications
You must be signed in to change notification settings - Fork 22
/
linguakit.perl
executable file
·576 lines (505 loc) · 18.4 KB
/
linguakit.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use lib dirname(__FILE__);
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
use utf8;
push @ARGV, "-h" if $#ARGV < 0;
#Linguakit dependencies
push(@INC, dirname(__FILE__)."/lib");
my $deps = 1;
if (!eval{require Getopt::ArgParse;}){
warn "Please install Getopt::ArgParse module: cpan Getopt::ArgParse\n";
$deps = 0;
}
if (!eval{require Storable;}){
warn "Please install Storable module: cpan Storable\n";
$deps = 0;
}
if (!eval{require PerlIO::gzip;}){
warn "WARNING PerlIO::gzip module not installed, use 'cpan PerlIO::gzip' to solve\n";
}
if (!eval{require LWP::UserAgent;}){
warn "WARNING LWP::UserAgent module not installed, use 'cpan LWP::UserAgent' to solve\n";
}
if (!eval{require HTTP::Request::Common;}){
warn "WARNING HTTP::Request::Common module not installed, use 'cpan HTTP::Request::Common' to solve\n";
}
exit 1 if !$deps;
#Custom text
my $name = "linguakit";
my $shortDescription = "";
my $longDescription = "";
my $foot = "";
my $errorPrint = "$name: ";
my $parser = Getopt::ArgParse -> new_parser( prog => $name, help => $shortDescription, description => $longDescription, epilog => $foot, error_prefix => $errorPrint);
my @common_args = (
['lang', type => 'Scalar', dest => 'lang', required => 1, metavar => "<lang>", help => "Choose the language", choices_i => ["en", "es", "gl", "pt", "histgz"], default => "en"],
['input', type => 'Scalar', dest => 'input', metavar => "<input>", help => "Path of the input(a plain text file or gz/zip) by default STDIN"],
['-s', type => 'Bool', dest => 'string', help => "Changes input from a file path to a text string"]
);
#Modulos
$parser->add_subparsers(title => 'Modules', description => '');
my $dep= $parser->add_parser('dep', help => "Dependency syntactic analysis");
$dep->add_args(@common_args);
$dep->add_argument('-a', type => 'Bool', dest => 'a', help => "Simple dependency analysis (by default syntactic output");
$dep->add_argument('-fa', type => 'Bool', dest => 'fa', help => "Full dependency analysis");
$dep->add_argument('-c', type => 'Bool', dest => 'c', help => "Tagged text with syntactic information (for correction rules)");
$dep->add_argument('-conll', type => 'Bool', dest => 'conll', help => "CoNLL output style");
my $tagger= $parser->add_parser('tagger', help => "Part-of-speech tagging");
$tagger->add_args(@common_args);
$tagger->add_argument('-noner', type => 'Bool', dest => 'noner', help => "No NER or NEC is processed (by default PoS tagger output)");
$tagger->add_argument('-ner', type => 'Bool', dest => 'ner', help => "PoS tagger with Named Entity Recognition - NER (only with 'tagger' module)");
$tagger->add_argument('-nec', type => 'Bool', dest => 'nec', help => "PoS tagger with Named Entity Classification - NEC (only with 'tagger' module)");
my $mwe= $parser->add_parser('mwe', help => "Multiword extraction");
$mwe->add_args(@common_args);
$mwe->add_argument('-chi', type => 'Bool', dest => 'chi', help => "Chi-square co-occurrence measure (by default)");
$mwe->add_argument('-log', type => 'Bool', dest => 'log', help => "Loglikelihood ");
$mwe->add_argument('-scp', type => 'Bool', dest => 'scp', help => "Symmetrical conditional probability");
$mwe->add_argument('-mi', type => 'Bool', dest => 'mi', help => "Mutual information ");
$mwe->add_argument('-cooc', type => 'Bool', dest => 'cooc', help => "Co-occurrence counting");
my $key= $parser->add_parser('key', help => "Keyword extraction");
$key->add_args(@common_args);
my $recog= $parser->add_parser('recog', help => "Language recognition");
$recog->add_args(@common_args[1..2]);
my $sent= $parser->add_parser('sent', help => "Sentiment analysis");
$sent->add_args(@common_args);
my $rel= $parser->add_parser('rel', help => "Relation extraction");
$rel->add_args(@common_args);
my $tok= $parser->add_parser('tok', help => "Tokenizer");
$tok->add_args(@common_args);
$tok->add_argument('-split', type => 'Bool', dest => 'split', help => "Tokenization with splitting");
$tok->add_argument('-sort', type => 'Bool', dest => 'sort', help => "Tokenization with tokens sorted by frequency");
my $seg= $parser->add_parser('seg', help => "Sentence segmentation");
$seg->add_args(@common_args);
my $lem= $parser->add_parser('lem', help => "Lemmatization");
$lem->add_args(@common_args);
my $kwic= $parser->add_parser('kwic', help => "Keyword in context (concordances)");
$kwic->add_args(@common_args);
$kwic->add_argument('-tokens', type => 'Scalar', dest => 'tokens', required => 1 ,help => "the keyword to be searched");
my $link= $parser->add_parser('link', help => "Entity linking and semantic annotation");
$link->add_args(@common_args);
$link->add_argument('-json', type => 'Bool', dest => 'json', help => "Json output format of entity linking (by default)");
$link->add_argument('-xml', type => 'Bool', dest => 'xml', help => "Xml output format of entity linking");
my $aval= $parser->add_parser('aval', help => "Language checker: Avalingua");
$aval->add_args(@common_args);
$aval->add_argument('-json', type => 'Bool', dest => 'json', help => "Json output format of language checker (by default)");
$aval->add_argument('-xml', type => 'Bool', dest => 'xml', help => "Xml output format of language checker");
my $sum= $parser->add_parser('sum', help => "Text summarizer");
$sum->add_args(@common_args);
$sum->add_argument('-p', type => 'Scalar', dest => 'percentage', help => "Percentage of the input text that will be summarized (by default 10%)");
my $conj= $parser->add_parser('conj', help => "Verb conjugator");
$conj->add_args(@common_args);
$conj->add_argument('-pe', type => 'Bool', dest => 'pe', help => "The verb conjugator uses European Portuguese (by default)");
$conj->add_argument('-pb', type => 'Bool', dest => 'pb', help => "The verb conjugator uses Brasilian Portuguese");
$conj->add_argument('-pen', type => 'Bool', dest => 'pen', help => "The verb conjugator uses European Portuguese before the spell agreement");
$conj->add_argument('-pbn', type => 'Bool', dest => 'pbn', help => "The verb conjugator uses Brasilian Portuguese before the spell agreement");
my $coref= $parser->add_parser('coref', help => "Named entity coreference solver");
$coref->add_args(@common_args);
$coref->add_argument('-crnec', type => 'Bool', dest => 'crnec', help => "NEC correction with NE Coreference Resolution");
my $args = $parser->parse_args();
my $LING = $args->get_attr("lang")?$args->lang:"en";
my $MOD = $args->current_command();
my $FILE = $args->get_attr("input");
my $STRING = $args->get_attr("string");
###################################################################
# Script to use different linguistic tools, for instance:
# - - dependency parser (DepPattern)
# - - PoS tagger + NER + NEC
# - - Sentiment Analysis
# - - Multiword Extractor (GaleXtra)
#
# Pablo Gamallo
# ProLNat@GE Group, CiTIUS
# University of Santiago de Compostela
###################################################################
############################
# Config
############################
my $PROGS = "scripts";
my $DIRPARSER = "parsers";
my $NAMEPARSER ="$DIRPARSER/parserDefault-$LING.perl";
my $FILTER ="$PROGS/AdapterFreeling-${LING}.perl";
my $CONLL ="$PROGS/saidaCoNLL-fa.perl";
my $SENT = "tagger/$LING/sentences-${LING}_exe.perl";
my $TOK = "tagger/$LING/tokens-${LING}_exe.perl";
my $SPLIT = "tagger/$LING/splitter-${LING}_exe.perl";
my $LEMMA = "tagger/$LING/lemma-${LING}_exe.perl";
my $NER = "tagger/$LING/ner-${LING}_exe.perl";
my $TAGGER = "tagger/$LING/tagger-${LING}_exe.perl" ;
my $NEC = "tagger/$LING/nec-${LING}_exe.perl";
my $COREF = "tagger/coref/coref_exe.perl";
my $SENTIMENT = "sentiment/nbayes.perl";
my $MWE = "mwe/mwe.perl";
my $MWE_FILT = "mwe/filtro_galextra.perl";
my $MWE_SIX = "mwe/six_tokens.perl";
my $KEYWORD = "keywords/keywords_exe.perl";
my $REL = "triples/triples_exe.perl";
my $LINK = "linking/linking_exe.perl";
my $SUM = "summarizer/summarizer_exe.perl";
my $SENT_SPLIT = "summarizer/Sentence.pl";
my $CONJ = "conjugator/conjugator_exe.perl";
my $AVAL = "avalingua/avalingua_exe.perl";
my $QUELINGUA = "lanrecog/lanrecog.perl";
my $QUELINGUA_LEX = "lanrecog/build_lex.perl";
my $KWIC = "kwic/kwic.perl";
#######################
# EXECUTION #
#######################
my $input;
if($FILE){
if ($STRING){
open($input,"<",\$FILE);
}elsif ($FILE =~ m/\.zip$/){
open ($input, '<:gzip', $FILE) or die("'$FILE' not found.");
}else{
open ($input, '<', $FILE) or die("'$FILE' not found.");
}
binmode $input, ':utf8';
}else{
$input = \*STDIN;
}
if($MOD eq "dep"){
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $FILTER;
do $NAMEPARSER;
if($args->conll){ ##Conll dependency output
do $CONLL;
while(my $line = <$input>){
my $list = CONLL::conll(Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-fa'));
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($args->fa){
while(my $line = <$input>){
my $list = Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-fa');
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($args->c){
while(my $line = <$input>){
my $list = Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-c');
for my $result (@{$list}){
print "$result\n";
}
}
}else{##by default dependency output
while(my $line = <$input>){
my $list = Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-a');
for my $result (@{$list}){
print "$result\n";
}
}
}
}elsif($MOD eq "rel"){ ##Triples extration (Open Information Extraction)
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $FILTER;
do $NAMEPARSER;
do $CONLL;
do $REL;
while(my $line = <$input>){
my $list = Triples::triples(CONLL::conll(Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-fa')));
for my $result (@{$list}){
print "$result\n";
}
}
# while(my $line = <$input>){
# my $list = Triples::triples(CONLL::conll(Parser::parse(AdapterFreeling::adapter(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), '-fa')));
# for my $result (@{$list}){
# print "$result\n";
# }
# }
}elsif($MOD eq "tagger"){
do $SENT;
do $TOK;
do $SPLIT;
do $TAGGER;
if ($args->ner){ ##PoS tagging with ner
do $NER;
while(my $line = <$input>){
my $list = Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))));
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($args->nec){ ##PoS tagging with nec
do $NER;
do $NEC;
while(my $line = <$input>){
my $list = Nec::nec(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line]))))));
for my $result (@{$list}){
print "$result\n";
}
}
}else{ ##by default PoS tagging
do $LEMMA;
while(my $line = <$input>){
my $list = Tagger::tagger(Lemma::lemma(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))));
for my $result (@{$list}){
print "$result\n";
}
}
}
}elsif($MOD eq "coref"){
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $NEC;
do $COREF;
my $Mentions_id = {};#<ref><hash><integer>
if($args->crnec){ ##PoS tagging with Coreference Resolution and NEC correction
while(my $line = <$input>){
my $list = Coref::coref(0, 1, Nec::nec(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), 500, $Mentions_id);
for my $result (@{$list}){
print "$result\n";
}
}
}else{ ##PoS tagging with Coreference Resolution
while(my $line = <$input>){
my $list = Coref::coref(1, 0, Nec::nec(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])))))), 500, $Mentions_id);
for my $result (@{$list}){
print "$result\n";
}
}
}
}elsif($MOD eq "mwe"){ ##multiword extraction
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $MWE_FILT;
do $MWE_SIX;
do $MWE;
if($args->log){
# while(my $line = <$input>){
my @lines = <$input>;
my $list = Mwe::mwe(SixTokens::sixTokens(FiltroGalExtra::filtro(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))))),"-log",1);
print STDERR "#@lines\n";
for my $result (@{$list}){
print "$result\n";
}
# }
}elsif($args->scp){
# while(my $line = <$input>){
my @lines = <$input>;
my $list = Mwe::mwe(SixTokens::sixTokens(FiltroGalExtra::filtro(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))))),"-scp",1);
for my $result (@{$list}){
print "$result\n";
}
# }
}elsif($args->mi){
# while(my $line = <$input>){
my @lines = <$input>;
my $list = Mwe::mwe(SixTokens::sixTokens(FiltroGalExtra::filtro(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))))),"-mi",1);
for my $result (@{$list}){
print "$result\n";
}
# }
}elsif($args->cooc){
# while(my $line = <$input>){
my @lines = <$input>;
my $list = Mwe::mwe(SixTokens::sixTokens(FiltroGalExtra::filtro(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))))),"-cooc",1);
for my $result (@{$list}){
print "$result\n";
}
# }
}else{
# while(my $line = <$input>){
my @lines = <$input>;
my $list = Mwe::mwe(SixTokens::sixTokens(FiltroGalExtra::filtro(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))))),"-chi",1);
for my $result (@{$list}){
print "$result\n";
}
# }
}
}elsif($MOD eq "key"){
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $KEYWORD;
Keywords::load($LING);
# while(my $line = <$input>){
my @lines = <$input>;
#my $list = Keywords::keywords(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line]))))));
my $list = Keywords::keywords(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([join("\n" ,@lines)]))))));
for my $result (@{$list}){
print "$result\n";
}
#}
}elsif($MOD eq "sent"){ ##sentiment analysis
do $SENT;
do $TOK;
do $SPLIT;
do $NER;
do $TAGGER;
do $SENTIMENT;
Nbayes::load($LING);
while(my $line = <$input>){
if ($line !~ /\w/){next}
my $result = Nbayes::nbayes(Tagger::tagger(Ner::ner(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line]))))));
print "$result\n";
}
my $result = Nbayes::end();
print $result;
}elsif($MOD eq "recog"){ ##language recognition
do $SENT;
do $TOK;
do $QUELINGUA_LEX;
do $QUELINGUA;
my %Peso = ();#Line acumulator
my $ling = "en";
while(my $line = <$input>){
$ling = LanRecog::langrecog(Tokens::tokens(Sentences::sentences([$line])), \%Peso);
}
print "$ling\n";
}elsif($MOD eq "tok"){ ##tokenizer
do $SENT;
do $TOK;
if($args->sort){ ##tokenizer with sorting by frequency
do $SPLIT;
my %count = ();
while(my $line = <$input>){
my $list = Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])));
for my $token (@{$list}){
$count{$token} = $count{$token} ? $count{$token} + 1 : 1;
}
}
for my $result (sort {$count{$b} <=> $count{$a}} keys %count){
print "$count{$result}\t$result\n";
}
}elsif($args->split){
do $SPLIT;
while(my $line = <$input>){
my $list = Splitter::splitter(Tokens::tokens(Sentences::sentences([$line])));
for my $result (@{$list}){
print "$result\n";
}
}
}else{
while(my $line = <$input>){
my $list = Tokens::tokens(Sentences::sentences([$line]));
for my $result (@{$list}){
print "$result\n";
}
}
}
}elsif($MOD eq "seg"){ ##segmentation
do $SENT;
while(my $line = <$input>){
my $list = Sentences::sentences([$line]);
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($MOD eq "lem"){ ##lemmatizer and PoS tagging
do $SENT;
do $TOK;
do $SPLIT;
do $LEMMA;
while(my $line = <$input>){
my $list = Lemma::lemma(Splitter::splitter(Tokens::tokens(Sentences::sentences([$line]))));
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($MOD eq "kwic"){ ##key word in context (kwic) or concordances with just tokens as context
do $SENT;
do $KWIC;
while(my $line = <$input>){
# my $list = Kwic::kwic(Sentences::sentences([$line]),$args->tokens);
my (@line) = split ("\n", $line);
my $list = Kwic::kwic(\@line,$args->tokens);
for my $result (@{$list}){
print "$result\n";
}
}
}elsif($MOD eq "link"){ ##entity linking
do $LINK;
if($args->xml){
#while(my $line = <$input>){
my @lines = <$input>;
my $result = Linking::linking(join("\n" ,@lines),$LING,"xml");
print "$result\n"
# }
}else{
# while(my $line = <$input>){
my @lines = <$input>;
my $result = Linking::linking(join("\n" ,@lines),$LING,"json");
print "$result\n"
# }
}
}elsif($MOD eq "aval"){ ##entity linking
do $AVAL;
if($args->xml){
#while(my $line = <$input>){
my @lines = <$input>;
my $result = Avalingua::avalingua(join("\n" ,@lines),$LING,"xml");
print "$result\n"
# }
}else{
# while(my $line = <$input>){
my @lines = <$input>;
my $result = Avalingua::avalingua(join("\n" ,@lines),$LING,"json");
print "$result\n"
# }
}
}elsif($MOD eq "sum"){ ##summarizer
do $SUM;
do $SENT_SPLIT;
if($args->percentage){
my @lines = <$input>;
my ($result) = Summarizer::summarizer(join("\n" ,@lines),$LING,$args->percentage);
print "<abstract>$result</abstract>\n";
}else{
my @lines = <$input>;
my ($result) = Summarizer::summarizer(join("\n" ,@lines),$LING,10);
print "<abstract>$result</abstract>\n";
}
}elsif($MOD eq "conj"){ ##conjugator
do $CONJ;
if($args->pe){
while(my $line = <$input>){
my $result = Conjugator::conjugator($line,$LING,'-pe');
print "$result\n"
}
}elsif($args->pb && $LING eq "pt"){
while(my $line = <$input>){
my $result = Conjugator::conjugator($line,$LING,'-pb');
print "$result\n"
}
}elsif($args->pen && $LING eq "pt"){
while(my $line = <$input>){
my $result = Conjugator::conjugator($line,$LING,'-pen');
print "$result\n"
}
}elsif($args->pbn && $LING eq "pt"){
while(my $line = <$input>){
my $result = Conjugator::conjugator($line,$LING,'-pbn');
print "$result\n"
}
}else{
while(my $line = <$input>){
my $result = Conjugator::conjugator($line,$LING);
print "$result\n"
}
}
}
close($input);