Menu

Cleaning Review Text

Cleaning Review Text

Raw reviews often contain punctuation, numbers, symbols, extra spaces, and uppercase letters. These elements usually do not contribute meaningful information for text classification.

Cleaning the text helps the model focus on the actual words.

We will perform the following preprocessing steps:

  • Convert text to lowercase
  • Remove punctuation
  • Remove numbers
  • Remove special characters
  • Remove extra spaces

Code

import re
def clean_text(text):

Convert to lowercase

text = text.lower()

Remove punctuation and special characters

text = re.sub(r'[^a-zA-Z\s]', '', text)

Remove extra spaces

text = re.sub(r'\s+', ' ', text)
return text.strip()

Apply the Function

df['clean_review'] = df['review'].apply(clean_text)

View Results

df[['review','clean_review']].head()