Blame view

tools/sctk-2.4.10/bin/mergectm2rttm.pl 5.33 KB
8dcb6dfcb   Yannick Estève   first commit
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
  #!/usr/bin/perl -w
  
  # ALIGN2HTML
  # Author: Jerome Ajot
  #
  # This software was developed at the National Institute of Standards and Technology by 
  # employees of the Federal Government in the course of their official duties. Pursuant
  # to title 17 Section 105 of the United States Code this software is not subject to
  # copyright protection and is in the public domain. ALIGN2HTML is an experimental system.
  # NIST assumes no responsibility whatsoever for its use by other parties, and makes no
  # guarantees, expressed or implied, about its quality, reliability, or any other
  # characteristic. We would appreciate acknowledgement if the software is used.
  #
  # THIS SOFTWARE IS PROVIDED "AS IS."  With regard to this software, NIST MAKES NO EXPRESS
  # OR IMPLIED WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING MERCHANTABILITY,
  # OR FITNESS FOR A PARTICULAR PURPOSE.
  
  use strict;
  use Getopt::Long;
  use Data::Dumper;
  
  Getopt::Long::Configure(qw( auto_abbrev no_ignore_case ));
  
  my $VERSION = "0.4";
  
  my $RTTMFile = "";
  my $CTMFile = "";
  my $PATHTOOLS = "..";
  
  sub usage
  {
  	print "perl mergectm2rttm.pl [OPTION] -r rttmfile -c ctmfile > outputfile
  ";
  	print "
  ";
  	print "Required file arguments:
  ";
  	print "  -r, --rttmfile           Path to the RTTM file.
  ";
  	print "  -c, --ctmfile            Path to the CTM file.
  ";
  	print "Path arguments:
  ";
  	print "  -p, --pathtools          Path of the tools.
  ";
  	print "
  ";
  }
  
  sub loadRTTMFile
  {
  	my ($rttmFile, $rttmhash) = @_;
  	
  	open(RTTM, $rttmFile) or die "Unable to open for read RTTM file '$rttmFile'";
  	
  	while (<RTTM>)
  	{
  		chomp;
  		
  		# Remove comments which start with ;;
  		s/;;.*$//;
  		
  		# Remove unwanted space at the begining and at the end of the line
  		s/^\s*//;
  		s/\s*$//;
  		
  		# if the line is empty then ignore
  		next if ($_ =~ /^$/);
  		
  		my ($type, $file, $chnl, $tbeg, $tdur, $ortho, $stype, $spkrname, $conf) = split(/\s+/,$_,9);
  		
  		if(uc($type) eq "SPKR-INFO")
  		{
  		    $rttmhash->{SPKR}{$file}{$chnl}{$spkrname} = 1;
  		}
  		
  		if(uc($type) eq "SPEAKER")
  		{
  			push( @{ $rttmhash->{DATA}{$file}{$chnl}{$tbeg} }, ($tbeg, $tdur, $ortho, $stype, $spkrname, $conf));
  		}
  	}
  	
  	close RTTM;
  }
  
  sub loadCTMFile
  {
  	my ($ctmFile, $ctmhash) = @_;
  	
  	open(CTM, $ctmFile) or die "Unable to open for read CTM file '$ctmFile'";
  	
  	while (<CTM>)
  	{
  		chomp;
  		
  		# Remove comments which start with ;;
  		s/;;.*$//;
  		
  		# Remove unwanted space at the begining and at the end of the line
  		s/^\s*//;
  		s/\s*$//;
  		
  		# if the line is empty then ignore
  		next if ($_ =~ /^$/);
  		
  		my ($file, $chnl, $tbeg, $tdur, $ortho, $conf, $stype, $spkrname) = split(/\s+/,$_,8);
  		
  		if($stype eq "lex")
  		{
  			my $mid = sprintf("%.4f", $tbeg+$tdur/2);
  									
  		    push( @{ $ctmhash->{$file}{$chnl}{$tbeg} }, ($tbeg, $tdur, $ortho, $stype, $conf, $mid));
  		}
  	}
  	
  	close CTM;
  }
  
  sub findSpeaker
  {
  	my ($rttmhash, $file, $chnl, $mid) = @_;
  	my @listspkr;
  	
  	foreach my $tbeg(sort keys %{ $rttmhash->{DATA}{$file}{$chnl} })
  	{
  		my $bt = @{ $rttmhash->{DATA}{$file}{$chnl}{$tbeg} }[0];
  		my $dur = @{ $rttmhash->{DATA}{$file}{$chnl}{$tbeg} }[1];
  		my $spkrname = @{ $rttmhash->{DATA}{$file}{$chnl}{$tbeg} }[4];
  		
  		if( ($bt <= $mid) && ($mid <= sprintf("%.3f", $bt+$dur) ) )
  		{
  			push( @listspkr, [ ($spkrname, $tbeg) ] );
  		}
  	}
  	
  	if(scalar(@listspkr) == 0)
  	{
  		return undef;
  	}
  	else
  	{
  		return(\@{ $listspkr[int(rand(scalar(@listspkr)))] });
  	}
  }
  
  sub MergedData
  {
  	my ($rttmhash, $ctmhash, $file, $chnl) = @_;
  	
  	if (exists($ctmhash->{$file}{$chnl}) )
  	{
  		foreach my $tbeg(sort {$a <=> $b} keys %{ $ctmhash->{$file}{$chnl} })
  		{
  			my $mid = $ctmhash->{$file}{$chnl}{$tbeg}[5];
  		    my $spkrtbeg = findSpeaker($rttmhash, $file, $chnl, $mid);
  		    
  		    if($spkrtbeg)
  		    {
  		    	my $ctmbeg = $ctmhash->{$file}{$chnl}{$tbeg}[0];
  		    	my $ctmdur = $ctmhash->{$file}{$chnl}{$tbeg}[1];
  		    	my $ctmortho = $ctmhash->{$file}{$chnl}{$tbeg}[2];
  		    	my $ctmstype = $ctmhash->{$file}{$chnl}{$tbeg}[3];
  		    	my $ctmconf = $ctmhash->{$file}{$chnl}{$tbeg}[4];
  		    	my $ctmspkr = $spkrtbeg->[0];
  		    	
  		    	print OUTPUT "SPEAKER $file $chnl $ctmbeg $ctmdur <NA> <NA> $ctmspkr <NA>
  ";
  		    	print OUTPUT "LEXEME $file $chnl $ctmbeg $ctmdur $ctmortho $ctmstype $ctmspkr $ctmconf
  ";
  		    }
  		}
  	}
  }
  
  GetOptions
  (
      'rttmfile=s' => \$RTTMFile,
      'ctmfile=s'  => \$CTMFile,
      'pathtools=s'  => \$PATHTOOLS,
      'version'    => sub { print "mergectm2rttm version: $VERSION
  "; exit },
      'help'       => sub { usage (); exit },
  );
  
  die "ERROR: An RTTM file must be set." if($RTTMFile eq "");
  die "ERROR: An CTM file must be set." if($CTMFile eq "");
  
  my %RTTM;
  my %CTM;
  
  loadRTTMFile($RTTMFile, \%RTTM);
  loadCTMFile($CTMFile, \%CTM);
  
  my $RTTMSMOOTH = "$PATHTOOLS/rttmSmooth/rttmSmooth.pl";
  my $RTTMSORT = "$PATHTOOLS/rttmSort/rttmSort.pl";
  
  die "ERROR: 'rttmSmooth.pl' cannot be found, please specify the tools path with -p
  " if(! -e $RTTMSMOOTH);
  die "ERROR: 'rttmSort.pl' cannot be found, please specify the tools path with -p
  " if(! -e $RTTMSORT);
  
  open(OUTPUT, "| $RTTMSMOOTH | $RTTMSORT");
  
  print ";; type file chnl tbeg tdur ortho stype name conf
  ";
  
  foreach my $file(sort keys %{ $RTTM{SPKR} })
  {
  	foreach my $chnl(sort keys %{ $RTTM{SPKR}{$file} })
  	{
  		foreach my $spkrname(sort keys %{ $RTTM{SPKR}{$file}{$chnl} })
  		{
  			print OUTPUT "SPKR-INFO $file $chnl <NA> <NA> <NA> unknown $spkrname <NA>
  ";
  		}
  		
  		MergedData(\%RTTM, \%CTM, $file, $chnl);
  	}
  }
  
  close(OUTPUT);