Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit b18d3ad

Browse files
authored
Merge pull request #50 from finos-voice/matthew/FixUpDollarFormatting
fix up dollar formatting and print lines that fail on cleaning
2 parents 97c1c15 + eb56063 commit b18d3ad

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

asrtoolkit/clean_formatting.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def fraction_to_string(input_string):
203203
lambda m: " ".join([digits_to_string(m.groups()[0]), m.groups()[1], "dollars"])
204204
)
205205
),
206-
("dollars", (re.compile(r"\$[0-9]{1,}\.?[0-9]{0,}\w"), lambda m: dollars_to_string(m.group()))),
206+
("dollars", (re.compile(r"\$[0-9]{1,}\.?[0-9]{0,}"), lambda m: dollars_to_string(m.group()))),
207207
("percent", (re.compile(r"\%"), lambda m: " percent")),
208208
("fractions", (re.compile(r"\b[0-9]\s?\/\s[0-9]\b"), lambda m: fraction_to_string(m.group()))),
209209
("plural_numbers", (re.compile(r"\b[0-9]{1,}s\b"), lambda m: plural_numbers_to_string(m.group()))),
@@ -213,6 +213,35 @@ def fraction_to_string(input_string):
213213
)
214214

215215

216+
def remove_special_chars(line, chars_to_replace):
217+
"remove a set of special chars"
218+
for char_to_replace in chars_to_replace:
219+
line = line.replace(char_to_replace, ' ')
220+
return line
221+
222+
223+
def remove_double_spaces(line):
224+
"remove all double spaces"
225+
while " " in line:
226+
line = line.replace(" ", " ")
227+
return line
228+
229+
230+
def apply_all_regex_and_replacements(input_line, rematch):
231+
"""
232+
For a line and list of paired regex and replacements,
233+
apply all replacements for all regex on the line
234+
"""
235+
236+
for pat in rematch:
237+
try:
238+
input_line = re.sub(rematch[pat][0], rematch[pat][1], input_line)
239+
except Exception as exc:
240+
print("Exception {} with line {}".format(exc, input_line))
241+
242+
return input_line
243+
244+
216245
def clean_up(input_line):
217246
"""
218247
Apply all text cleaning operations to input line
@@ -241,20 +270,17 @@ def clean_up(input_line):
241270
>>> clean_up("you can reach me at 1-(317)-222-2222 or fax me at 555-555-5555")
242271
'you can reach me at one three one seven two two two two two two two or fax me at five five five five five five five five five five'
243272
"""
244-
for char_to_replace in ",*&!?":
245-
input_line = input_line.replace(char_to_replace, ' ')
273+
input_line = remove_special_chars(input_line, ",*&!?")
246274

247-
for pat in rematch:
248-
input_line = re.sub(rematch[pat][0], rematch[pat][1], input_line)
275+
input_line = apply_all_regex_and_replacements(input_line, rematch)
249276

250-
for char_to_replace in ",.-":
251-
input_line = input_line.replace(char_to_replace, ' ')
277+
input_line = remove_special_chars(input_line, ",.-")
252278

253279
input_line = input_line.encode().decode('utf-8').lower()
254280

255281
# check for double spacing
256-
while " " in input_line:
257-
input_line = input_line.replace(" ", " ")
282+
input_line = remove_double_spaces(input_line)
283+
258284
return input_line.strip()
259285

260286

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='asrtoolkit',
15-
version='0.1.16',
15+
version='0.1.17',
1616
description=
1717
'The GreenKey ASRToolkit provides tools for automatic speech recognition (ASR) file conversion and corpora organization.',
1818
long_description=long_description,

0 commit comments

Comments
 (0)