How to use cleantext.replace_punct() 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
The replace_punct() method
The replace_punct() method replaces the punctuation in the given text with the replacement string.
Syntax
replace_punct(text, replace_with=" ")
Parameters
text: The text data.replace_with: The replacement string.
Return value
The method returns the text data after replacing the punctuation.
Example
import cleantextstring = "hello educative - 2-3-4 - hello edpresso!!"new_string = cleantext.replace_punct(string, replace_with=" ")print("Original String - '" + string + "'")print("Modified String - '" + new_string + "'")
Explanation
- Line 1: We import the
cleantextpackage. - Line 3: We define a string with random punctuation.
- Line 5: We replace the punctuation marks in the string with whitespace using the
replace_punct()method. - Lines 7–8: We print the original and modified strings.