-
Notifications
You must be signed in to change notification settings - Fork 64
/
normalizer.py
137 lines (119 loc) · 4.04 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
import string
import inflect
class 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(
[Normalizer.AMERICAN_SPELLINGS[x] if x in Normalizer.AMERICAN_SPELLINGS else x for x in sentence.split()])
@staticmethod
def normalize_abbreviations(sentence: str) -> str:
return ' '.join([Normalizer.ABBREVIATIONS[x] if x in Normalizer.ABBREVIATIONS else x for x in sentence.split()])
@staticmethod
def normalize(sentence: str) -> 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 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']