-
Notifications
You must be signed in to change notification settings - Fork 0
/
NERefinements.pm
1089 lines (991 loc) · 40.9 KB
/
NERefinements.pm
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#===========File: NERefinements.pm===============
#Title: NERefinements.pm
#Description: The Module contains data refinement methods for NE tagging.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 08.06.2011
#Last Changes: 14.07.2011. by Mārcis Pinnis, SIA Tilde.
#===============================================
use strict;
use warnings;
package NERefinements;
use File::Basename;
use NEUtilities;
#==========Method: CombinedRefsOnFile===========
#Title: CombinedRefsOnFile
#Description: Calls all refinement methods on one file (saves processing time by reading the file only once).
#Author: Mārcis Pinnis, SIA Tilde.
#Created: 10.06.2011
#Last Changes: 04.07.2011. by Mārcis Pinnis, SIA Tilde.
#===============================================
sub CombinedRefsOnFile
{
#Checking if all required parameters are set.
if (not(defined($_[0])&&defined($_[1])&&defined($_[2])))
{
print STDERR "Usage: CombinedRefsOnFile [Input file] [Output file] [POS Tagged file with original newlines] [Refinement order definition string - optional]\n\tThe refinement order definition string has to be in the following form \"P1 P2 ... Pn\", where Pn is a refinement parameter. Available parameters are:\n\t\tA - Add missing line breaks (Other parameters after this one will be ignored).\n\t\tC - Consolidate equal entities.\n\t\tL - Clean brackets and quotation marks.\n\t\tN - Removes NEs, which contain more than a fixed number of strings (for example - persons and \"/\" string).\n\t\tS - Removes corrupt string tokens, for instance web addresses from NEs.\n\t\tR_0.# - Remove low probability NE tags (\"#\" have to be digits after 0).\n\t\tT_0.# - Tag equal lemmas (\"#\" have to be digits after 0).\n"; die;
}
my $inFile = $_[0];
my $outFile = $_[1];
my ($inputFileName,$inputFilePath,$inputFileSuffix) = fileparse($inFile,qr/\.[^.]*/);
my $tempFile = $inputFilePath.$inputFileName.".ref_temp";
my $posTaggedFile = $_[2];
my $refDefString = "";
#The combined refinements can also be run using a refinement order definition string, but if such is not given, a default approach is used to maximize precision (evaluated on Latvian development data).
if (!defined($_[3]) || $_[3] eq "")
{
$refDefString = "L N S R_0.7 C T_0.90";
}
else
{
#Set the refinement order definition string (argument 3).
$refDefString = $_[3];
}
my @refDefArray = split(/ /,$refDefString );
#Load the input file.
#Argument 0 - @arr - an array of tokens.
# @arr[$i] - @token.
# @token[$i] has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability.
my @arr = NERefinements::LoadTabSepFile($inFile);
my $saved = 0;
#Run all refinements in the order defined by the refinement order definition string.
for my $configParam ( @refDefArray )
{
if ($configParam =~ /T.*/)
{
#Tag Equal lemmas below the threshold, which is defined within the parameter ("T_0.#", where "#" are decimal digits of the parameter).
my $probab = $configParam;
$probab =~ s/^T_//g;
NERefinements::TagEqualLemmas(\@arr, $probab);
}
elsif ($configParam =~ /C.*/)
{
@arr = NERefinements::ConsolidateEqualEntities(\@arr);
}
elsif ($configParam =~ /R.*/)
{
#Remove NE tags below the threshold, which is defined within the parameter ("R_0.#", where "#" are decimal digits of the parameter).
my $probab = $configParam;
$probab =~ s/^R_//g;
@arr = NERefinements::RemoveLowProbNETags(\@arr,0,$probab);
}
elsif ($configParam =~ /L.*/)
{
NERefinements::CleanBracketsAndQuotations(\@arr);
}
elsif ($configParam =~ /S.*/)
{
NERefinements::RemoveCorruptStringTokensFromNETags(\@arr);
}
elsif ($configParam =~ /N.*/)
{
NERefinements::RemoveCorruptStringNETags(\@arr);
}
elsif ($configParam =~ /A.*/)
{
#Add missing line breaks, which are defined in the input data, but are not defined in the NE tagged results.
NERefinements::SaveTabSepDoc(\@arr,$tempFile);
NEUtilities::AddMissingLineBreaks($posTaggedFile, $tempFile, $outFile);
unlink ($tempFile);
$saved = 1;
#As after adding line breaks, the file is saved, no further refinements may be carried out (simply because these would again remove all line breaks).
last;
}
}
if ($saved == 0)
{
NERefinements::SaveTabSepDoc(\@arr,$outFile);
}
}
#====Method: ConsolidateEqualEntitiesOnFile=====
#Title: ConsolidateEqualEntitiesOnFile
#Description: Calls the consolidation of equal entities method on a single file (argument 0) and writes the results to a file (argument 1).
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 09.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub ConsolidateEqualEntitiesOnFile
{
if (not(defined($_[0])&&defined($_[1])))
{
print STDERR "Usage: ConsolidateEqualEntitiesOnFile [Input file] [Output file]\n"; die;
}
my @arr= NERefinements::LoadTabSepFile($_[0]);
@arr = NERefinements::ConsolidateEqualEntities(\@arr);
NERefinements::SaveTabSepDoc(\@arr,$_[1]);
}
#=========Method: CalculateProbibility==========
#Title: CalculateProbibility
#Description: Calculates the overall probability of a full NE tag from separate NE token probabilities (argument 0).
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 04.07.2011. by Mārcis Pinnis, SIA Tilde.
#===============================================
sub CalculateProbibility
{
if (!defined($_[0]))
{
print STDERR "Usage: CalculateProbibility [Probability array]\n"; die;
}
my @Probabilities = @{$_[0]};
#The array @Probabilities is a one dimensional array containing decimal values.
my $arrLen = @Probabilities;
my $Probability = 0;
#Sum all probabilities.
for my $i (0 .. $#Probabilities)
{
$Probability += $Probabilities[$i];
}
#Return 0 if no values defined or the sum is 0.
if ($Probability == 0 || $arrLen == 0)
{
return 0;
}
#Return the average if at least one non-zero probability exists.
return $Probability/($arrLen);
}
#=========Method: LoadTabSepFile==========
#Title: LoadTabSepFile
#Description: Reads all tokens form a tab separated file (argument 0) into an array.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 04.07.2011. by Mārcis Pinnis, SIA Tilde.
#===============================================
sub LoadTabSepFile
{
if (!defined($_[0]))
{
print STDERR "Usage: LoadTabSepFile [Input file]\n"; die;
}
open(FIN, "<:encoding(UTF-8)", $_[0]);
my @tokens;
#The array @tokens may contain tab separated values of any length and amount.
#Read each line of the tab separated file.
while (my $line = <FIN>)
{
$line =~ s/^\x{FEFF}//;# Strips BOM symbol.
$line =~ s/\n//;
$line =~ s/\r//;
if ($line =~ /^\s*$/) {next;}
my @token = split(/\t/,$line );
push @tokens , [@token];
}
close FIN;
return @tokens;
}
#=========Method: SaveTabSepDoc==========
#Title: SaveTabSepDoc
#Description: Saves the values of an array (argument 0) into a tab separated file (argument 1).
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 04.07.2011. by Mārcis Pinnis, SIA Tilde.
#===============================================
sub SaveTabSepDoc
{
if (not(defined($_[0])&& defined($_[1])))
{
print STDERR "Usage: SaveTabSepDoc [Array of tokens] [Output file]\n"; die;
}
my @arr = @{$_[0]};
#The array @arr may be a two dimensional array of any length or structure (as long as its second dimension is supported by the "join" function).
open(FOUT, ">:encoding(UTF-8)", $_[1]);
#Print each token to the output file.
for my $i (0 .. $#arr)
{
#Tab separators are automatically added by the join function.
print FOUT join("\t",@{$arr[$i]})."\n";
}
close FOUT;
}
#=========Method: GetFullNETagsFromTokens==========
#Title: GetFullNETagsFromTokens
#Description: Iterates trough an array of tokens (argument 0) and finds full NE tags from NE tokens and returns them as an array.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 09.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub GetFullNETagsFromTokens
{
if (not(defined($_[0])))
{
print STDERR "Usage: GetFullNETagsFromTokens [Reference To An Array Of Tokens] [Min Avereage Probability](optional) [Min First NE Token Probability](optional)\n"; die;
}
my @NEtags;
#Iterates trough an array of tokens (argument 0).
#Argument 0 - @{$_[0]} - an array of tokens.
# @{$_[0]}[$i] - @token.
# @token[$i] has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability (optional).
#Usage:
# The array of Tokens is used to search for NE tags.
# Each token contains a value that holds NE tags(${$_[0]}[][8]) and from these values Full NE tags are extracted and their positions saved.
for my $i (0 .. $#{$_[0]})
{
# NE tokens beginning with "B-" indicate full NE tag beggining.
if(${$_[0]}[$i][8]=~ /^B/)
{
my $pos=1;
my $endPos = ${$_[0]}[$i][6];
my $endLine = ${$_[0]}[$i][7];
my $lemmas = ${$_[0]}[$i][2];
my @probabilities;
push @probabilities, ${$_[0]}[$i][9];
# If this is not the last token, search for full NE tag ending.
if($i+$pos <= $#{$_[0]} )
{
#Finds full NE tag end position (all trailing tokens with NE tag starting whit 'I')
while (${$_[0]}[$i+$pos][8] =~ /^I/)
{
$endPos = ${$_[0]}[$i+$pos][6];
$endLine = ${$_[0]}[$i+$pos][7];
#Combines the lemmas in a string separated with a white space.
$lemmas .= " ".${$_[0]}[$i+$pos][2];
push @probabilities, ${$_[0]}[$i+$pos][9]; #Saves the NE token probabilities in an array.
$pos++;
if($i+$pos > $#{$_[0]} ){last;}
}
}
#Gets full NE tag name from the NE token of the first token.
my $NETagName = ${$_[0]}[$i][8];
$NETagName =~ s/^B-//;
$NETagName = NEUtilities::GetNEtagType($NETagName);
#Calculates full NE tag probability from all NE token probabilities in the full tag.
my $AvgProb = CalculateProbibility(\@probabilities);
#If optional argument 1 defined and not "0" replaces all tags with full NE tag probability lower than the arguments value whit an empty tag.
if ($_[1])
{
if ( $_[1] > $AvgProb ) {$NETagName = "O";}
}
#If optional argument 2 defined and not "0" replaces all tags with first NE token probability lower than the arguments value whit an empty tag.
if ($_[2])
{
if ( $_[2] > ${$_[0]}[$i][9] ) {$NETagName = "O";}
}
#Saves the NE tags: [0-3] - positions, [4] - lemmas separated by a white space ,[5] NE tag name, [6] NE Tag probability.
my @NETag =(${$_[0]}[$i][4], ${$_[0]}[$i][5], $endPos, $endLine, $lemmas,$NETagName,$AvgProb);
push @NEtags , [@NETag];
}
}
return @NEtags;
}
#=========Method: WriteNEtagsInTokens==========
#Title: WriteNEtagsInTokens
#Description: Writes full NE tags (from an array (argument 1)) as NE tokens assigned to tokens in to token array (argument 0).
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 09.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub WriteNEtagsInTokens
{
#Argument 0 - @{$_[0]} - reference to an array of tokens.
# @{$_[0]}[$i] - @token.
# @token has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability (optional).
#Usage:
# In the array of tokens, the method changes NE tags (${$_[0]}[][8]) of those tokens, whose positions match with an NE tag form the array defined by argument 1.
##<== FOR DEBUGING
# open (ERRO,">:encoding(UTF-8)", "test.log");
# for my $j (0 .. $#{$_[0]})
# {
# print ERRO join("\t",@{${$_[0]}[$j]})."\n";
# }
# print ERRO "\n\n\n\n\n\n";
# for my $j (0 .. $#{$_[1]})
# {
# print ERRO join("\t",@{${$_[1]}[$j]})."\n";
# }
##<==END
for my $i (0 .. $#{$_[0]})
{
#Argument 1 - @{$_[1]} - reference to an array of NE Tags.
# @{$_[1]}[$i] - @NETag.
# @NETag has values:[0] - start line, [1] - start position, [2] - end line, [3] - end position, [4] - lemmas separated by a white space ,[5] NE tag name, [6] NE Tag probability.
#Usage: @NETags array is used to search for position matchch with a token(argument 1). In case of a match the token is assigned with the NE tag it matches.
for my $j (0 .. $#{$_[1]})
{
#Finds tokens that have the same starting positions as NE tags.
if ((${$_[0]}[$i][4] == ${$_[1]}[$j][0]) && (${$_[0]}[$i][5] == ${$_[1]}[$j][1]) )
{
#Changes the NE tag to the new one.
my $pr=0;
#Changes the first NE token to the Full NE tags respective short version.
if ( ${$_[1]}[$j][5] ne "O" ) { ${$_[0]}[$i][8] = 'B-'.NEUtilities::GetShortTagType(${$_[1]}[$j][5]); }
#Handles empty tag separately because it has no short form.
else { ${$_[0]}[$i][8] = "O"; }
my $cont=0;
while (1)
{
#Adds trailing NE tags until full NE tag positions matches with token end position.
#print ERRO "${$_[0]}[$i + $cont][6] == ${$_[1]}[$j][2] and (${$_[0]}[$i + $cont][7] == ${$_[1]}[$j][3]) \n";
if ((${$_[0]}[$i + $cont][6] == ${$_[1]}[$j][2]) && (${$_[0]}[$i + $cont][7] == ${$_[1]}[$j][3])) {last;}
$cont++;
#if ($pr==1){ #<==FOR DEBUGGING!
#print STDERR ${$_[0]}[$i + $cont][0]." ".${$_[1]}[$j][5]."\n";} #<==FOR DEBUGGING!
if (${$_[1]}[$j][5] ne "O") {${$_[0]}[$i + $cont][8] = 'I-'.NEUtilities::GetShortTagType(${$_[1]}[$j][5]);}
else {${$_[0]}[$i + $cont][8]="O";}
}
}
}
}
# close ERRO;#<==FOR DEBUGGING!
}
#=========Method: ConsolidateEqualEntities==========
#Title: ConsolidateEqualEntities
#Description: Finds NE tags with equal lemmas and overwrites their tags with the ones that have the highest NE Tag probability.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 09.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub ConsolidateEqualEntities
{
if (not($_[0])){die " ERR:No paramters given to ConsolidateEqualEntities\n";}
#Argument 0 - reference to an array of tokens.
#Token array has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability.
#Usage: Argument 0 is reference to an array contains a list of tokens and all changes made to NE tags are saved in this array. It is also used to extract current Ne tags assigned to tokens.
my @tokens = @{$_[0]};
#Gets an array of NE tags.
# @NETags - A array of @NETag arrays.
# @NETag has values:[0] - start line, [1] - start position, [2] - end line, [3] - end position, [4] - lemmas separated by a white space ,[5] NE tag name, [6] NE Tag probability.
#Usage:@NETags is used to get unique lemmas and their NE tags and NE tag probability.
my @NEtags = GetFullNETagsFromTokens(\@tokens);
# %neProbabHash: {first key} - lemma, {second key} - NE tag, [0] - count of occurrences, [1] - minimum NE Tag probability, [2] maximum NE Tag probability, [3] - the sum of all NE Tag probability values.
#Usage: Used to find and evaluate different NE tags for the same lemmas.
my %neProbabHash;
#Puts NE tag lammas and full NE tags in hashes.
for my $i (0 .. $#NEtags)
{
my $lemmaKey = $NEtags[$i][4];#Lemma as primary hash key.
my $NEKey = $NEtags[$i][5]; #Ne tag name as hash key that is linked to the lemma.
#Creates a hash of hashes to link NE tagged lemmas separated by a space with possible NE tag types assigned to these lemmas. Saves the NE tag count, sum of probabilities and maximum and minimum probability values in an array linked to NE tag type keys.
if (defined $neProbabHash{$lemmaKey})
{
if (defined $neProbabHash{$lemmaKey}{$NEKey})
{
$neProbabHash{$lemmaKey}{$NEKey}[0]++;
if ($neProbabHash{$lemmaKey}{$NEKey}[1] > $NEtags[$i][6])
{
$neProbabHash{$lemmaKey}{$NEKey}[1] = $NEtags[$i][6];
}
if ($neProbabHash{$lemmaKey}{$NEKey}[2] < $NEtags[$i][6])
{
$neProbabHash{$lemmaKey}{$NEKey}[2] = $NEtags[$i][6];
}
$neProbabHash{$lemmaKey}{$NEKey}[3] += $NEtags[$i][6];
}
else
{
$neProbabHash{$lemmaKey}{$NEKey} = ();
push @{$neProbabHash{$lemmaKey}{$NEKey}} , (1,$NEtags[$i][6],$NEtags[$i][6],$NEtags[$i][6]);
}
}
else
{
$neProbabHash{$lemmaKey} = ();
$neProbabHash{$lemmaKey}{$NEKey} = ();
push @{$neProbabHash{$lemmaKey}{$NEKey}} , (1,$NEtags[$i][6],$NEtags[$i][6],$NEtags[$i][6]);
}
}
#Iterates trough lemmas. Keys1- NE tagged lemmas.
foreach my $keys1 (keys %neProbabHash )
{
my $count = 0 ;
#Mārcis: Added counting of NE occurrances regardless of type.
#$count++ foreach keys %{$neProbabHash{$keys1}}; #Count possible Ne tags for a lemma.
my $countOfOccurs = 0;
for my $keys2 (keys %{$neProbabHash{$keys1}})
{
$count++;
$countOfOccurs += $neProbabHash{$keys1}{$keys2}[0];
}
#Mārcis: end of changes
#if ($keys1=~/.*Lattelecom.*/) {print $keys1."\n";} #<==FOR DEBUGGING!
#If there is more than a single NE tag for the same lemma calculate which has the highest NE Tag probability.
if($count > 1)
{
my @maxVal = ([-1,"1"],[-1,"2"],[-1,"3"]); # 0 - MAX AVG 1 - MAX MIN 2- MAX MAX
for my $keys2 (keys %{$neProbabHash{$keys1}})
{
#if ($keys1=~/.*Lattelecom.*/) {print "\t".$keys2."\n";} #<==FOR DEBUGGING!
#Finds the NE tag with the highest minimum NE Tag probability.
if($maxVal[2][0] < $neProbabHash{$keys1}{$keys2}[2])
{
$maxVal[2][0] = $neProbabHash{$keys1}{$keys2}[2];
$maxVal[2][1] = $keys2;
}
#Finds the NE tag with the highest maximum NE Tag probability.
if($maxVal[1][0] < $neProbabHash{$keys1}{$keys2}[1])
{
$maxVal[1][0] = $neProbabHash{$keys1}{$keys2}[1];
$maxVal[1][1] = $keys2;
}
#Finds the NE tag with the highest average NE Tag probability.
#Mārcis: Changed the algorithm to favour higher frequency NEs.
#if( $maxVal[0][0] < ($neProbabHash{$keys1}{$keys2}[3]/$neProbabHash{$keys1}{$keys2}[0]) )
if( $maxVal[0][0] < ($neProbabHash{$keys1}{$keys2}[3]/$countOfOccurs) )
{
$maxVal[0][0] = $neProbabHash{$keys1}{$keys2}[3]/$countOfOccurs;
$maxVal[0][1] = $keys2;
}
#If two NE Tags have the same probability don’t retag anything.
if( $maxVal[0][0] == ($neProbabHash{$keys1}{$keys2}[3]/$countOfOccurs))
{
#Equals to 5 because five is greater than one, which is the highest probability to a NE tag.
$maxVal[0][0] = 5;
$maxVal[0][1] = "NotEquaToAnyThing";
}
}
#Finds the NE tag with the highest NE Tag probability changes the ne tags for all the lemmas ne NE tag array.
if (($maxVal[0][1] eq $maxVal[1][1]) || ($maxVal[0][1] eq $maxVal[2][1]) )
{
for my $i (0 .. $#NEtags)
{
if ($NEtags[$i][4] eq $keys1)
{
$NEtags[$i][5] = $maxVal[0][1];
}
}
}
}
}
WriteNEtagsInTokens(\@tokens,\@NEtags);
return @tokens;
}
#=========Method: RemoveLowProbNETags==========
#Title: RemoveLowProbNETags
#Description: Removes NE tags with probably lower than a number (argument 0) and returns the changed array.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 09.06.2011
#Last Changes: 09.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub RemoveLowProbNETags
{
if (not($_[0])){print STDERR "Usage: RemoveLowProbNETags [Array Of Tokens] [Min Avereage Probability](optional) [Min First NE Token Probability](optional)\n"; die;}
#Argument 0 - reference to an array of tokens.
#Each token has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability.
#Usage:
# The array of Tokens is used to extract NE tags from them and then write new ones in to them.
# Each token conatins a value that holds NE tags(${$_[0]}[][8]) is changed if NE Tag probability of the full NE tag id below threshold.
my @tokens = @{$_[0]};
my @NEtags = GetFullNETagsFromTokens(\@tokens,$_[1],$_[2]);
WriteNEtagsInTokens(\@tokens,\@NEtags);
return @tokens;
}
#=========Method: TagEqualLemmas==========
#Title: TagEqualLemmas
#Description: Tags untagged lemmas if the same lemmas have been tagged in other places with probability above threshold (argument 1). Changes the array passed doesn’t return anything.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 17.06.2011
#Last Changes: 20.06.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub TagEqualLemmas
{
if(not(($_[0])&&($_[1])))
{
die "usage TagEqualLemmas [reference To Array Of Tokens] [Threshold]";
}
#Argument 0 - @{$_[0]} - reference to an array of tokens.
# @{$_[0]}[$i] - @token.
# @token has values: [0] - token, [1] – POS tag, [2] - lemma, [3] - morphological tag, [4] - token start position(in text) in line, [5] - starting line, [6] - token end position, [7] - end line, [8] – NE tag, [9] - NE Tag probability (optional).
#Usage:
# The array of Tokens is used to extract lemmas linked to NE tags and write new NE tags in them.
# Each token contains a value that holds NE tag(${$_[0]}[][8]) which is saved in an array of full NE tags and is changed if positions match with new NE Tag (argument 1).
#Gets an array of full NE tags.
# @NETags -A list of @NETag arrays.
# @NETag has values:[0] - start line, [1] - start position, [2] - end line, [3] - end position, [4] - lemmas separated by a white space ,[5] NE tag name, [6] NE Tag probability.
#Usage: Contains full NE tags extracted from tokens. A hash of unique Lemmas and their NE probabilities is extracted from this array.
my @NEtags = GetFullNETagsFromTokens(\@{$_[0]});
#%lemmaHash: {key} - lemmas inside NE tag separated my a spece, [0] NE tag, [1] array of lemmas , [2] - lemma count, [3] - possibly of NE tag being correct
#Usage: Used to save unique lemmas and thier NE tags un NE tag probobility.
my %lemmaHash;
#Puts lemmas as keys in hashes where they are linked to their NE tags and the probability of these tags.
for my $i (0 .. $#NEtags)
{
my $lemmaKey = $NEtags[$i][4];
if (defined $lemmaHash{$lemmaKey}) #If the tagged lemma exists corrects the tags probability
{
#Checks NE tags match - if not, doesn't tag the lemmas.
if ($lemmaHash{$lemmaKey}[0] ne $NEtags[$i][5] )
{
$lemmaHash{$lemmaKey}[0] = "TagMismatch";
}
#Gets tag count and sum of probabilities to get average possibly of the tag being correct.
$lemmaHash{$lemmaKey}[2]++;
$lemmaHash{$lemmaKey}[3] += $NEtags[$i][6];
}
else#If the tagged lemma doesn’t exist creates a hash value.
{
$lemmaHash{$lemmaKey} = ();
my @splitLemaKey = split (/ /,$lemmaKey); #Saves split lemmas for code optimization.
#Saves hash.
push @{$lemmaHash{$lemmaKey}} , ($NEtags[$i][5],\@splitLemaKey,1,$NEtags[$i][6]);
}
}
#Tags the longest lemmas first to avoid tagging lemmas that are part of other lemmas.
for my $lemas (sort {length($b) <=> length($a)}(keys %lemmaHash))
{
#Ignores lemmas whose tags have mismatched.
if($lemmaHash{$lemas}[0] eq "TagMismatch"){next;}
#Skip if NE tag average probability is smaller than threshold.
if ($lemmaHash{$lemas}[3]/$lemmaHash{$lemas}[2] < $_[1]){next;}
for my $i (0 .. $#{$_[0]})
{
#Checks all untagged tokens.
if(${$_[0]}[$i][8] eq "O")
{
#Searches for tokens whose lemmas match NE tags first lemma.
if ($lemmaHash{$lemas}[1][0] eq ${$_[0]}[$i][2])
{
#Trailing lemmas mismatch indicator.
my $matches = 1;
#Compare the rest of NE tag's lemmas to trailing token lemmas.
for my $j (1 .. $#{$lemmaHash{$lemas}[1]})
{
if(defined ${$_[0]}[$i+$j])
{
if (${$_[0]}[$i+$j] ne $lemmaHash{$lemas}[1][$j]) {$matches=0; last;}
#Break if trailing token lemma is already tagged.
if(${$_[0]}[$i+$j][8] ne "O") {$matches=0; last;}
}
else {$matches=0; last;} #Stops comparing and saves a false value if a mismatch occurs.
}
if ($matches) # If NE tag lemmas match token lemmas adds NE tags to tokens.
{
#Gets the short NE tag name and adds "B-" to mark NE tag beginning.
${$_[0]}[$i][8] = "B-".NEUtilities::GetShortTagType($lemmaHash{$lemas}[0]);
#Gets the short NE tag name and adds "I-" to all trailing tokens with the same tag.
for my $j (1 .. $#{$lemmaHash{$lemas}[1]})
{
${$_[0]}[$i+$j][8] = "I-".NEUtilities::GetShortTagType($lemmaHash{$lemas}[0]);
}
}
}
}
}
}
}
#=========Method: CleanBracketsAndQuotations==========
#Title: CleanBracketsAndQuotations
#Description: Finds NE tags with brackets or quotation marks as tokens (argument 0). If the bracket or quotation mark is in the middle, the method tries to find nearly located opening or closing bracket or quotation mark (depending on the found one) token and tag the sequence till the quotation mark or bracket (including) as a part of the named entity.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 13.07.2011
#Last Changes: 13.07.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub CleanBracketsAndQuotations
{
#@tokens -a list of tokens (see desccription above)
my @tokens = @{$_[0]};
# %bracketHash =(
# [opening brakets] => [closing brakets]
#Usage: used to link starting brackets with closing brakes and to find out whether they are unopened or unclosed.
my $leftEgeLength = 3;
my %bracketHash =(
"[" => ["]",0],
"(" => [")",0],
"{" => ["}",0]
);
# %qouteMarkList =(
# [qoute mark] => [nothing]
#Usage: used to check if token is one of possible quote marks.
my %qouteMarkList =(
"\"" => "",
"\x{201C}" => "",
"\x{201D}" => "",
"\x{201E}" => "",
"\x{00AB}" => "",
"\x{00BB}" => "",
"\x{2033}" => ""
);
for my $i (0 .. $#tokens)
{
#Finds NE tag beggining.
if ($tokens[$i][8] =~ /^B/)
{
#$qouteMarks is used to count qoute marks in NE tag.
my $qouteMarks = 0;
#$NETagLenght is used in combination with $i to find out NE Tag position in @tokens array.
my $NETagLenght = 1;
#Looks for all brackets and increases the number asigned to the braket in hash by 1 if the current token is an opening bracket and decreases by 1 if it is a closing bracket. This is done to find missing opening or closing brackets.
for my $startBraketKey (keys %bracketHash)
{
if ($tokens[$i][0] eq $startBraketKey)
{
$bracketHash{$startBraketKey}[1]++;
}
if ($tokens[$i][0] eq $bracketHash{$startBraketKey}[0])
{
$bracketHash{$startBraketKey}[1]--;
}
}
#If the current tokens is a qoute mark increases $qouteMarks counter by one.
if(defined( $qouteMarkList{$tokens[$i][0]} )) {$qouteMarks ++;}
#If the first token in NE tag is not the last token look for trailing NE tokens and count quote marks and brackets in them.
if ($i+$NETagLenght <= $#tokens)
{
while ($tokens[$i+$NETagLenght][8] =~ /^I/)
{
#Looks for all brackets and increases the number asigned to the braket in hash by 1 if the current token is an opening bracket and decreases by 1 if it is a closing bracket. This is done to find missing opening or closing brackets.
for my $startBraketKey (keys %bracketHash)
{
if ($tokens[$i+$NETagLenght][0] eq $startBraketKey)
{
$bracketHash{$startBraketKey}[1]++;
}
if ($tokens[$i+$NETagLenght][0] eq $bracketHash{$startBraketKey}[0])
{
$bracketHash{$startBraketKey}[1]--;
}
}
#If the current tokens is a qoute mark increases $qouteMarks counter by one.
if(defined( $qouteMarkList{$tokens[$i+$NETagLenght][0]} )) {$qouteMarks ++;}
#Counts tokens in NE tag.
$NETagLenght++;
if($i+$NETagLenght >= $#tokens){last;}
}
#Decreases $NETagLenght to get accurate NE tag position.
$NETagLenght--;
#Handles bracket or quote mismatch cases.
#Counts both unclosed brakes and qoutes. If there is more than one unclosed braket or qoute remove the whole NE tag.
my $unclosedCount = 0;
for my $startBraketKey (keys %bracketHash)
{
#Counts absolute values to count both missing opening and closing brackets.
$unclosedCount += abs($bracketHash{$startBraketKey}[1]);
}
if ($qouteMarks % 2) {$unclosedCount++;}
if ($unclosedCount == 0){next;}
elsif($unclosedCount > 1)
{
#Removes The NE tag.
for my $g ($i .. ($i+$NETagLenght))
{
$tokens[$g][8] = "O";
}
next;
}
#Handles unopened or unclosed brackets.
for my $startBraketKey (keys %bracketHash)
{
#If there is a missing opening bracket:
if ($bracketHash{$startBraketKey}[1] > 0)
{
#Removes starting token from the NE tag if it is an opening bracket.
if ($tokens[$i][0] eq $startBraketKey)
{
if ($NETagLenght > 0)
{
$tokens[$i+1][8] = $tokens[$i][8];
}
$tokens[$i][8] = "O";
}
#Removes end token from the NE tag if it is an opening braked.
elsif ($tokens[$i + $NETagLenght][0] eq $startBraketKey)
{
$tokens[$i+$NETagLenght][8] = "O";
}
#Looks for a closing bracket to the right (in text) of the NE tag and if one is found add it to the NE tag.
else
{
my $goRight = 1;
#$foundMatch - false by default.
my $foundMatch = 0;
#Iterates trough the tokens to the right.
while (($i+$goRight+$NETagLenght) < $#tokens)
{
#Exits loop after pre-defined number ($leftEgeLength) of tokens have been checked.
if (($leftEgeLength-$goRight) <= 0) {last;}
#Exits the loop if the current token is inside another NE tag.
if ($tokens[$i+$goRight+$NETagLenght][8] ne "O") {last;}
#If current token is a closing bracket - save the position and exit loop.
if ($tokens[$i+$goRight+$NETagLenght][0] eq $bracketHash{$startBraketKey}[0])
{
$foundMatch = $i+$goRight+$NETagLenght; last;
}
#Go to next token.
$goRight++;
}
#If match found add it to the NE tag.
if ($foundMatch)
{
#Get NE tag type and replace the starting NE token with trailing NE token.
my $tokenNEtag = $tokens[$i][8];
$tokenNEtag =~s/^B/I/;
#Add token until the closing bracket to the NE tag.
for my $z (1 .. ($foundMatch-$NETagLenght-$i))
{
$tokens[$i+$z+$NETagLenght][8] = $tokenNEtag;
}
}
}
}
#If there is a missing closing bracket:
elsif ($bracketHash{$startBraketKey}[1] < 0)
{
#Removes starting token from the NE tag if it is an closing bracket.
if ($tokens[$i][0] eq $bracketHash{$startBraketKey})
{
if ($NETagLenght > 0)
{
$tokens[$i+1][8] = $tokens[$i][8];
}
$tokens[$i][8] = "O";
}
#Removes starting token from the NE tag if it is an closing bracket.
elsif ($tokens[$i + $NETagLenght][0] eq $bracketHash{$startBraketKey})
{
$tokens[$i+$NETagLenght][8] = "O";
}
#Looks for an opening bracket to the right (in text) of the NE tag and if one is found add it to the NE tag.
else
{
my $goLeft = -1;
my $foundMatch = -1;
#Iterates trough the tokens to the left.
while (($i+$goLeft)>=0)
{
#Exits loop after pre-defined number ($leftEgeLength) of tokens have been checked.
if (($goLeft+$leftEgeLength) <= 0) {last;}
#Exits the loop if the current token is inside another NE tag.
if ($tokens[$i+$goLeft][8] ne "O") {last;}
#If current token is a closing bracket - save the position and exit loop.
if ($tokens[$i+$goLeft][0] eq $startBraketKey) {$foundMatch = $i+$goLeft}
$goLeft--;
}
#If match found add it to the NE tag.
if ($foundMatch != -1)
{
#Get NE tag type and replace the starting NE token with trailing NE token.
my $tokenNEtag = $tokens[$i][8];
$tokenNEtag =~ s/^B/I/ ;
for my $z (($foundMatch-$i) .. 0 )
{
#Add a starting NE token ($tokens[$i][8] - NE the beginning before changes) to the first token.
if($z == ($foundMatch-$i)) {$tokens[$i+$z][8] = $tokens[$i][8]}
else
{
$tokens[$i+$z][8] = $tokenNEtag;
}
}
}
}
}
$bracketHash{$startBraketKey}[1] = 0;
}
#If there is an odd number of qoutes in NE tag:
if ($qouteMarks % 2)
{
#If both starting tag and end tags are quote marks remove the NE tag from the token that has another quote mark next to it.
if ( (defined( $qouteMarkList{$tokens[$i+$NETagLenght][0]})) && (defined( $qouteMarkList{$tokens[$i][0]} )) )
{
if ($NETagLenght == 0){ $tokens[$i+$NETagLenght][8] = "O"; }
else
{
if($tokens[$i][0] eq $tokens[$i+1][0])
{
$tokens[$i+1][8] = $tokens[$i][8];
$tokens[$i][8] = "O";
}
else
{
$tokens[$i+$NETagLenght][8] = "O";
}
}
}
#.
elsif (defined( $qouteMarkList{$tokens[$i+$NETagLenght][0]} ))
{
$tokens[$i+$NETagLenght][8] = "O";
}
#Removes starting token from the NE tag if it is a quote mark.
elsif (defined( $qouteMarkList{$tokens[$i][0]} ))
{
if ($NETagLenght > 0)
{
$tokens[$i+1][8] = $tokens[$i][8];
}
$tokens[$i][8] = "O";
}
#Looks for a quote mark to the right (in text) of the NE tag and if one is found add it to the NE tag.
#If one is not found looks for one left (in text) (done the same way as with the brackets).
else
{
my $goRight = 1;
my $foundRightMatch = 0;
while (($i+$goRight+$NETagLenght)<$#tokens)
{
if (($leftEgeLength-$goRight) <= 0) {last;}
if ($tokens[$i+$goRight+$NETagLenght][8] ne "O") {last;}
if (defined( $qouteMarkList{$tokens[$i+$goRight+$NETagLenght][0]} )) {$foundRightMatch = $i+$goRight+$NETagLenght; last;}
$goRight++;
}
if ($foundRightMatch)
{
my $tokenNEtag = $tokens[$i][8];
$tokenNEtag =~s/^B/I/;
for my $z (1 .. ($foundRightMatch-$NETagLenght-$i))
{
$tokens[$i+$z+$NETagLenght][8] = $tokenNEtag;
}
}
else
{
my $goLeft = -1;
my $foundLeftMatch = -1;
while (($i+$goLeft)>0)
{
if (($goLeft+$leftEgeLength) <= 0) {last;}
if ($tokens[$i+$goLeft][8] ne "O") {last;}
if (defined( $qouteMarkList{$tokens[$i+$goLeft][0]} )) {$foundLeftMatch = $i+$goLeft}
$goLeft--;
}
if ($foundLeftMatch != -1)
{
my $tokenNEtag = $tokens[$i][8];
$tokenNEtag =~ s/^B/I/ ;
for my $z (($foundLeftMatch-$i) .. 0 )
{
if($z == ($foundLeftMatch-$i)) {$tokens[$i+$z][8] = $tokens[$i][8]}
else
{
$tokens[$i+$z][8] = $tokenNEtag;
}
}
}
}
}
}
}
}
}
}
#=========Method: RemoveCorruptStringTokensFromNETags==========
#Title: RemoveCorruptStringTokensFromNETags
#Description: Removes tokens containing defined strings from NE tags if they are in the beginning or at the end of the NE tag. Removes the whole NE tag if in they are in the middle of the NE tag.
#Author: Kārlis Gediņš, SIA Tilde.
#Created: 14.07.2011
#Last Changes: 14.07.2011. by Kārlis Gediņš, SIA Tilde.
#===============================================
sub RemoveCorruptStringTokensFromNETags
{
#@tokens -a list of tokens (see desccription above)
my @tokens = @{$_[0]};
# @faultyStrings - strings to look for to remove NE tags.
my @faultyStrings = (":\/\/");
#Combines the array of multiple strings in to a single pattern.
my $patern = join ("\|",@faultyStrings);
#Gets full NE tagd positions.
for my $i (0 .. $#tokens)
{
if ($tokens[$i][8] =~ /^B/)
{
#$NETagLenght is used in combination with $i to find out NE Tag position in @tokens array.
my $NETagLenght = 1;
if ($i+$NETagLenght <= $#tokens)
{
while ($tokens[$i+$NETagLenght][8] =~ /^I/)
{
#Counts tokens in NE tag.
$NETagLenght++;
if($i+$NETagLenght >= $#tokens){last;}
}
#Reduces the $NETagLenght so that $tokens[$i + $NETagLenght] would be the last token in NE tag.
$NETagLenght--;
}
#Iterates trough tokens in NE tag. Looks for the string pattern in the token and removes the NE token or the whole NE tag according to the token position in NE tag.
for my $j ($i .. ($i+$NETagLenght))
{
#If matches one of the paterns.
if($tokens[$j][0] =~ /(?:$patern)/)
{
#Removes NE tag the form token if the token is at the beginning or end of the NE tag.
if($j == $i)
{
if ($NETagLenght > 0)
{
$tokens[$j+1][8] = $tokens[$j][8];
}
$tokens[$j][8] = "O";
}
elsif ($j ==($i+$NETagLenght) )
{
$tokens[$j][8] = "O";
}
#Removes the whole NE tag if the string is at the middle of the NE tag.
else