-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_cleaning.py
More file actions
98 lines (75 loc) · 2.95 KB
/
Copy pathdata_cleaning.py
File metadata and controls
98 lines (75 loc) · 2.95 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import emoji
import nltk
nltk.download('stopwords')
import re
import string
import pandas as pd
df = pd.read_csv('data/offensivelang_dataset.csv')
test_data = df.sample(frac=0.2,random_state=200)
test_data.shape
data=df.drop(test_data.index)
arabic_stopwords = set(nltk.corpus.stopwords.words("arabic"))
arabic_diacritics = re.compile("""
ّ | # Tashdid
َ | # Fatha
ً | # Tanwin Fath
ُ | # Damma
ٌ | # Tanwin Damm
ِ | # Kasra
ٍ | # Tanwin Kasr
ْ | # Sukun
ـ # Tatwil/Kashida
""", re.VERBOSE)
arabic_punctuations = '''`÷×؛<>_()*&^%][ـ،/:"؟.,'{}~¦+|!”…“–ـ'''
english_punctuations = string.punctuation
punctuations = arabic_punctuations + english_punctuations
X = df.Comment.values
Y = df.Majority_Label.values
def remove_urls (text):
text = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '', text, flags=re.MULTILINE)
return text
def remove_emails(text):
text = re.sub(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", "", text, flags=re.MULTILINE)
return text
def remove_emoji(text):
return emoji.get_emoji_regexp().sub(u'', text)
def normalization(text):
text = re.sub("[إأآا]", "ا", text)
text = re.sub("ى", "ي", text)
text = re.sub("ؤ", "ء", text)
text = re.sub("ئ", "ء", text)
text = re.sub("ة", "ه", text)
text = re.sub("گ", "ك", text)
return text
def remove_diacritics(text):
text = re.sub(arabic_diacritics, '', text)
return text
def remove_stopwords(text):
filtered_sentence = [w for w in text.split() if not w in arabic_stopwords]
return ' '.join(filtered_sentence)
def cleaning_content(line):
if (isinstance(line, float)):
return None
line.replace('\n', ' ')
line = remove_emails(line)
line = remove_urls(line)
line = remove_emoji(line)
nline = [w if '@' not in w else 'USERID' for w in line.split()]
line = ' '.join(nline)
line = line.replace('RT', '').replace('<LF>', '').replace('<br />','').replace('"', '').replace('<url>', '')
# add spaces between punc,
line = line.translate(str.maketrans({key: " {0} ".format(key) for key in punctuations}))
# then remove punc,
translator = str.maketrans('', '', punctuations)
line = line.translate(translator)
line=remove_diacritics(normalization(line))
line = remove_stopwords(line)
line = ' '.join(nline)
return line
def hasDigits(s):
return any( 48 <= ord(char) <= 57 or 1632 <= ord(char) <= 1641 for char in s)
if __name__=='__main__':
df.Comment = df.Comment.apply(cleaning_content)
comments = ' '.join(list(df.Comment))
words = comments.split(' ')
print(pd.Series(nltk.ngrams(words, 1)).value_counts()[:20])