What is replace_numbers() method of clean-text package in Python?

What is the clean-text package?

Clean-text is a third-party package used to pre-process text data to obtain a normalized text representation.

The package can be installed via pip. Check the following command to install the clean-text package.

pip install clean-text

replace_numbers() method

The replace_numbers() method is used to replace all the numbers in the given text with the replacement string.

Method signature

replace_numbers(text, replace_with="<NUMBER>")

Parameters

  • text: This is the text data whose numbers we want to replace with the replacement string.
  • replace_with: This is the replacement string we are replacing the numbers with.

Return value

This method returns the text data where all the numbers are replaced with the string.

Code

import cleantext
string = """hello educative - 234123 - hello edpresso"""
new_string = cleantext.replace_numbers(string, replace_with="number_was_here")
print("Original String - '" + string + "'")
print("Modified String - '" + new_string + "'")

Code explanation

  • Line 1: We import the cleantext package.
  • Line 3: We define a string with numbers in it.
  • Line 5: We replace the numbers in the string with number_was_here using the replace_numbers() method.
  • Lines 7-8: We print the original and the modified strings.

Free Resources