@@ -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+
216245def 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
0 commit comments