-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
33 lines (24 loc) · 859 Bytes
/
utils.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
import re
import string
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
def text_preprocessing(text,stopword):
# lowercase
text = text.lower()
# Remove URL
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# Remove user @ references and ‘#’ from text
text = re.sub(r'\@\w+|\#','', text)
# Remove punctuations
translator = str.maketrans("", "", string.punctuation)
text = text.translate(translator)
# Tokenize to remove stopwords
text_tokens = word_tokenize(text)
# Remove stopwords
# Remove stopwords
cleaned_text = ' '
tokens_without_sw = [cleaned_text.join(word) for word in text_tokens if word not in stopword]
return tokens_without_sw