-
Notifications
You must be signed in to change notification settings - Fork 64
/
normalizer.py
234 lines (207 loc) · 6.58 KB
/
normalizer.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
import re
import string
import unicodedata
import inflect
from languages import Languages
class Normalizer(object):
@staticmethod
def normalize(sentence: str, raise_error_on_invalid_sentence: bool) -> str:
raise NotImplementedError()
@classmethod
def create(cls, language: Languages):
if language == Languages.EN:
return EnglishNormalizer()
elif language in [
Languages.DE,
Languages.ES,
Languages.FR,
Languages.IT,
Languages.PT_PT,
Languages.PT_BR,
]:
return DefaultNormalizer()
else:
raise ValueError(f"Cannot create {cls.__name__} of type `{language}`")
class DefaultNormalizer:
"""
Adapted from: https://github.com/openai/whisper/blob/main/whisper/normalizers/basic.py
"""
ADDITIONAL_DIACRITICS = {
"œ": "oe",
"Œ": "OE",
"ø": "o",
"Ø": "O",
"æ": "ae",
"Æ": "AE",
"ß": "ss",
"ẞ": "SS",
"đ": "d",
"Đ": "D",
"ð": "d",
"Ð": "D",
"þ": "th",
"Þ": "th",
"ł": "l",
"Ł": "L",
}
@staticmethod
def _remove_symbols_and_diacritics(s: str) -> str:
return "".join(
(
DefaultNormalizer.ADDITIONAL_DIACRITICS[c]
if c in DefaultNormalizer.ADDITIONAL_DIACRITICS
else (
""
if unicodedata.category(c) == "Mn"
else " " if unicodedata.category(c)[0] in "MSP" else c
)
)
for c in unicodedata.normalize("NFKD", s)
)
@staticmethod
def normalize(sentence: str, raise_error_on_invalid_sentence: bool = False) -> str:
sentence = sentence.lower()
sentence = re.sub(r"[<\[][^>\]]*[>\]]", "", sentence)
sentence = re.sub(r"\(([^)]+?)\)", "", sentence)
sentence = DefaultNormalizer._remove_symbols_and_diacritics(sentence).lower()
sentence = re.sub(r"\s+", " ", sentence)
return sentence
class EnglishNormalizer(Normalizer):
AMERICAN_SPELLINGS = {
"acknowledgement": "acknowledgment",
"analogue": "analog",
"armour": "armor",
"ascendency": "ascendancy",
"behaviour": "behavior",
"behaviourist": "behaviorist",
"cancelled": "canceled",
"catalogue": "catalog",
"centre": "center",
"centres": "centers",
"colour": "color",
"coloured": "colored",
"colourist": "colorist",
"colourists": "colorists",
"colours": "colors",
"cosier": "cozier",
"counselled": "counseled",
"criticised": "criticized",
"crystallise": "crystallize",
"defence": "defense",
"discoloured": "discolored",
"dishonour": "dishonor",
"dishonoured": "dishonored",
"encyclopaedia": "Encyclopedia",
"endeavour": "endeavor",
"endeavouring": "endeavoring",
"favour": "favor",
"favourite": "favorite",
"favours": "favors",
"fibre": "fiber",
"flamingoes": "flamingos",
"fulfill": "fulfil",
"grey": "gray",
"harmonised": "harmonized",
"honour": "honor",
"honourable": "honorable",
"honourably": "honorably",
"honoured": "honored",
"honours": "honors",
"humour": "humor",
"islamised": "islamized",
"labour": "labor",
"labourers": "laborers",
"levelling": "leveling",
"luis": "lewis",
"lustre": "luster",
"manoeuvring": "maneuvering",
"marshall": "marshal",
"marvellous": "marvelous",
"merchandising": "merchandizing",
"milicent": "millicent",
"moustache": "mustache",
"moustaches": "mustaches",
"neighbour": "neighbor",
"neighbourhood": "neighborhood",
"neighbouring": "neighboring",
"neighbours": "neighbors",
"omelette": "omelet",
"organisation": "organization",
"organiser": "organizer",
"practise": "practice",
"pretence": "pretense",
"programme": "program",
"realise": "realize",
"realised": "realized",
"recognised": "recognized",
"shrivelled": "shriveled",
"signalling": "signaling",
"skilfully": "skillfully",
"smouldering": "smoldering",
"specialised": "specialized",
"sterilise": "sterilize",
"sylvia": "silvia",
"theatre": "theater",
"theatres": "theaters",
"travelled": "traveled",
"travellers": "travelers",
"travelling": "traveling",
"vapours": "vapors",
"wilful": "willful",
}
ABBREVIATIONS = {
"junior": "jr",
"senior": "sr",
"okay": "ok",
"doctor": "dr",
"mister": "mr",
"missus": "mrs",
"saint": "st",
}
@staticmethod
def to_american(sentence: str) -> str:
return " ".join(
[
(
EnglishNormalizer.AMERICAN_SPELLINGS[x]
if x in EnglishNormalizer.AMERICAN_SPELLINGS
else x
)
for x in sentence.split()
]
)
@staticmethod
def normalize_abbreviations(sentence: str) -> str:
return " ".join(
[
(
EnglishNormalizer.ABBREVIATIONS[x]
if x in EnglishNormalizer.ABBREVIATIONS
else x
)
for x in sentence.split()
]
)
@staticmethod
def normalize(sentence: str, raise_error_on_invalid_sentence: bool = False) -> str:
p = inflect.engine()
sentence = sentence.lower()
for c in "-/–—":
sentence = sentence.replace(c, " ")
for c in '‘!",.:;?“”`':
sentence = sentence.replace(c, "")
sentence = sentence.replace("’", "'").replace("&", "and")
def num2txt(y):
return (
p.number_to_words(y).replace("-", " ").replace(",", "")
if any(x.isdigit() for x in y)
else y
)
sentence = " ".join(num2txt(x) for x in sentence.split())
if raise_error_on_invalid_sentence:
if not all(c in " '" + string.ascii_lowercase for c in sentence):
raise RuntimeError()
if any(x.startswith("'") for x in sentence.split()):
raise RuntimeError()
return sentence
__all__ = ["Normalizer"]