-
Notifications
You must be signed in to change notification settings - Fork 7
/
md-eval-v21.pl
2864 lines (2499 loc) · 105 KB
/
md-eval-v21.pl
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 -w
use strict;
#################################
# History:
#
# version 21: * JGF: added a flag '-n' to not remove the directory paths from the source
# files in the UEM file.
#
# version 20: * change metadata discard rule: rather than discard if the midpoint
# (or endpoint) of the metadata object lies in a no-eval zone, discard
# if there is ANY overlap whatsoever between the metadata object and
# a no-eval zone. This holds for system output objects only if the
# system output metadata object is not mapped to a ref object.
# * optimize IP and SU mapping by giving a secondary bonus mapping score
# to candidate ref-sys MD map pairs if the end-words of both coincide.
#
# version 19: * bug fix in subroutine speakers_match
# * bug fix in tag_ref_words_with_metadata_info
#
# version 18: * cosmetic fix to error message in eval_condition
# * added conditional output options for word coverage performance
# * added secondary MD word coverage optimization to word alignment
# * further optimize word alignment by considering MD subtypes
# * further optimize MD alignment by considering MD subtypes
# * add a new SU discard rule: discard if TEND in no-eval zone
# * enforce legal values for su_extent_limit
#
# version 17: create_speaker_segs modified to accommodate the same speaker
# having multiple overlapping speaker segments. (This is an
# error and pathological condition, but the system must either
# disallow (abort on) the condition, or perform properly under
# the pathological condition. The second option is chosen.)
#
# version 16: * If neither -w nor -W is specified, suppress warnings about
# ref SPEAKER records subsuming no lexemes.
# * Output the overall speaker diarization stats after the
# stats for the individual files
# * Do not alter the case of alphabetic characters in the filename
# field from the ref rttm file
# * Made the format of the overall speaker error line more similar to
# the corresponding line of output from SpkrSegEval, to facilitate
# use of existing "grep" commands in existing scripts.
#
# version 15: * bug fix in create_speaker_segs to accommodate
# contiguous same-speaker segments
# * added conditional file/channel scoring to
# speaker diarization evaluation
#
# version 14: bug fix in md_score
#
# version 13: add DISCOURSE_RESPONSE as a FILLER subtype
#
# version 12: make REF LEXEMES optional if they aren't required
#
# version 11: change default for noscore MD regions
#
# version 10: bug fix
#
# version 09:
# * avoid crash when metadata discard yields no metadata
# * make evaluated ref_wds sensitive to metadata type
# * defer discarding of system output metadata until after
# metadata mapping, then discard only unmapped events.
# * extend 1-speaker scoring inhibition to metadata
# * eliminate demand for SPKR-INFO subtype for speakers
# * correct ref count of IP and SU exact boundary words
# * add official RT-04F scores
# * add conditional analyses for file/chnl/spkr/gender
#
# version 08:
# * bug fixes speaker diarization scoring
# - count of EVAL_WORDS corrected
# - no-score extended to nearest SPEAKER boundary
#
# version 07:
# * warning issued when discarding metadata events
# that cover LEXEMEs in the evaluation region
#
# version 06:
# * eliminated unused speakers from speaker scoring
# * changed discard algorithm for unannotated SU's and
# complex EDIT's to discard sys SU's and EDIT's when
# their midpoints overlap (rather than ANY overlap).
# * fixed display_metadata_mapping
#
# version 05:
# * upgraded display_metadata_mapping
#
# version 04:
# * diagnostic metadata mapping output added
# * uem_from_rttm bug fix
#
# version 03:
# * adjusted times used for speaker diarization
# * changed usage of max_extend to agree with cookbook
#
# version 02: speaker diarization evaluation added
#
# version 01: a merged version of df-eval-v14 and su-eval-v16
#
#################################
#global data
my $epsilon = 1E-8;
my $miss_name = " MISS";
my $fa_name = " FALSE ALARM";
my %rttm_datatypes = (SEGMENT => {eval => 1, "<na>" => 1},
NOSCORE => {"<na>" => 1},
NO_RT_METADATA => {"<na>" => 1},
LEXEME => {lex => 1, fp => 1, frag => 1, "un-lex" => 1,
"for-lex" => 1, alpha => 1, acronym => 1,
interjection => 1, propernoun => 1, other => 1},
"NON-LEX" => {laugh => 1, breath => 1, lipsmack => 1,
cough => 1, sneeze => 1, other => 1},
"NON-SPEECH" => {noise => 1, music => 1, other => 1},
FILLER => {filled_pause => 1, discourse_marker => 1,
discourse_response => 1, explicit_editing_term => 1,
other => 1},
EDIT => {repetition => 1, restart => 1, revision => 1,
simple => 1, complex => 1, other => 1},
IP => {edit => 1, filler => 1, "edit&filler" => 1,
other => 1},
SU => {statement => 1, backchannel => 1, question => 1,
incomplete => 1, unannotated => 1, other => 1},
CB => {coordinating => 1, clausal => 1, other => 1},
"A/P" => {"<na>" => 1},
SPEAKER => {"<na>" => 1},
"SPKR-INFO" => {adult_male => 1, adult_female => 1, child => 1, unknown => 1});
my %md_subtypes = (FILLER => $rttm_datatypes{FILLER},
EDIT => $rttm_datatypes{EDIT},
IP => $rttm_datatypes{IP},
SU => $rttm_datatypes{SU});
my %spkr_subtypes = (adult_male => 1, adult_female => 1, child => 1, unknown => 1);
my $noeval_mds = {
DEFAULT => {
NOSCORE => {"<na>" => 1},
NO_RT_METADATA => {"<na>" => 1},
},
};
my $noscore_mds = {
DEFAULT => {
NOSCORE => {"<na>" => 1},
LEXEME => {"un-lex" => 1},
SU => {unannotated => 1},
},
MIN => {
NOSCORE => {"<na>" => 1},
SU => {unannotated => 1},
},
FRAG_UNLEX => {
NOSCORE => {"<na>" => 1},
LEXEME => {frag => 1, "un-lex" => 1},
SU => {unannotated => 1},
},
FRAG => {
NOSCORE => {"<na>" => 1},
LEXEME => {frag => 1},
SU => {unannotated => 1},
},
NONE => {
},
};
my $noeval_sds = {
DEFAULT => {
NOSCORE => {"<na>" => 1},
},
};
my $noscore_sds = {
DEFAULT => {
NOSCORE => {"<na>" => 1},
"NON-LEX" => {laugh => 1, breath => 1, lipsmack => 1,
cough => 1, sneeze => 1, other => 1},
},
};
my %speaker_map;
my $default_extend = 0.50; #the maximum time (in seconds) to extend a no-score zone
my $default_collar = 0.00; #the no-score collar (in +/- seconds) to attach to SPEAKER boundaries
my $default_tgap = 1.00; #the max gap (in seconds) between matching ref/sys words
my $default_Tgap = 1.00; #the max gap (in seconds) between matching ref/sys metadata events
my $default_Wgap = 0.10; #the max gap (in words) between matching ref/sys metadata events
my $default_su_time_limit = 0.50; #the max extent (in seconds) to match for SU's
my $default_su_word_limit = 2.00; #the max extent (in words) to match for SU's
my $default_word_delta_score = 10.0; #the max delta score for word-based DP alignment of ref/sys words
my $default_time_delta_score = 1.00; #the max delta score for time-based DP alignment of ref/sys words
my $usage = "\n\nUsage: $0 [-h] -r <ref_file> -s <src_file>\n\n".
"Description: md-eval evaluates EARS metadata detection performance\n".
" by comparing system metadata output data with reference data\n".
"INPUT:\n".
" -R <ref-list> A file containing a list of the reference metadata files\n".
" being evaluated, in RTTM format. If the word-mediated alignment\n".
" option is used then this data must include reference STT data\n".
" in addition to the metadata being evaluated.\n".
" OR\n".
" -r <ref-file> A file containing reference metadata, in RTTM format\n\n".
" -S <sys-list> A file containing a list of the system output metadata\n".
" files to be evaluated, in RTTM format. If the word-mediated\n".
" alignment option is used then this data must include system STT\n".
" output data in addition to the metadata to be evaluated.\n".
" OR\n".
" -s <sys-file> A file containin system output metadata, in RTTM format\n\n".
" input options:\n".
" -x to include complex edits in the analysis and scoring.\n".
" -w for word-mediated alignment.\n".
" * The default (time-mediated) alignment aligns ref and sys metadata\n".
" according to the time overlap of the original ref and sys metadata\n".
" time intervals.\n".
" * Word-mediated alignment aligns ref and sys metadata according to\n".
" the alignment of the words that are subsumed within the metadata\n".
" time intervals.\n".
" -W for word-optimized mapping.\n".
" * The default (time-optimized) mapping maps ref and sys metadata\n".
" so as to maximize the time overlap of mapped metadata events.\n".
" * Word-optimized mapping maps ref and sys metadata so as to\n".
" maximize the overlap in terms of the number of reference words\n".
" that are subsumed within the overlapping time interval.\n".
" -a <cfgs> Conditional analysis options for metadata detection performance:\n".
" c for performance versus channel,\n".
" f for performance versus file,\n".
" g for performance versus gender, and\n".
" s for performance versus speaker.\n".
" -A <cf> Conditional analysis options for word coverage performance:\n".
" c for performance versus channel,\n".
" f for performance versus file,\n".
" -t <time gap> The maximum time gap allowed between matching reference\n".
" and system output words (in seconds). Default value is $default_tgap.\n".
" -T <time gap> The maximum time gap allowed between matching reference\n".
" and system output metadata (in seconds). Default value is $default_Tgap.\n".
" -l <SU extent limit> The maximum SU extent used to compute overlap\n".
" between reference and system output SU's. For time-optimized SU\n".
" mapping this is the maximum time extent. For word-optimized SU\n".
" mapping (using the -W option) this is the maximum number of words.\n".
" SU extent is limited to the last part of the SU. Default value is\n".
" $default_su_time_limit for time-optimized mapping, $default_su_word_limit for word-optimized mapping.\n".
" -u <uem-file> A file containing the evaluation partitions,\n".
" in UEM format.\n".
" -g <glm-file> A file containing word transformations used to\n".
" standardize the representation of words.\n".
" -o to include overlapping speech in MD evaluation. With this option,\n".
" separate recognition passes are made for each reference speaker.\n".
" -c <collar> is the no-score zone around reference speaker segment\n".
" boundaries. (Speaker Diarization output is not evaluated within\n".
" +/- collar seconds of a reference speaker segment boundary.)\n".
" Default value is $default_collar seconds.\n".
" -1 to limit scoring to those time regions in which only a single\n".
" speaker is speaking\n".
" -y <name> to select named no-eval conditions for metadata\n".
" -Y <name> to select named no-score conditions for metadata\n".
" -z <name> to select named no-eval conditions for speaker diarization\n".
" -Z <name> to select named no-score conditions for speaker diarization\n".
" -e to examine metadata mapping\n".
" -d to print word alignment and error calculation details\n".
" -D to print metadata event alignment and error calculation details\n".
" -m to print speaker mapping details for speaker diarization\n".
" -v to print the event sequence for each diarization source file\n".
" -n to keep the directory names of the UEM source file entries\n".
"OUTPUT:\n".
" Performance statistics are written to STDOUT.\n".
"\n";
######
# Intro
my ($date, $time) = date_time_stamp();
print "command line (run on $date at $time): ", $0, " ", join(" ", @ARGV), "\n";
use vars qw ($opt_h $opt_w $opt_W $opt_d $opt_D $opt_R $opt_r $opt_S $opt_s $opt_l $opt_c $opt_x);
use vars qw ($opt_t $opt_T $opt_g $opt_p $opt_P $opt_o $opt_a $opt_A $opt_u $opt_1 $opt_m $opt_v $opt_e);
use vars qw ($opt_y $opt_Y $opt_z $opt_Z $opt_n);
$opt_y = $opt_Y = $opt_z = $opt_Z = "DEFAULT";
use Getopt::Std;
getopts ('nhdDwWox1mvec:R:r:S:s:t:T:g:p:P:a:A:u:l:y:Y:z:Z:');
not defined $opt_h or die
"\n$usage";
defined $opt_r or defined $opt_R or die
"\nCOMMAND LINE ERROR: no reference data specified$usage";
not defined $opt_r or not defined $opt_R or die
"\nCOMMAND LINE ERROR: both reference file list and reference file specified$usage";
defined $opt_s or defined $opt_S or die
"\nCOMMAND LINE ERROR: no system output data specified$usage";
not defined $opt_s or not defined $opt_S or die
"\nCOMMAND LINE ERROR: both system output file list and system output file specified$usage";
my $word_gap = defined $opt_t ? $opt_t : $default_tgap;
my $md_gap = $opt_W ? $default_Wgap : (defined $opt_T ? $opt_T : $default_Tgap);
my $su_extent_limit = defined $opt_l ? $opt_l :
($opt_W ? $default_su_word_limit : $default_su_time_limit);
$opt_W ? ($su_extent_limit >= 1 or die "\nCOMMAND LINE ERROR: SU extent limit must be at least 1 for word-based MD alignment$usage") :
($su_extent_limit > 0 or die "\nCOMMAND LINE ERROR: SU extent limit must be positive for time-based MD alignment$usage");
my $max_wd_delta_score = $opt_w ? $default_word_delta_score : $default_time_delta_score;
$max_wd_delta_score = $opt_p if defined $opt_p;
my $max_md_delta_score = $opt_W ? $default_word_delta_score : $default_time_delta_score;
$max_md_delta_score = $opt_P if defined $opt_P;
my $collar = defined($opt_c) ? $opt_c : $default_collar;
$collar >= 0 or die
"\nCOMMAND LINE ERROR: Speaker Diarization scoring collar ('$collar') must be non-negative$usage";
my $max_extend = $default_extend;
$opt_a = "" unless defined $opt_a;
$opt_A = "" unless defined $opt_A;
my $noeval_md = eval_condition ($opt_y, $noeval_mds, "no-eval", "metadata");
my $noscore_md = eval_condition ($opt_Y, $noscore_mds, "no-score", "metadata");
my $noeval_sd = eval_condition ($opt_z, $noeval_sds, "no-score", "speaker diarization");
my $noscore_sd = eval_condition ($opt_Z, $noscore_sds, "no-score", "speaker diarization");
my %type_order = (NOSCORE => 0,
NO_RT_METADATA => 1,
SEGMENT => 2,
SPEAKER => 3,
SU => 4,
"A/P" => 5,
"NON-SPEECH" => 6,
EDIT => 7,
FILLER => 8,
IP => 9,
CB => 10,
"NON-LEX" => 11,
LEXEME => 12);
my %event_order = (END => 0,
MID => 1,
BEG => 2);
my %source_order = (REF => 0,
SYS => 1);
{
my (%ref, %sys, $glm, $uem);
print_parameters ();
($glm) = get_glm_data ($opt_g);
get_rttm_file (\%ref, $opt_r, $glm);
get_rttm_data (\%ref, $opt_R, $glm);
get_rttm_file (\%sys, $opt_s, $glm);
get_rttm_data (\%sys, $opt_S, $glm);
($uem) = get_uem_data ($opt_u, $opt_n);
evaluate (\%ref, \%sys, $uem);
}
#################################
sub eval_condition {
my ($name, $conditions, $exclusion, $evaluation) = @_;
$name = "DEFAULT" unless $name;
return $conditions->{$name} if defined $conditions->{$name};
print STDERR "\nCOMMAND LINE ERROR: unknown name ($name) of $exclusion conditions for $evaluation\n".
" available $exclusion conditions for $evaluation are:\n\n";
foreach $name (sort keys %$conditions) {
printf STDERR "%-24stype subtype\n", " for \"$name\":";
foreach my $type (sort keys %{$conditions->{$name}}) {
foreach my $subt (sort keys %{$conditions->{$name}{$type}}) {
printf STDERR "%28s %s\n", $type, $subt;
}
}
print "\n";
}
die "$usage";
}
#################################
sub print_parameters {
print $opt_w ? "\nWord-based metadata alignment, max gap between matching words = $word_gap sec\n" :
"\nTime-based metadata alignment\n";
print "\nMetadata evaluation parameters:\n";
$opt_W ? (print " word-optimized metadata mapping\n".
" max gap between matching metadata events = $md_gap words\n".
" max extent to match for SU's = $su_extent_limit words\n") :
(print " time-optimized metadata mapping\n".
" max gap between matching metadata events = $md_gap sec\n".
" max extent to match for SU's = $su_extent_limit sec\n");
print "\nSpeaker Diarization evaluation parameters:\n".
" The max time to extend no-score zones for NON-LEX exclusions is $max_extend sec\n".
" The no-score collar at SPEAKER boundaries is $collar sec\n";
printf "\nExclusion zones for evaluation and scoring are:\n".
" -----MetaData----- -----SpkrData-----\n".
" exclusion set name:%12s%11s%15s%11s\n".
" token type/subtype no-eval no-score no-eval no-score\n",
$opt_y, $opt_Y, $opt_z, $opt_Z;
print " (UEM) X X\n";
foreach my $type (sort keys %rttm_datatypes) {
foreach my $subt (sort keys %{$rttm_datatypes{$type}}) {
next unless ($noeval_md->{$type}{$subt} or
$noscore_md->{$type}{$subt} or
$noeval_sd->{$type}{$subt} or
$noscore_sd->{$type}{$subt});
printf "%15s/%-14s", $type, $subt;
printf "%3s", $noeval_md->{$type}{$subt} ? "X" : "";
printf "%10s", $noscore_md->{$type}{$subt} ? "X" : "";
printf "%16s", $noeval_sd->{$type}{$subt} ? "X" : "";
printf "%10s\n", $noscore_sd->{$type}{$subt} ? "X" : "";
}
}
}
#################################
sub get_glm_data {
my ($file) = @_;
my ($record, @fields, $word, %words, %data);
return unless defined $file;
open DATA, $file or die
"\nCOMMAND LINE ERROR: unable to open glm file '$file'$usage";
while ($record = <DATA>) {
next if $record =~ /^\s*$/;
next if $record =~ /^\s*(\[|\*|\%|\;)/;
@fields = split /\s+=>\s+/, lc $record;
shift @fields if $fields[0] eq "";
next unless @fields > 1;
$fields[0] =~ s/^\s+//;
$fields[1] =~ s/[^a-z-'_ \.].*//;
next if $fields[0] =~ /^\s*$/ or $fields[1] =~ /^\s*$/;
$data{$fields[0]} = [split /\s+/, $fields[1]];
}
close DATA;
return {%data};
}
#################################
sub get_uem_data {
my ($file, $keepDirectoryPath) = @_;
my ($record, @fields, $seg, $chnl, %data);
return unless defined $file;
open DATA, $file or die
"\nCOMMAND LINE ERROR: unable to open uem file '$file'$usage";
while ($record = <DATA>) {
next if $record =~ /^\s*[\#;]|^\s*$/;
@fields = split /\s+/, $record;
shift @fields if $fields[0] eq "";
@fields >= 4 or die
("\n\nFATAL ERROR: insufficient number of fields in UEM record\n".
" record is: '$record'\n\n");
undef $seg;
$seg->{FILE} = shift @fields;
$seg->{CHNL} = lc shift @fields;
$seg->{TBEG} = lc shift @fields;
$seg->{TEND} = lc shift @fields;
$seg->{FILE} =~ s/.*\/// if (! $keepDirectoryPath); #strip directory
$seg->{FILE} =~ s/\.[^.]*//; #strip file type
$seg->{TBEG} =~ s/[^0-9\.]//g; #strip non-numeric (commas)
$seg->{TEND} =~ s/[^0-9\.]//g; #strip non-numeric (commas)
push @{$data{$seg->{FILE}}{$seg->{CHNL}}}, $seg;
}
close DATA;
#sort and check data
foreach $file (keys %data) {
foreach $chnl (keys %{$data{$file}}) {
@{$data{$file}{$chnl}} =
sort {$a->{TBEG} <=> $b->{TBEG}} @{$data{$file}{$chnl}};
my $prev_seg;
foreach $seg (@{$data{$file}{$chnl}}) {
$seg->{TEND} > $seg->{TBEG} or die
"\n\nFATAL ERROR: non-positive evaluation segment length in UEM data for file $file, channel $chnl\n\n";
not defined $prev_seg or $seg->{TBEG} >= $prev_seg->{TEND} or die
("\n\nFATAL ERROR: UEM file has overlapping evaluation segments\n".
" file $file, channel $chnl: ($prev_seg->{TBEG},$prev_seg->{TEND}),".
" ($seg->{TBEG},$seg->{TEND})\n\n");
$prev_seg = $seg;
}
}
}
return {%data};
}
#################################
sub get_rttm_data {
my ($data, $list, $glm) = @_;
return unless defined $list;
open LIST, $list or die
"\nCOMMAND LINE ERROR: unable to open file list '$list'$usage";
while (my $file = <LIST>) {
get_rttm_file ($data, $file, $glm);
}
close LIST;
}
#################################
sub get_rttm_file {
my ($data, $rttm_file, $glm) = @_;
my ($record, @fields, $data_type, $file, $chnl, $word, @words, $token);
return unless defined $rttm_file;
open DATA, $rttm_file or die
"\nCOMMAND LINE ERROR: unable to open RTTM file '$rttm_file'$usage";
while ($record = <DATA>) {
next if $record =~ /^\s*[\#;]|^\s*$/;
@fields = split /\s+/, $record;
shift @fields if $fields[0] eq "";
@fields >= 9 or die
("\n\nFATAL ERROR: insufficient number of fields in RTTM file '$rttm_file'\n".
" input RTTM record is: '$record'\n\n");
$data_type = uc shift @fields;
undef $token;
$token->{TYPE} = $data_type;
$token->{FILE} = $file = shift @fields;
$token->{CHNL} = $chnl = lc shift @fields;
$token->{TBEG} = lc shift @fields;
$token->{TBEG} =~ s/\*//;
$token->{TDUR} = lc shift @fields;
$token->{TDUR} =~ s/\*//;
$token->{TDUR} = 0 if $token->{TDUR} eq "<na>";
$token->{TDUR} >= 0 or die
("\n\nFATAL ERROR -- negative metadata duration in file $file,'\n".
" input RTTM record is: '$record'\n\n");
$token->{WORD} = lc shift @fields;
$token->{SUBT} = lc shift @fields;
$rttm_datatypes{$token->{TYPE}}{$token->{SUBT}} or die
("\n\nFATAL ERROR: unknown RTTM data type/subtype ('$token->{TYPE}'/'$token->{SUBT}') in file $rttm_file\n".
" input RTTM record is: '$record'\n\n");
$token->{SPKR} = lc shift @fields;
$token->{CONF} = lc shift @fields;
$token->{CONF} = "-" unless defined $token->{CONF};
$token->{SPKR} = "<na>" unless defined $token->{SPKR};
if ($data_type eq "SPKR-INFO") {
not defined $data->{$file}{$chnl}{$data_type}{$token->{SPKR}} or die
("\n\nFATAL ERROR: multiple $data_type records for speaker $token->{SPKR} in file $file\n".
" input RTTM record is: '$record'\n\n");
defined $spkr_subtypes{$token->{SUBT}} or die
("\n\nFATAL ERROR: unknown $data_type subtype ($token->{SUBT}) in file '$file'\n".
" input RTTM record is: '$record'\n\n");
$data->{$file}{$chnl}{$data_type}{$token->{SPKR}}{GENDER} = $token->{SUBT};
}
else {
$token->{TEND} = $token->{TBEG}+$token->{TDUR};
$token->{TMID} = $token->{TBEG}+$token->{TDUR}/2;
}
if ($data_type eq "LEXEME") {
$token->{WTYP} = ($token->{SUBT} =~ /^fp$/ ? "fp" :
($token->{SUBT} =~ /^frag$/ ? "frag" :
($token->{SUBT} =~ /^un-lex$/ ? "un-lex" :
($token->{SUBT} =~ /^for-lex$/ ? "for-lex" : "lex"))));
@words = standardize_word ($token, $glm);
foreach $word (@words) {
push @{$data->{$file}{$chnl}{LEXEME}}, $word;
push @{$data->{$file}{$chnl}{RTTM}}, $word;
}
}
elsif ($data_type eq "SPEAKER") {
push @{$data->{$file}{$chnl}{SPEAKER}{$token->{SPKR}}}, $token;
push @{$data->{$file}{$chnl}{RTTM}}, $token;
}
elsif ($md_subtypes{$token->{TYPE}}) {
defined $md_subtypes{$token->{TYPE}}{$token->{SUBT}} or die
("\n\nFATAL ERROR: unknown $data_type subtype ($token->{SUBT}) in file '$file'\n".
" input RTTM record is: '$record'\n\n");
push @{$data->{$file}{$chnl}{$data_type}}, $token;
push @{$data->{$file}{$chnl}{RTTM}}, $token;
}
elsif ($data_type ne "SPKR-INFO") {
push @{$data->{$file}{$chnl}{RTTM}}, $token;
}
}
close DATA;
#sort and check data
foreach $file (keys %$data) {
foreach $chnl (keys %{$data->{$file}}) {
foreach $data_type (keys %{$data->{$file}{$chnl}}) {
next if $data_type eq "SPKR-INFO";
if ($data_type eq "SPEAKER") {
foreach my $spkr (keys %{$data->{$file}{$chnl}{$data_type}}) {
my $gender = $data->{$file}{$chnl}{"SPKR-INFO"}{$spkr}{GENDER};
$gender = $data->{$file}{$chnl}{"SPKR-INFO"}{$spkr}{GENDER} = "unknown" if not $gender;
@{$data->{$file}{$chnl}{$data_type}{$spkr}} =
sort {$a->{TMID}<=>$b->{TMID}} @{$data->{$file}{$chnl}{$data_type}{$spkr}};
my $prev_token;
foreach $token (@{$data->{$file}{$chnl}{$data_type}{$spkr}}) {
$token->{SUBT} = $gender;
next unless $prev_token;
not $prev_token or $token->{TBEG} >= $prev_token->{TEND}-$epsilon or die
("\n\nFATAL ERROR: RTTM file has overlapping $data_type tokens for speaker $spkr\n".
" in file $file, channel $chnl: ($prev_token->{TBEG},$prev_token->{TEND}),".
" ($token->{TBEG},$token->{TEND})\n\n");
$prev_token = $token;
}
}
}
else {
@{$data->{$file}{$chnl}{$data_type}} =
sort {$a->{TMID} <=> $b->{TMID}} @{$data->{$file}{$chnl}{$data_type}};
}
}
}
}
}
#################################
sub evaluate {
my ($ref_data, $sys_data, $uem_data) = @_;
my ($uem, $uem_sd_eval, $uem_sd_score, $uem_md_eval, $uem_md_score);
my ($ref_wds, $sys_wds, $ref_mds, $sys_mds, $type, %scores, $ref_rttm, $sys_rttm);
foreach my $file (sort keys %$ref_data) {
foreach my $chnl (sort keys %{$ref_data->{$file}}) {
$ref_rttm = $ref_data->{$file}{$chnl}{RTTM};
$sys_rttm = $sys_data->{$file}{$chnl}{RTTM};
$ref_wds = $ref_data->{$file}{$chnl}{LEXEME} ? $ref_data->{$file}{$chnl}{LEXEME} : [];
$sys_wds = $sys_data->{$file}{$chnl}{LEXEME} ? $sys_data->{$file}{$chnl}{LEXEME} : [];
$uem = $uem_data->{$file}{$chnl};
$uem = uem_from_rttm ($ref_rttm) if not defined $uem;
@$ref_wds > 0 or not $opt_w or die
"\n\nFATAL ERROR: no reference words for file '$file' and channel '$chnl'\n\n";
@$sys_wds > 0 or not $opt_w or die
"\n\nFATAL ERROR: no system output words for file '$file' and channel '$chnl'\n".
" Words are required for word-mediated alignment\n\n";
if ($ref_wds and ($opt_w or $opt_e)) {
tag_words_with_metadata_attributes ($ref_rttm, $ref_wds);
tag_words_with_metadata_attributes ($sys_rttm, $sys_wds);
perform_word_alignment ($file, $chnl, $ref_wds, $sys_wds, $uem);
}
$uem_md_eval = add_exclusion_zones_to_uem ($noeval_md, $uem, $ref_rttm);
$uem_md_score = add_exclusion_zones_to_uem ($noscore_md, $uem_md_eval, $ref_rttm);
$uem_md_score = exclude_overlapping_speech_from_uem ($uem_md_score, $ref_rttm) if $opt_1;
tag_scoreable_words ($ref_wds, $uem_md_score);
foreach $type (sort keys %md_subtypes) {
$ref_mds = $ref_data->{$file}{$chnl}{$type};
next unless defined $ref_mds;
@$ref_wds > 0 or die
"\n\nFATAL ERROR: no reference words for file '$file' and channel '$chnl'\n\n";
$sys_mds = $sys_data->{$file}{$chnl}{$type};
$sys_mds = $sys_data->{$file}{$chnl}{$type} = [] unless defined $sys_mds;
map_metadata_to_words ($sys_mds, $sys_wds, $ref_mds, $ref_wds);
discard_unevaluated_metadata ($uem_md_eval, $type, $ref_mds, $ref_wds, "REF");
next if @$ref_mds == 0;
align_data ($ref_mds, $sys_mds, "", \&md_score, $max_md_delta_score);
trace_best_path ($ref_mds, $sys_mds);
discard_metadata_subtype ("EDIT", "complex", $ref_mds, $sys_mds) if $type eq "EDIT" and $opt_x;
discard_metadata_subtype ("SU", "unannotated", $ref_mds, $sys_mds) if $type eq "SU";
discard_unevaluated_metadata ($uem_md_eval, $type, $sys_mds, $ref_wds, "SYS");
($scores{$type}{$file}{$chnl}) = score_metadata_path
($type, $file, $chnl, $ref_mds, $sys_mds, $ref_wds);
}
$ref_mds = $ref_data->{$file}{$chnl}{SPEAKER};
if (defined $ref_mds) {
@$ref_wds > 0 or not $opt_W or die
"\n\nFATAL ERROR: no reference words for file '$file' and channel '$chnl'\n\n";
$uem_sd_eval = add_exclusion_zones_to_uem ($noeval_sd, $uem, $ref_rttm);
$sys_mds = $sys_data->{$file}{$chnl}{SPEAKER};
$sys_mds = $sys_data->{$file}{$chnl}{SPEAKER} = {} unless defined $sys_mds;
map_spkrdata_to_words ($sys_mds, $sys_wds, $ref_mds, $ref_wds);
($scores{SPEAKER}{$file}{$chnl}) = score_speaker_diarization
($file, $chnl, $ref_mds, $sys_mds, $ref_wds, $uem_sd_eval, $ref_rttm);
}
if ($opt_e) {
discard_unevaluated_metadata ($uem, "LEXEME", $ref_rttm);
discard_unevaluated_metadata ($uem, "LEXEME", $sys_rttm);
discard_unevaluated_metadata ($uem_md_eval, "", $ref_rttm);
discard_metadata_subtype ("EDIT", "complex", $ref_rttm, $sys_rttm) if $opt_x;
discard_metadata_subtype ("SU", "unannotated", $ref_rttm, $sys_rttm);
discard_unevaluated_metadata ($uem_md_eval, "", $sys_rttm);
display_metadata_mapping ($file, $chnl, $ref_rttm, $sys_rttm, $ref_wds);
}
}
}
foreach $type (sort keys %md_subtypes) {
md_performance_analysis ($type, $scores{$type}, $md_subtypes{$type}, $ref_data)
if $scores{$type};
}
sd_performance_analysis ($scores{SPEAKER}, \%spkr_subtypes)
if $scores{SPEAKER};
}
#################################
sub perform_word_alignment {
my ($file, $chnl, $ref_wds, $sys_wds, $uem) = @_;
my @ref_wds = @$ref_wds;
my @sys_wds = @$sys_wds;
discard_unevaluated_words ($uem, \@ref_wds);
discard_unevaluated_words ($uem, \@sys_wds);
@ref_wds > 0 or die
"\n\nFATAL ERROR: no reference words in UEM portion of file '$file' and channel '$chnl'\n\n";
@sys_wds > 0 or not $opt_w or die
"\n\nFATAL ERROR: no system output words in UEM portion of file '$file' and channel '$chnl'\n".
" Words are required for word-mediated alignment\n\n";
return unless @sys_wds > 0;
if ($opt_o) {
foreach my $spkr (word_kinds ($ref_wds, "SPKR")) {
align_data ($ref_wds, $sys_wds, $spkr, \&word_score, $max_wd_delta_score);
trace_best_path ($ref_wds, $sys_wds, $spkr);
}
decide_who_spoke_the_words ($ref_wds, $sys_wds);
}
else {
align_data ($ref_wds, $sys_wds, "", \&word_score, $max_wd_delta_score);
trace_best_path ($ref_wds, $sys_wds);
}
#map system output word times to ref words
foreach my $wd (@$sys_wds) {
$wd->{RTBEG} = adjust_sys_time_to_ref ($wd->{TBEG}, $sys_wds);
$wd->{RTEND} = adjust_sys_time_to_ref ($wd->{TEND}, $sys_wds);
$wd->{RTDUR} = $wd->{RTEND} - $wd->{RTBEG};
$wd->{RTMID} = $wd->{RTBEG} + $wd->{RTDUR}/2;
}
score_word_path ($file, $chnl, $ref_wds, $sys_wds) if $opt_d;
}
################################
sub time_in_eval_partition {
my ($time, $uem_eval) = @_;
return 1 unless defined $uem_eval; #not using UEM partition specification
foreach my $partition (@$uem_eval) {
return 1 if event_covers_time ($partition, $time);
}
return 0;
}
#################################
sub discard_unevaluated_words {
my ($uem, $wds) = @_;
for (my $index=0; $index<@$wds; $index++) {
splice (@$wds, $index--, 1)
if ($wds->[$index]{TYPE} eq "LEXEME" and
not time_in_eval_partition ($wds->[$index]{TMID}, $uem));
}
}
#################################
sub discard_unevaluated_metadata {
my ($uem_eval, $type, $mds, $ref_wds, $src) = @_;
for (my $index=0; $index<@$mds; $index++) {
my $md = $mds->[$index];
next if (($type and $md->{TYPE} ne $type) or
(not $type and not $md_subtypes{$md->{TYPE}}) or
$md->{MAPPTR} or
md_in_uem ($md, $uem_eval));
warn_if_discarded_md_covers_scored_lexemes ($md, $ref_wds, $uem_eval, $src) if $ref_wds;
splice (@$mds, $index--, 1);
}
}
#################################
sub warn_if_discarded_md_covers_scored_lexemes {
my ($md, $ref_wds, $uem, $source) = @_;
my ($wbeg, $wend, $index);
($wbeg, $wend) = md_word_indices ($md, $ref_wds);
for ($index=$wbeg; $index<=$wend; $index++) {
next unless ($ref_wds->[$index]{SCOREABLE} and
time_in_eval_partition ($ref_wds->[$index]{TMID}, $uem));
warn "\nWARNING: A $source metadata event is being deleted that covers evaluated reference LEXEMEs\n".
" (type=$md->{TYPE}, subtype=$md->{SUBT}, spkr=$md->{SPKR}, TBEG=$md->{TBEG}, TEND=$md->{TEND})\n";
last;
}
}
#################################
sub discard_metadata_subtype {
my ($type, $subtype, $ref_mds, $sys_mds) = @_;
my ($iref, $isys, $ref_md, $sys_md);
#discard all sys $type events that map to a ref event with subtype = $subtype
#or that are unmapped and have midpoints that lie within a ref event with subtype = $subtype
for ($iref=0; $iref<@$ref_mds; $iref++) {
$ref_md = $ref_mds->[$iref];
next unless ($ref_md->{TYPE} eq $type and
$ref_md->{SUBT} eq $subtype);
for ($isys=0; $isys<@$sys_mds; $isys++) {
$sys_md = $sys_mds->[$isys];
splice (@$sys_mds, $isys--, 1)
if ($sys_md->{TYPE} eq $type and
(($sys_md->{MAPPTR} and $sys_md->{MAPPTR}{SUBT} eq $subtype) or
(not $sys_md->{MAPPTR} and event_covers_time ($ref_md, $sys_md->{RTMID}))));
}
#discard all ref $type/$subtype events
splice (@$ref_mds, $iref--, 1);
}
}
#################################
sub tag_scoreable_words {
my ($wds, $uem_eval) = @_;
foreach my $wd (@$wds) {
$wd->{SCOREABLE} = time_in_eval_partition ($wd->{TMID}, $uem_eval);
}
}
#################################
sub tag_words_with_metadata_attributes {
my ($mds, $wds) = @_;
my ($md, $iwbeg, $iwend, $iw, $wd, $type);
foreach $md (@$mds) {
$type = $md->{TYPE};
next unless $type =~ /^(FILLER|EDIT|SU|IP)$/;
($iwbeg, $iwend) = md_word_indices ($md, $wds);
if ($type =~ /^(FILLER|EDIT)$/) {
for ($iw=$iwbeg; $iw<=$iwend; $iw++) {
$wds->[$iw]{ATTRIBUTES}{$md->{TYPE}} = $md->{SUBT};
}
}
elsif ($type =~ /^(SU|IP)$/) {
$wds->[$iwend]{ATTRIBUTES}{$md->{TYPE}} = $md->{SUBT};
}
}
return;
}
#################################
sub tag_ref_words_with_metadata_info {
my ($mds, $wds, $src) = @_;
my ($md, $iwbeg, $iwend, $iw, $type);
foreach $md (@$mds) {
$type = $md->{TYPE};
($iwbeg, $iwend) = $src eq "REF" ?
($md->{WBEG}, $md->{WEND}) : ($md->{RWBEG}, $md->{RWEND}) ;
if ($type =~ /^(FILLER|EDIT)$/) {
for ($iw=max($iwbeg,0); $iw<=min($iwend,@$wds-1); $iw++) {
$wds->[$iw]{"$src-$type"}{$md->{SUBT}}{MAP}++;
}
}
elsif ($type =~ /^(SU|IP)$/) {
$iwend = min(max($iwend,0),@$wds-1);
$wds->[$iwend]{"$src-$type"}{$md->{SUBT}}{defined $md->{MAPPTR} ? "MAP" : "NOT"}++;
}
}
return;
}
#################################
sub md_performance_analysis {
my ($metadata_type, $counts, $subtypes, $ref_data) = @_;
my ($file, $chnl, $spkr, $word, $type, $type_counts, $key);
my (@files, @chnls, @spkrs, @types, %nevent, %nwerr);
my ($subtype, $sys_subtype, %nconf, %offsets);
#compute marginal counts
@files = keys %$counts;
foreach $file (@files) {
@chnls = keys %{$counts->{$file}};
foreach $chnl (@chnls) {
$type_counts = $counts->{$file}{$chnl};
foreach $type ("REF", "DEL", "INS", "SUB") {
next unless defined $type_counts->{WORDS}{$type};
$nwerr{ALL}{$type} += $type_counts->{WORDS}{$type};
$nwerr{"c=$chnl f=$file"}{$type} += $type_counts->{WORDS}{$type} if $opt_A =~ /c/i and $opt_A =~ /f/i;
$nwerr{"c=$chnl"}{$type} += $type_counts->{WORDS}{$type} if $opt_A =~ /c/i and not $opt_A =~ /f/i;
$nwerr{"f=$file"}{$type} += $type_counts->{WORDS}{$type} if $opt_A =~ /f/i and not $opt_A =~ /c/i;
}
foreach $type ("WBEG", "WEND") {
foreach $key (keys %{$type_counts->{WORD_OFFSET}{$type}}) {
$offsets{ALL}{$type}{$key} += $type_counts->{WORD_OFFSET}{$type}{$key};
}
}
my $spkr_info = $ref_data->{$file}{$chnl}{"SPKR-INFO"};
$spkr_info->{unknown}{GENDER} = "unknown" unless defined $spkr_info->{unknown};
foreach $type (keys %$type_counts) {
next unless $type =~ /^(REF|DEL|INS|SUB|CONFUSION)$/;
@spkrs = keys %{$type_counts->{$type}};
foreach $spkr (@spkrs) {
my $gndr = $spkr_info->{$spkr}{GENDER};
foreach $subtype (keys %$subtypes) {
my $count = $type_counts->{$type}{$spkr}{$subtype};
next unless $count;
if ($type eq "CONFUSION") {
foreach $sys_subtype (keys%$count) {
$nconf{ALL}{$subtype}{$sys_subtype} += $count->{$sys_subtype};
$nconf{ALL}{$subtype}{$sys_subtype} = 0 if not $nconf{ALL}{$subtype}{$sys_subtype};
$nconf{ALL}{$sys_subtype}{$subtype} = 0 if not $nconf{ALL}{$sys_subtype}{$subtype};
}
next;
}
$nconf{ALL}{$subtype}{"{Miss}"} += $count if $type eq "DEL";
$nconf{ALL}{"{FA}"}{$subtype} += $count if $type eq "INS";
$nconf{ALL}{$subtype}{"{Miss}"} = 0 unless defined $nconf{ALL}{$subtype}{"{Miss}"};
$nconf{ALL}{"{FA}"}{$subtype} = 0 unless defined $nconf{ALL}{"{FA}"}{$subtype};
$nevent{ALL}{$type} += $count;
$nevent{"c=$chnl f=$file"}{$type} += $count if $opt_a =~ /c/i and $opt_a =~ /f/i;
$nevent{"c=$chnl"}{$type} += $count if $opt_a =~ /c/i and not $opt_a =~ /f/i;
$nevent{"f=$file"}{$type} += $count if $opt_a =~ /f/i and not $opt_a =~ /c/i;
$nevent{"s=$spkr"}{$type} += $count if $opt_a =~ /s/i;
$nevent{"g=$gndr"}{$type} += $count if $opt_a =~ /g/i;
}
}
}
}
}
print_md_scores ($metadata_type, \%nevent, \%nconf, \%offsets, \%nwerr);
}
#################################
sub print_offset_stats {
my ($counts) = @_;
my (@offsets, $count, $min, $max, $i);
@offsets = (keys %{$counts->{WBEG}}, keys %{$counts->{WEND}});
$min = min (-3, @offsets);
$max = max (3, @offsets);
print " word offsets: <-3 ";
for ($i=-3; $i<=3; $i++) {
printf "%5d", $i;
}
print " >3\n";
print " BEG:";
for ($count=0,$i=$min; $i<-3; $i++) {
$count += $counts->{WBEG}{$i} if defined $counts->{WBEG}{$i};
}
printf "%5d ", $count if defined $count;
print " - ", unless defined $count;
for ($i=-3; $i<=3; $i++) {
$count = $counts->{WBEG}{$i};
printf "%5d", $count if defined $count;
print " -", unless defined $count;
}
for ($count=0,$i=4; $i<=$max; $i++) {
$count += $counts->{WBEG}{$i} if defined $counts->{WBEG}{$i};
}
printf "%7d", $count if defined $count;
print " -", unless defined $count;
print "\n END:";
for ($count=0,$i=$min; $i<-3; $i++) {
$count += $counts->{WEND}{$i} if defined $counts->{WEND}{$i};
}
printf "%5d ", $count if defined $count;
print " - ", unless defined $count;
for ($i=-3; $i<=3; $i++) {
$count = $counts->{WEND}{$i};
printf "%5d", $count if defined $count;
print " -", unless defined $count;
}
for ($count=0,$i=4; $i<=$max; $i++) {
$count += $counts->{WEND}{$i} if defined $counts->{WEND}{$i};
}
printf "%7d", $count if defined $count;
print " -", unless defined $count;
print "\n";
}
#################################
sub print_md_scores {
my ($metadata_type, $event_counts, $conf_counts, $offset_counts, $word_counts) = @_;
my ($type, $nerr, $norm, $name, $ref, $sys, $category, $counts);
my ($count, $min, $max, $i, @offsets);
my $head_format = "%36s %5s %5s %5s %6s %6s %6s %6s %6s\n";
my $data_format = "%-28.28s %5d %5d %5d %5s %6.2f %6.2f %6.2f %6.2f %6.2f\n";
my @header = ("Nref", "Ndel", "Nins", "Nsub", "%Del", "%Ins", "%Sub", "%D+I", "%Tot");
$counts = $word_counts->{ALL};
$nerr = $counts->{DEL} + $counts->{INS};
$nerr += $counts->{SUB} if $metadata_type =~ /^(SU|FILLER)$/;
printf "\n*** Performance analysis for %ss *** overall error SCORE = %.2f%s\n",
$metadata_type, 100*$nerr/max($counts->{REF},$epsilon), "%";
#metadata word detection
print "\nSU (exact) end detection statistics" if $metadata_type eq "SU";