-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cleaner.py
40 lines (29 loc) · 994 Bytes
/
data_cleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import nltk
import re
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
stop_words = stopwords.words('english')
lemmatizer = WordNetLemmatizer()
def preprocess_text(text):
# Make text lowercase
text = text.lower()
# Remove text in square brackets
text = re.sub('\[.*?\]', '', text)
# Remove links
text = re.sub('https?://\S+|www\.\S+', '', text)
# Remove punctuation
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
# Remove words containing numbers
text = re.sub('\w*\d\w*', '', text)
# Tokenize the text
tokens = word_tokenize(text)
# Remove stopwords
tokens = [word for word in tokens if word not in stop_words]
# Lemmatize the tokens
tokens = [lemmatizer.lemmatize(word) for word in tokens]
# Join the tokens back into text
text = ' '.join(tokens)
return text