-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
41 lines (33 loc) · 1.18 KB
/
preprocessing.py
File metadata and controls
41 lines (33 loc) · 1.18 KB
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
41
import re
import string
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
stemmer = PorterStemmer()
def cleaningText(text):
text = re.sub(r'@[A-Za-z0-9]+', '', text)
text = re.sub(r'#[A-Za-z0-9]+', '', text)
text = re.sub(r'http\S+', '', text)
text = re.sub(r'[0-9]+', '', text)
text = text.replace('\n', ' ')
text = text.translate(str.maketrans('', '', string.punctuation))
text = text.strip(' ')
return text
def casefoldingText(text):
return text.lower()
def tokenizingText(text):
return word_tokenize(text)
def filteringText(text):
listStopwords = set(stopwords.words('english'))
filtered = [word for word in text if word not in listStopwords]
return filtered
def stemmingText(text):
stemmed_words = [stemmer.stem(word) for word in text]
return ' '.join(stemmed_words)
def preprocess_text(text):
text = cleaningText(text)
text = casefoldingText(text)
tokens = tokenizingText(text)
filtered_tokens = filteringText(tokens)
stemmed_text = stemmingText(filtered_tokens)
return stemmed_text