-
Notifications
You must be signed in to change notification settings - Fork 1
/
txt2ssml.pl
executable file
·142 lines (111 loc) · 3.51 KB
/
txt2ssml.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
#!/usr/bin/env perl
use v5.10.1;
use bytes;
use strict;
use warnings;
use Getopt::Std;
sub process_sentence ($$);
my %opts;
getopts("s:", \%opts) or die "Usage: $0 [-s SPEED] TXT-FILE\n";
my $speed = $opts{s} // 'slow';
my @chunks;
my $chunk = qq{<speak><prosody rate="$speed">};
my $size = 0;
my $paragraphs = 0;
my $init_tags = qq{<speak><prosody rate="$speed"><break time="500ms"/>};
my $closing_tags = '</prosody></speak>';
while (<>) {
next if /^\s*$/;
chomp;
#warn "line: $_";
if (/^[A-Z][^.,!?]+[A-Za-z]$/) {
my $len = bytes::length $_;
if ($size + $len > 1500) {
$chunk .= $closing_tags;
push @chunks, $chunk;
$chunk = $init_tags;
$size = 0;
}
if ($len <= 50) {
#warn "size: $size, len: $len";
#warn "found title: $_";
s/&/&/g;
s/</</g;
s/>/>/g;
$chunk .= qq{<break time="500ms"/><emphasis>$_.</emphasis><break time="500ms"/>};
$size += $len;
$paragraphs++;
next;
}
}
# a paragraph
s/^\s+|\s+$//g;
$paragraphs++;
my $first = 1;
while (/(.*?[.?!;:]+['")]*)/gc) {
process_sentence($1, \$first);
}
if (!defined pos $_) {
process_sentence($_, \$first);
next;
}
if (pos $_ < bytes::length $_) {
process_sentence(substr($_, pos $_), \$first);
}
}
sub process_sentence ($$) {
my ($sentence, $rfirst) = @_;
#warn "found sentence: $sentence";
my $len = bytes::length $sentence;
#warn "size: $size, len: $len";
if ($size + $len > 1500) {
$chunk .= $closing_tags;
push @chunks, $chunk;
$chunk = $init_tags;
$size = 0;
}
if ($$rfirst) {
if ($paragraphs > 1) {
$chunk .= '<break time="500ms"/>';
}
$$rfirst = 0;
}
$sentence =~ s/\&/\&/g;
$sentence =~ s/</\</g;
$sentence =~ s/>/\>/g;
#$sentence =~ s{N`([A-Za-z][^`]*?[A-Za-z])`}{<emphasis level="strong">$1</emphasis>}smg;
$sentence =~ s{VB`([A-Za-z][^`]*?[A-Za-z])`}{<w role="amazon:VB">$1</w>}smg;
$sentence =~ s{VBD`([A-Za-z][^`]*?[A-Za-z])`}{<w role="amazon:VBD">$1</w>}smg;
$sentence =~ s{N`([A-Za-z][^`]*?[A-Za-z])`}{<w role="amazon:SENSE_1">$1</w>}smg;
$sentence =~ s{(?<=[,:"']) (["'])(.*?)\1}{
my $mark = $1;
my $v = $2;
if ($v =~ /^\w+$/) {
qq! <emphasis>$v</emphasis>!
} else {
qq! <break time="250ms"/>$mark$v$mark!
}
}esmg;
$sentence =~ s{\bcrosslegged\b}{<phoneme alphabet="x-sampa" ph="kr\\OslEgd">crosslegged</phoneme>}gsm;
$sentence =~ s{\bKuru\b}{<phoneme alphabet="x-sampa" ph="ku%r\\u">Kuru</phoneme>}gsm;
$sentence =~ s{\bWill\b}{<phoneme alphabet="x-sampa" ph="%wil">Will</phoneme>}gsm;
$sentence =~ s{\b(?:IR|ir)\b}{<prosody rate="x-slow">I R</prosody>}gsm;
$sentence =~ s{\b(?:ssa|SSA)\b}{<prosody rate="medium">S S A</prosody>}gsm;
$sentence =~ s{\bconcentrated\b}{<phoneme alphabet="x-sampa" ph="%kAnsntSeItId">concentrated</phoneme>}gsm;
if ($len <= 1500) {
$chunk .= $sentence;
$size += $len;
} else {
die "$.: sentence too long: $sentence";
}
}
if ($chunk) {
$chunk .= $closing_tags;
push @chunks, $chunk;
}
for my $chunk (@chunks) {
$chunk =~ s/> />/g;
$chunk =~ s{<break time="\d+m?s"/><break time="(\d+m?s)"/>}{<break time="$1"/>}gs;
#warn "!!", bytes::length $chunk;
}
print join "\n", @chunks;