forked from bootphon/phonemizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_punctuation.py
269 lines (222 loc) · 9.38 KB
/
test_punctuation.py
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
# Copyright 2015-2021 Mathieu Bernard
#
# This file is part of phonemizer: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Phonemizer is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with phonemizer. If not, see <http://www.gnu.org/licenses/>.
"""Test of the punctuation processing"""
# pylint: disable=missing-docstring
from pathlib import Path
import pytest
import re
from phonemizer.backend import EspeakBackend, FestivalBackend, SegmentsBackend
from phonemizer.punctuation import Punctuation
from phonemizer.phonemize import phonemize
from phonemizer.separator import Separator, default_separator
# True if we are using espeak>=1.50
ESPEAK_150 = (EspeakBackend.version() >= (1, 50))
# True if we are using espeak>=1.49.3
ESPEAK_143 = (EspeakBackend.version() >= (1, 49, 3))
# True if we are using festival>=2.5
FESTIVAL_25 = (FestivalBackend.version() >= (2, 5))
@pytest.mark.parametrize(
'inp, out', [
('a, b,c.', 'a b c'),
('abc de', 'abc de'),
('!d.d. dd?? d!', 'd d dd d')])
def test_remove(inp, out):
assert Punctuation().remove(inp) == out
@pytest.mark.parametrize(
'inp', [
['.a.b.c.'],
['a, a?', 'b, b'],
['a, a?', 'b, b', '!'],
['a, a?', '!?', 'b, b'],
['!?', 'a, a?', 'b, b'],
['a, a, a'],
['a, a?', 'aaa bb', '.bb, b', 'c', '!d.d. dd?? d!'],
['Truly replied, "Yes".'],
['hi; ho,"'],
["!?"],
["!'"]])
def test_preserve(inp):
punct = Punctuation()
text, marks = punct.preserve(inp)
assert inp == punct.restore(text, marks, sep=default_separator, strip=True)
@pytest.mark.parametrize(
'text, expected_restore, expected_output', [
(['hi; ho,"'], ['hi; ho," '], ['haɪ; hoʊ, ']),
(['hi; "ho,'], ['hi; "ho, '], ['haɪ; hoʊ, '] if ESPEAK_143 else ['haɪ; hoʊ, ']),
(['"hi; ho,'], ['"hi; ho, '], ['haɪ; hoʊ, '] if ESPEAK_143 else [' haɪ; hoʊ, '])])
def test_preserve_2(text, expected_restore, expected_output):
marks = ".!;:,?"
punct = Punctuation(marks=marks)
assert expected_restore == punct.restore(*punct.preserve(text), sep=default_separator, strip=False)
output = phonemize(
text, backend="espeak",
preserve_punctuation=True, punctuation_marks=marks)
assert output == expected_output
def test_custom():
punct = Punctuation()
assert set(punct.marks) == set(punct.default_marks())
assert punct.remove('a,b.c') == 'a b c'
with pytest.raises(ValueError):
punct.marks = ['?', '.']
punct.marks = '?.'
assert len(punct.marks) == 2
assert punct.remove('a,b.c') == 'a,b c'
def test_espeak():
text = 'hello, world!'
expected1 = 'həloʊ wɜːld'
expected2 = 'həloʊ, wɜːld!'
expected3 = 'həloʊ wɜːld '
expected4 = 'həloʊ, wɜːld! '
out1 = EspeakBackend('en-us', preserve_punctuation=False).phonemize(
[text], strip=True)[0]
assert out1 == expected1
out2 = EspeakBackend('en-us', preserve_punctuation=True).phonemize(
[text], strip=True)[0]
assert out2 == expected2
out3 = EspeakBackend('en-us', preserve_punctuation=False).phonemize(
[text], strip=False)[0]
assert out3 == expected3
out4 = EspeakBackend('en-us', preserve_punctuation=True).phonemize(
[text], strip=False)[0]
assert out4 == expected4
def test_festival():
text = 'hello, world!'
expected1 = 'hhaxlow werld'
expected2 = 'hhaxlow, werld!'
expected3 = 'hhaxlow werld '
expected4 = 'hhaxlow, werld! '
out1 = FestivalBackend('en-us', preserve_punctuation=False).phonemize(
[text], strip=True)[0]
assert out1 == expected1
out2 = FestivalBackend('en-us', preserve_punctuation=True).phonemize(
[text], strip=True)[0]
assert out2 == expected2
out3 = FestivalBackend('en-us', preserve_punctuation=False).phonemize(
[text], strip=False)[0]
assert out3 == expected3
out4 = FestivalBackend('en-us', preserve_punctuation=True).phonemize(
[text], strip=False)[0]
assert out4 == expected4
def test_segments():
text = 'achi, acho!'
expected1 = 'ʌtʃɪ ʌtʃʊ'
expected2 = 'ʌtʃɪ, ʌtʃʊ!'
expected3 = 'ʌtʃɪ ʌtʃʊ '
expected4 = 'ʌtʃɪ, ʌtʃʊ! '
out1 = SegmentsBackend('cree', preserve_punctuation=False).phonemize(
[text], strip=True)[0]
assert out1 == expected1
out2 = SegmentsBackend('cree', preserve_punctuation=True).phonemize(
[text], strip=True)[0]
assert out2 == expected2
out3 = SegmentsBackend('cree', preserve_punctuation=False).phonemize(
[text], strip=False)[0]
assert out3 == expected3
out4 = SegmentsBackend('cree', preserve_punctuation=True).phonemize(
[text], strip=False)[0]
assert out4 == expected4
# see https://github.com/bootphon/phonemizer/issues/54
@pytest.mark.parametrize(
'text, expected', [("!'", "! "), ("'!", "! "), ("!'!", "!! "), ("'!'", "! ")])
def test_issue_54(text, expected):
output = phonemize(
[text], language='en-us', backend='espeak',
preserve_punctuation=True)[0]
assert expected == output
# see https://github.com/bootphon/phonemizer/issues/55
@pytest.mark.parametrize(
'backend, marks, text, expected', [
('espeak', 'default', ['"Hey! "', '"hey,"'], ['"heɪ! " ', '"heɪ," ']),
('espeak', '.!;:,?', ['"Hey! " ', '"hey," '],
['heɪ! ', 'heɪ, '] if ESPEAK_150 else [' heɪ! ', ' heɪ, ']),
('espeak', 'default', ['! ?', 'hey!'], ['! ? ', 'heɪ! ']),
('espeak', '!', ['! ?', 'hey!'], ['! ', 'heɪ! ']),
('segments', 'default', ['! ?', 'hey!'], ['! ? ', 'heːj! ']),
('segments', '!', ['! ?', 'hey!'], ValueError),
('festival', 'default', ['! ?', 'hey!'], ['! ? ', 'hhey! ']),
('festival', '!', ['! ?', 'hey!'], ['! ', 'hhey! '])])
def test_issue55(backend, marks, text, expected):
if marks == 'default':
marks = Punctuation.default_marks()
language = 'cree' if backend == 'segments' else 'en-us'
try:
with pytest.raises(expected):
phonemize(
text, language=language, backend=backend,
preserve_punctuation=True, punctuation_marks=marks)
except TypeError:
try:
assert expected == phonemize(
text, language=language, backend=backend,
preserve_punctuation=True, punctuation_marks=marks)
except RuntimeError:
if backend == 'festival':
# TODO on some installations festival fails to phonemize "?".
# It ends with a segmentation fault. This seems to only appear
# with festival-2.5 (but is working on travis and docker image)
pass
@pytest.mark.parametrize(
'punctuation_marks, text, expected', [
(';:,.!?¡—…"«»“”',
'hello, ,world? ‡ 3,000, or 2.50. ¿hello?',
'həloʊ, ,wɜːld? θɹiː,ziəɹoʊziəɹoʊ ziəɹoʊ, ɔːɹ tuː.fɪfti. həloʊ? '),
(re.compile(r"[^a-zA-ZÀ-ÖØ-öø-ÿ0-9'$@&+%\-=/\\]"),
'hello, ,world? ‡ 3,000, or 2.50. ¿hello?',
'həloʊ, ,wɜːld? ‡ θɹiː,ziəɹoʊziəɹoʊ ziəɹoʊ, ɔːɹ tuː.fɪfti. ¿həloʊ? '),
(re.compile(r"[^a-zA-ZÀ-ÖØ-öø-ÿ0-9',.$@&+%\-=/\\]|[,.](?!\d)"),
'hello, ,world? ‡ 3,000, or 2.50. ¿hello?',
'həloʊ, ,wɜːld? ‡ θɹiː θaʊzənd, ɔːɹ tuː pɔɪnt faɪv ziəɹoʊ. ¿həloʊ? ')
])
def test_punctuation_marks_regex(punctuation_marks, text, expected):
assert expected == phonemize(
text, preserve_punctuation=True, punctuation_marks=punctuation_marks)
def test_marks_getter_with_regex():
marks_re = re.compile(r"[^a-zA-Z0-9]")
punct = Punctuation(marks_re)
with pytest.raises(ValueError):
punct.marks == marks_re
def test_long_document():
# testing issue raised by #108
DATA_FOLDER = Path(__file__).parent / "data"
with open(DATA_FOLDER / "pg67147.txt") as txt_file:
phonemize(txt_file.read().split("\n"), backend="espeak", preserve_punctuation=True)
@pytest.mark.parametrize(
'text', [
([
'worked david ford i started in deloitte and i was immediately',
]
),
([
'worked david ford i started in deloitte, and i was immediately',
]
),
([
'worked david ford i started in deloitte and i was immediately',
'an offer of price waterhouse cooper and here i take may',
'we are now as maximum plan for a customer time and',
"they're going to meet all the xvin so great it"
]
),
([
'worked david ford i started in deloitte, and i was immediately',
'an offer of price waterhouse cooper and here i take may',
'we are now as maximum plan for a customer time and',
"they're going to meet all the xvin so great it."
]
),
])
def test_multiline_punctuation(text):
phonemized = phonemize(text, preserve_punctuation=True)
assert len(text) == len(phonemized)