Blame view
egs/hub4_spanish/s5/local/parse_sgm.pl
5.68 KB
8dcb6dfcb 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 |
#!/usr/bin/env perl #=============================================================================== # Copyright (c) 2017 Johns Hopkins University (Author: Jan "Yenda" Trmal <jtrmal@gmail.com>) # # 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 . " "; 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) "; print STDERR " Usage: $0 <transripts> "; print STDERR " where "; print STDERR " <transcripts> is a file containing the official SGML format "; print STDERR " transcripts. The files are parsed and the parsed representation "; print STDERR " is dumped to STDOUT (one utterance + the additional data fields "; print STDERR " per line (we dump all the fields, but not all fields are used "; print STDERR " in the recipe). "; die; } my $filelist=$ARGV[0]; my $p = HTML::Parser->new(); my @files=(); open(F, '<', $filelist) or die "Could not open file $filelist: $? "; while(<F>) { chomp; push @files, $_; } foreach my $file (@files) { my $reporter=""; my $start = -1; my $end = -1; my $turn_start = -1; my $turn_end = -1; my $turn_speaker; my $turn_dialect = "unknown"; my $turn_sex; 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/\.sgm$//g; open(my $f, '<:encoding(iso-8859-1)', $file) or die "Could not open file $file: $? "; while(my $line = <$f>) { chomp $line; $line = trim $line; next unless $line; if ($line =~ /<episode/) { my %tags = parse_sgml_tag $line; $filename = $tags{'filename'}; $filename =~ s/"//g; print STDERR "$0: WARNING: SGML filename does not match episode filename $filename in file $file "; #print "BS: $line "; push @tagqueue, ["episode", \%tags]; ; } elsif ($line =~ /<\/episode/) { my $p = pop @tagqueue; $line =~ s/<\/(.*)( +.*)?>/$1/g; die "Unaligned tags: " . $p->[0] . " vs $line" if $p->[0] ne $line; #print "ES: $line "; ; } elsif ($line =~ /<section/) { my %tags = parse_sgml_tag $line; $section_start = $tags{'startTime'}; $section_end = $tags{'endTime'}; #print "BS: $line "; push @tagqueue, ["section", \%tags]; ; } elsif ($line =~ /<\/section/) { my $p = pop @tagqueue; $line =~ s/<\/(.*)( +.*)?>/$1/g; die "Unaligned tags: " . $p->[0] . " vs $line" if $p->[0] ne $line; #print "ES: $line "; ; } elsif ($line =~ /<turn/) { #print "BT: $line "; my %tags = parse_sgml_tag $line; $turn_speaker = $tags{'speaker'}; $turn_start = $tags{'startTime'}; $turn_end = $tags{'endTime'}; $turn_dialect = $tags{'dialect'} if $tags{'dialect'}; $turn_sex = $tags{'sex'}; push @tagqueue, ["turn", \%tags]; ; } elsif ($line =~ /<\/turn/) { my $p = pop @tagqueue; $line =~ s/<\/(.*)( +.*)?>/$1/g; die "Unaligned tags: " . $p->[0] . " vs $line" if $p->[0] ne $line; #print join(" ", @text) . " " if @text > 0; my $new_time = $turn_end; if (@text > 0) { print "$sgml_file $filename $turn_speaker $turn_dialect $turn_sex $time $new_time "; print join(" ", @text) . " "; } @text = (); $time = 0; $turn_speaker = "XXX"; $turn_start = "XXX"; $turn_end = "XXX"; $turn_dialect = "XXX"; $turn_sex = "XXX"; #print "ET: $line "; ; } elsif ($line =~ /<time/) { my %tags = parse_sgml_tag $line; my $new_time = $tags{'sec'}; if (@text > 0) { print "$sgml_file $filename $turn_speaker $turn_dialect $turn_sex $time $new_time "; print join(" ", @text) . " "; } @text = (); $time = $new_time; ; } elsif ($line =~ /<\/time/) { #print $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 "; } elsif ($line =~ /<\//) { ; } else { $line = trim $line; push @text, $line if $line; ; } } close($f); } |