-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_preprocess.py
341 lines (261 loc) · 8.76 KB
/
1_preprocess.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# This file transforms the dataset so that we remove all unnecessary characters, accents, stop words and stems the words to reduce the vocabulary
# %% [markdown]
# # Import libraries
# %% [markdown]
# ## TODO: move to requirements.txt
# %%
# !pip install stop-words
# !pip install wordcloud
# !pip install stanza
# !pip install spacy-stanza
# %%
import pandas as pd
import numpy as np
import re
import unidecode
from nltk.probability import FreqDist
from nltk.corpus import stopwords as swords
from stop_words import get_stop_words
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
import pickle
import stanza
import spacy_stanza
import itertools
import nltk
from nltk.stem import SnowballStemmer
from matplotlib import pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import json
nltk.download('stopwords')
# Download the stanza model if necessary
stanza.download("es")
stemmer = SnowballStemmer("spanish")
# Initialize the pipeline
nlp = spacy_stanza.load_pipeline("es")
# %% [markdown]
# # Load the dataset
# %%
df = pd.read_pickle('initiatives.pkl')
# %%
df.head()
# %%
# If content is not present, use the title as content
df['content_coalesce'] = df['content'].combine_first(df['title'])
# %%
# test
df[df['content'].isna()][['content_coalesce','title','content']]
# %%
# THIS FLATTENS the conetent from lists to str
df['content_coalesce'] = [''.join(l) for l in df['content_coalesce']]
# %%
# Here we flatten and then create lists of the individual words
# df['lists_content_coalesce'] = [''.join(l).split(" ") for l in df['content_coalesce']]
# %% [markdown]
# # Define helper functions
# %%
def create_df_from_json(file_path, columns_to_keep=['content','title','initiative_type_alt'],field_name='initiatives'):
with open(file_path,'r', encoding="utf8") as f:
data = json.loads(f.read())
data_frame = pd.json_normalize(data,record_path=field_name)
data_frame = data_frame[columns_to_keep]
return(data_frame)
def retrieve_stop_words():
spanish_stopwords = swords.words('spanish')
stop_words_spanish = get_stop_words('spanish')
stopwords = list(set(spanish_stopwords + stop_words_spanish))
return stopwords
def space_out_your_text(row):
doc = nlp(row)
cleaned = ""
for token in doc:
if token.pos_ not in ("PUNCT","ADP","SCONJ","PRON","CCONJ"):
#print(token.text, token.lemma_, token.pos_, token.dep_)
cleaned+=token.lemma_+" "
return cleaned
def remove_accents(row,column):
return unidecode.unidecode(row[column])
#remove special characters
def replace_special_char(row):
for word, initial in {".":" ",
"-":" ",
"/":" ",
"@":" ",
"#":" ",
"(":" ",
")":" ",
'"' : "",
" ,":"",
">":"",
"<":"",
"*":"",
",":" ",
":":"",
"?":"",
"'":"",
"%":"",
"+":" ",
"&":"",
"\n":" ",
"\\":""}.items(): #special case - comma becomes space just in case
row = row.replace(word, initial)
return row
def remove_stopwords(row, stopwords):
removed_stopwords = " ".join([word for word in row.split(" ") if word not in stopwords and word.replace(" ","")!=""])
return removed_stopwords
def remove_numbers(col):
return col.str.replace('\d+', '')
def unique_words(col):
words = col.str.lower().str.findall("\w+")
unique = set()
for x in words:
unique.update(x)
return unique
def unique_words_using_vectorizer(text_column):
# tokenize and build vocab
vectorizer = CountVectorizer()
vectorizer.fit(text_column)
# summarize
# print(vectorizer.vocabulary_)
# encode document
vector = vectorizer.transform(text_column)
# summarize encoded vector
print(vector.shape)
def word_count(df):
tf = df['text'].apply(lambda x: FreqDist(x)).sum(axis = 0)
tf2 = dict(tf)
data_items = tf2.items()
data_list = list(data_items)
freq_dataframe = pd.DataFrame(data_list)
freq_dataframe.columns = ['Word','Counts']
freq_dataframe = freq_dataframe.sort_values(by="Counts",ascending=False)
pd.set_option("max_rows", None)
return freq_dataframe
# %% [markdown]
# # Starting with this many unique words
# %%
unique_words_using_vectorizer(df['content_coalesce'])
# %% [markdown]
# # Get stopwords
# %%
stop_words = retrieve_stop_words()
#apply same transformations as on the corpus
stop_words = [unidecode.unidecode(each_string.lower()) for each_string in stop_words]
# %%
len(stop_words)
# %% [markdown]
# # Wordcloud Per Category
# %%
test_df.initiative_type_alt.value_counts()
# %%
# example usage: test_df = create_df_from_json('./small-batch.json')
test_df = create_df_from_json('small-batch.json')
grouped = test_df.groupby("initiative_type_alt")['content'].apply(lambda tags: ','.join(tags))
def show_cloud(i):
text = grouped[i]
wordcloud = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(text)
plt.figure(figsize=(12,5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
for i, row in grouped.iteritems():
unique_id = i
print(i)
if len(grouped[i])>0:
show_cloud(i)
# %% [markdown]
# # Spot check
# %%
# list(df[df._id == 'ea99726d258373625ba3210f46c8ae07113ce965']['content'])
# %% [markdown]
# # Apply transformations one-by-one
# %% [markdown]
# ## Original content
# %%
unique_words_using_vectorizer(df['content_coalesce'])
# %% [markdown]
# ## Remove accents
# %%
df['t1_no_accents'] = df.apply(lambda row:remove_accents(row,'content_coalesce'),axis=1)
df['t1_no_accents'].head()
# %%
# list(df[df._id == 'ea99726d258373625ba3210f46c8ae07113ce965']['t1_no_accents'])
# %%
unique_words_using_vectorizer(df['t1_no_accents'])
# %% [markdown]
# ## Remove numbers
# %%
df['t2_no_numbers'] = remove_numbers(df['t1_no_accents'])
df['t2_no_numbers'].head()
# %%
unique_words_using_vectorizer(df['t2_no_numbers'])
# %% [markdown]
# ## Remove special characters
# %%
df['t3_no_special_char'] = df['t2_no_numbers'].apply(lambda row:replace_special_char(row))
df['t3_no_special_char'].head()
# %%
unique_words_using_vectorizer(df['t3_no_special_char'])
# %% [markdown]
# ## Lowercase all
# %%
df['t4_lowercase'] = df['t3_no_special_char'].str.lower()
df['t4_lowercase'].head()
# %%
unique_words_using_vectorizer(df['t4_lowercase'])
# %% [markdown]
# ## Remove Stopwords
# %%
df['t5_stopwords_removed'] = df['t4_lowercase'].apply(lambda row:remove_stopwords(row, stop_words))
df['t5_stopwords_removed'].head()
# %%
unique_words_using_vectorizer(df['t5_stopwords_removed'])
# %% [markdown]
# ## Stem words
# %%
df['t6_stemming'] = df["t5_stopwords_removed"].apply(lambda row: [stemmer.stem(x) for x in row.split(" ")])
df['t6_stemming'].head()
# %%
# THIS FLATTENS the list from list to str
df['t6_stemming'] = [' '.join(l) for l in df['t6_stemming']]
# %%
unique_words_using_vectorizer(df['t6_stemming'])
# %% [markdown]
# # Checkpoint
# %%
save_cols = ['_id', 'author_parliamentarygroups', 'created', 'initiative_type',
'initiative_type_alt', 'reference', 'status', 'tagged', 'title',
'updated', 'url', 'tags', 'topics', 'history', 'author_others', 'place',
'content', 'extra', 'author_deputies', 'content_coalesce', 't6_stemming']
df[save_cols].to_csv("preprocessed.csv")
# %%
# df2['tokenized_sents'] = df2.apply(lambda row: nlp(row['removed_num']), axis=1)
# df2['tokenized_sents_str'] = df2.tokenized_sents.apply(lambda x:str(x))
# infreq = freq_dataframe[freq_dataframe['Counts'] < 3 ]['Word'].tolist()
# df2['removed_infreq'] = df2.tokenized_sents_str.apply(lambda x: remove_stopwords(x,infreq))
# df2['removed_infreq_str'] = df2.removed_infreq.apply(lambda x:str(x))
# v = TfidfVectorizer()
# x = v.fit_transform(df2['removed_infreq_str'])
# x.todense()
# len(v.vocabulary_)
# %%
# from collections import Counter
# from itertools import chain
# counter = Counter(chain.from_iterable(map(str.split, df.t5_stopwords_removed.tolist())))
# df['count'] = df['content_coalesce'].str.split().str.len()
# df['count2'] = df['t5_stopwords_removed'].str.split().str.len()