-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
187 lines (135 loc) · 5.17 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import pandas as pd
from nltk import word_tokenize
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from string import punctuation
pd.set_option('display.max_columns', 20)
pd.set_option('display.max_rows', 20)
my_punctuation = '€£' + punctuation
def word_count(headline):
return len(headline.split())
def question_mark(headline):
return int('?' in headline)
def exclamation_mark(headline):
return int('!' in headline)
def starts_with_digit(headline):
return int(headline[0].isdigit())
def starts_with_question_word(headline):
return int(headline.startswith(('what', 'where', 'when', 'who', 'why', 'whom', 'whose', 'which',
'how', 'will', 'would', 'should', 'could', 'do', 'did')))
def remove_punctuation(headline):
return ''.join(w for w in headline if w not in my_punctuation)
def remove_punct_tokens(tokens):
return [token for token in tokens if token.isalnum()]
def tokenize(headline):
tokens = [word_tokenize(x) for x in headline]
return tokens
def longest_word_len(tokens):
"""
:param tokens: tokenized headline
"""
token_lengths = set(len(token) for token in tokens)
return max(token_lengths)
def avg_word_len(tokens):
"""
:param tokens: tokenized headline
"""
return round(sum(len(token) for token in tokens) / len(tokens), 4)
def ratio_stopwords(tokens):
"""
:param tokens: tokenized headline
"""
stop_words = set(stopwords.words('english'))
count = 0
for token in tokens:
if token in stop_words:
count += 1
return round(count / len(tokens), 4)
def remove_stopwords(tokens):
"""
:param tokens: tokenized headline
"""
stop_words = set(stopwords.words('english'))
return [token for token in tokens if token not in stop_words]
def pos_tagging(tokens):
return pos_tag(tokens)
def lemmatise(tokens_wordnet):
lemmatiser = WordNetLemmatizer()
new_tokens = []
for token, tag in tokens_wordnet:
if tag is None:
new_tokens.append(lemmatiser.lemmatize(token))
else:
new_tokens.append(lemmatiser.lemmatize(token, tag))
return new_tokens
def pos_tag_convert(nltk_tag):
if nltk_tag.startswith('J'):
return wordnet.ADJ
elif nltk_tag.startswith('V'):
return wordnet.VERB
elif nltk_tag.startswith('N'):
return wordnet.NOUN
elif nltk_tag.startswith('R'):
return wordnet.ADV
else:
return None
def change_pos_tag(list_tuples):
result = []
for token, tag in list_tuples:
result.append(tuple([token, pos_tag_convert(tag)]))
return result
clickbait = [
"data/buzzfeed_2020.csv",
"data/boredpanda_2020.csv",
"data/joedotie_2020.csv",
"data/theodyssey_2019-20.csv",
"data/upworthy_2020.csv",
"data/clickbait_data.csv"]
non_clickbait = [
"NON_CLICKBAIT_DATA/BBC_DATA/BBC_merged_2010-2020.csv",
"NON_CLICKBAIT_DATA/CNN_DATA/CNN_merged_2010-2020.csv",
"NON_CLICKBAIT_DATA/GUARDIAN_DATA/guardian_merged_2010-2020.csv",
"NON_CLICKBAIT_DATA/IRISH_TIMES_DATA/Irish_times_merged_2010-2020.csv",
"NON_CLICKBAIT_DATA/NY_TIMES_DATA/NY_Times_merged_2010-2020.csv"]
data_frames = []
for path in clickbait:
curr_df = pd.read_csv(path, quotechar='"', skipinitialspace=True, dtype='string')
curr_df.insert(0, 'class', 1) # label = 1 clickbait
data_frames.append(curr_df)
for path in non_clickbait:
curr_df = pd.read_csv(path, quotechar='"', skipinitialspace=True, dtype='string')
curr_df.insert(0, 'class', 0) # label = 0 non-clickbait
data_frames.append(curr_df)
for df in data_frames:
df.columns = ['class', 'headline', 'date']
# tokens for POS tagging and lemmatisation later
df['text'] = tokenize(df['headline'])
df['text'] = df['text'].apply(remove_punct_tokens)
# make lowercase
df['headline'] = df['headline'].str.lower()
# text processing with strings BEFORE removing punctuation and stopwords
df['word_count'] = df['headline'].apply(word_count)
df['question_mark'] = df['headline'].apply(question_mark)
df['exclamation_mark'] = df['headline'].apply(exclamation_mark)
df['start_digit'] = df['headline'].apply(starts_with_digit)
df['start_question'] = df['headline'].apply(starts_with_question_word)
# remove punctuation
df['headline'] = df['headline'].apply(remove_punctuation)
# tokenize
df['headline'] = tokenize(df.headline)
# text processing with tokens
df['longest_word_len'] = df['headline'].apply(longest_word_len)
df['avg_word_len'] = df['headline'].apply(avg_word_len)
df['ratio_stopwords'] = df['headline'].apply(ratio_stopwords)
# lemmatisation
df['text'] = df['text'].apply(pos_tagging)
df['text'] = df['text'].apply(change_pos_tag)
df['text'] = df['text'].apply(lemmatise)
df['text'] = df['text'].apply(lambda x: [token.lower() for token in x])
df['text'] = df['text'].apply(remove_stopwords)
df.drop(columns=['date', 'headline'], inplace=True)
print(df.head())
the_motherload = pd.concat(data_frames, ignore_index=True)
the_motherload.to_csv(path_or_buf="data/features.csv")