hub4_96_parse_sgm.pl
6.88 KB
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
#!/usr/bin/env perl
###########################################################################################
# This script was copied from egs/hub4_english/s5/local/data_prep/parse_sgm_1996_hub4_eng.pl
# The source commit was 9f61a1b0efa76f37fc29fa2dbeede6dc776a0203
# No change was made
###########################################################################################
#===============================================================================
# Copyright (c) 2017 Johns Hopkins University (Author: Jan "Yenda" Trmal <jtrmal@gmail.com>)
# 2017 Vimal Manohar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
# WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
# MERCHANTABLITY OR NON-INFRINGEMENT.
# See the Apache 2 License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
use strict;
use warnings;
use utf8;
require HTML::Parser or die "This script needs HTML::Parser from CPAN";
HTML::Parser->import();
binmode(STDOUT, ":utf8");
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
sub parse_sgml_tag {
my $tag = shift(@_);
my %ret;
if ($tag !~ /=/) {
return %ret;
}
$tag =~ s/<[a-zA-Z]+ //;
$tag =~ s/> *$//;
#print $tag . "\n";
my @key_value_pairs = split / *,? +/, $tag;
for my $entry(@key_value_pairs) {
(my $key, my $value) = split '=', $entry, 2;
$ret{$key}=$value;
}
return %ret;
}
if (@ARGV != 1) {
print STDERR "$0: This script needs exactly one parameter (list of SGML files)\n";
print STDERR " Usage: $0 <transripts>\n";
print STDERR " where\n";
print STDERR " <transcripts> is a file containing the official SGML format\n";
print STDERR " transcripts. The files are parsed and the parsed representation\n";
print STDERR " is dumped to STDOUT (one utterance + the additional data fields\n";
print STDERR " per line (we dump all the fields, but not all fields are used\n";
print STDERR " in the recipe).\n";
die;
}
my $filelist=$ARGV[0];
my $p = HTML::Parser->new();
my @files=();
open(F, '<', $filelist) or die "Could not open file $filelist: $?\n";
while(<F>) {
chomp;
push @files, $_;
}
foreach my $file (@files) {
my $reporter="";
my $start = -1;
my $end = -1;
my $segment_start = -1;
my $segment_end = -1;
my $segment_speaker;
my $segment_fidelity = "XXX";
my $segment_mode = "XXX";
my $section_start = -1;
my $section_end = -1;
my $filename = "";
my $seq = 0;
my @text = ();
my $time;
my @tagqueue;
my $sgml_file = `basename $file`;
$sgml_file = trim $sgml_file;
$sgml_file =~ s/\.txt$//g;
$sgml_file =~ s/\.sgml$//g;
$sgml_file =~ s/_$//g;
open(my $f, '<:encoding(iso-8859-1)', $file) or die "Could not open file $file: $?\n";
while(my $line = <$f>) {
chomp $line;
$line = trim $line;
$line = lc $line;
next unless $line;
if ($line =~ /<episode/) {
my %tags = parse_sgml_tag $line;
$filename = $tags{'filename'};
$filename =~ s/"//g;
$filename =~ s/\.sph//g;
if ($sgml_file ne $filename) {
print STDERR "$0: WARNING: SGML filename does not match episode filename $filename in file $file\n";
}
#print "BS: $line\n";
push @tagqueue, ["episode", \%tags];
;
} elsif ($line =~ /<\/episode/) {
my $p = pop @tagqueue;
$line =~ s/<\/(.*)( +.*)?>/$1/g;
$line = trim $line;
die "Unaligned tags: '" . $p->[0] . "' vs '$line'" if $p->[0] ne $line;
#print "ES: $line\n";
;
} elsif ($line =~ /<section/) {
my %tags = parse_sgml_tag $line;
if ($tags{'s_time'}) {
$section_start = $tags{'s_time'};
} else {
$section_start = $tags{'starttime'};
}
if ($tags{'e_time'}) {
$section_end = $tags{'e_time'};
} else {
$section_end = $tags{'endtime'};
}
#print "BS: $line\n";
push @tagqueue, ["section", \%tags];
;
} elsif ($line =~ /<\/section/) {
my $p = pop @tagqueue;
$line =~ s/<\/(.*)( +.*)?>/$1/g;
$line = trim $line;
die "Unaligned tags: '" . $p->[0] . "' vs '$line'" if $p->[0] ne $line;
#print "ES: $line\n";
;
} elsif ($line =~ /<segment/) {
#print "BT: $line\n";
my %tags = parse_sgml_tag $line;
$segment_speaker = $tags{'speaker'};
$segment_speaker =~ s/"//g;
$segment_start = $tags{'s_time'};
$segment_end = $tags{'e_time'};
$segment_fidelity = $tags{'fidelity'} if $tags{'fidelity'};
$segment_mode = $tags{'mode'} if $tags{'mode'};
$time = $segment_start;
push @tagqueue, ["segment", \%tags];
;
} elsif ($line =~ /<\/segment/) {
my $p = pop @tagqueue;
$line =~ s/<\/(.*)( +.*)?>/$1/g;
$line = trim $line;
die "Unaligned tags: '" . $p->[0] . "' vs '$line'" if $p->[0] ne $line;
#print join(" ", @text) . "\n" if @text > 0;
my $new_time = $segment_end;
if (@text > 0) {
print "$sgml_file $filename $segment_speaker $segment_fidelity $segment_mode $time $new_time ";
print join(" ", @text) . "\n";
}
@text = ();
$time = 0;
$segment_speaker = "XXX";
$segment_start = "XXX";
$segment_end = "XXX";
$segment_fidelity = "XXX";
$segment_mode = "XXX";
#print "ET: $line\n";
;
} elsif ($line =~ /<sync/) {
my %tags = parse_sgml_tag $line;
my $new_time = $tags{'time'};
if (@text > 0) {
print "$sgml_file $filename $segment_speaker $segment_fidelity $segment_mode $time $new_time ";
print join(" ", @text) . "\n";
}
@text = ();
$time = $new_time;
;
} elsif ($line =~ /<\/sync/) {
#print $line;
;
} elsif ($line =~ /<overlap/) {
#print $line;
;
} elsif ($line =~ /<\/overlap/) {
#print $line;
;
} elsif ($line =~ /<background/ || $line =~ /<comment/) {
# ignore line;
} elsif ($line =~ /<foreign/) {
$line = trim $line;
push @text, $line;
} elsif ($line =~ /<\/foreign/) {
$line = trim $line;
push @text, $line;
;
} elsif ($line =~ /<unclear/) {
$line = trim $line;
push @text, $line;
} elsif ($line =~ /<\/unclear/) {
$line = trim $line;
push @text, $line;
;
} elsif ($line =~ /<[^\/]/) {
parse_sgml_tag $line;
print STDERR "$0: INFO: Unknown tag $line in file $file\n";
} elsif ($line =~ /<\//) {
;
} else {
$line = trim $line;
push @text, $line if $line;
;
}
}
close($f);
}