-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessing.py
54 lines (49 loc) · 1.94 KB
/
Preprocessing.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pandas as pd
import re, string
import nltk
#nltk.download()
from nltk.corpus import stopwords
def clean_text(text):
global stopword
text = str(text).lower()
text = re.sub('\[.*?\]', '', text)
text = re.sub('@[^>]+:','',text)import pandas as pd
import re, string
import nltk
#nltk.download()
from nltk.corpus import stopwords
def clean_text(text):
global stopword
text = str(text).lower()
text = re.sub('\[.*?\]', '', text)
text = re.sub('@[^>]+:','',text)
text = re.sub('https?://\S+|www\.\S+', '', text)
text = re.sub('<.*?>+', '', text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
text = re.sub('\n', '', text)
text = re.sub('\w*\d\w*', '', text)
text = [word for word in text.split(' ') if word not in stopword]
text=" ".join(text)
text = [stemmer.stem(word) for word in text.split(' ')]
text=" ".join(text)
return text
df = pd.read_csv('/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/labeled_data.csv', usecols=['class','tweet'])
stemmer = nltk.SnowballStemmer("english")
stopword=set(stopwords.words('english'))
df['clean_txt'] = df['tweet'].apply(lambda x: clean_text(x))
df.to_csv('/Users/umeshkethepalli/Desktop/Hate Speech/HateSpeech-5/data_files/Cleaned_text.csv',index=False)
text = re.sub('https?://\S+|www\.\S+', '', text)
text = re.sub('<.*?>+', '', text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
text = re.sub('\n', '', text)
text = re.sub('\w*\d\w*', '', text)
text = [word for word in text.split(' ') if word not in stopword]
text=" ".join(text)
text = [stemmer.stem(word) for word in text.split(' ')]
text=" ".join(text)
return text
df = pd.read_csv('./Data_files/labeled_data.csv', usecols=['class','tweet'])
stemmer = nltk.SnowballStemmer("english")
stopword=set(stopwords.words('english'))
df['clean_txt'] = df['tweet'].apply(lambda x: clean_text(x))
df.to_csv('Cleaned_text.csv',index=False)