-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
55 lines (46 loc) · 1.94 KB
/
preprocess.py
File metadata and controls
55 lines (46 loc) · 1.94 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import pyarabic.araby as araby
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stopwords_list = stopwords.words('arabic')
class Preprocessor():
def __init__(self, df, classes):
self.df = df
self.classes = classes
def ClassCount(self, figsize=(10,6)):
frequency = []
for label in self.classes:
frequency.append(len(self.df[self.df.Class==label]))
self.counter = dict(zip(self.classes, frequency))
plt.figure(figsize=figsize)
sns.barplot(list(self.counter.keys()), list(self.counter.values()), alpha=1)
plt.xlabel('Class')
plt.ylabel('Frequency')
plt.title('Number of Comments Per Class')
plt.show()
return self.counter
def map_labels(self):
# change labels if classes are different than 3
self.df['Class'] = self.df['Class'].replace({'normal':0, 'abusive':1, 'hate':2})
@staticmethod
def normalize_text(text):
text = re.sub('[^\u0600-\u06FF\s!؟]', '', text) # remove non-arabic ASCII and keep !?
text = re.sub('[٠-٩]', '', text) # remove arabic numbers
text = re.sub('[.،:-؛()]', '', text) # remove punctuation marks except ? and !
text = re.sub('[اآأإٱىٰ]', 'ا', text) # subsitutes forms of alif with one form
text = araby.strip_diacritics(text) # remove diacritics
text = re.sub(' +', ' ', text) # ensure only one empty space between words
text = ' '.join([tok for tok in text.split() if tok not in (stopwords_list)])
text = text.strip() # remove leading and trailing spaces
return text
def density_plot(self):
seqlen = self.df['Tweet'].apply(lambda x: len(x.split(' ')))
plt.figure(figsize=(10,8))
sns.distplot(seqlen)
plt.show()