-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
56 lines (39 loc) · 1.42 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
55
56
import pandas as pd
import re
import nltk
from nltk.corpus import stopwords
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
nltk.download('punkt')
nltk.download('stopwords')
df = pd.read_csv('https://raw.githubusercontent.com/asthala/racism-detection/master/datasetfix.csv')
df.head()
def clean(data):
tweets = []
for tweet in data:
tweet = re.sub(r"'(?:\@|https?\://)\S+", "", tweet)
tweet = re.sub('\n', '', tweet)
tweet = re.sub('rt', '', tweet)
tweet = re.sub("[^a-zA-Z^']", " ", tweet)
tweet = re.sub(" {2,}", " ", tweet)
tweet = tweet.strip()
tweets.append(tweet)
return tweets
df_clean = df.copy()
df_clean['tweets']= clean(df['tweets'])
def case_fold(data):
return data.str.lower()
df_clean['tweets'] = case_fold(df_clean['tweets'])
def token(data):
return data.apply(nltk.word_tokenize)
df_clean['tweets'] = token(df_clean['tweets'])
def stop_words(data) :
stop_words = set(stopwords.words('indonesian'))
return data.apply(lambda x: [item for item in x if item not in stop_words])
df_clean['tweets'] = stop_words(df_clean['tweets'])
def stem(data):
factory = StemmerFactory()
stemmer = factory.create_stemmer()
return data.apply(lambda x: [ stemmer.stem(item) for item in x])
df_clean['tweets'] = stem(df_clean['tweets'])
print(df_clean['tweets'][:5])
df_clean.to_csv('data_clean.csv',index = False)