-
Notifications
You must be signed in to change notification settings - Fork 1
/
0_normalizing.py
325 lines (267 loc) · 10.9 KB
/
0_normalizing.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
################# LIBRERIE #################
import os
import json
import pandas as pd
#import pprint
import seaborn as sns
import numpy as np
from collections import Counter
from matplotlib import pyplot as plt
#import statistics
import gc
#from IPython.display import clear_output
import multiprocessing
#from functools import partial
from multiprocessing import Pool
#from pandarallel import pandarallel
import swifter
# from swifter import set_defaults
# cores = multiprocessing.cpu_count()
# num_process = cores
# set_defaults(
# npartitions=cores,
# dask_threshold=1,
# scheduler="processes",
# progress_bar=True,
# progress_bar_desc=None,
# allow_dask_on_strings=False,
# force_parallel=False,
# )
from tqdm import tqdm, tqdm_pandas
# nlp
from num2words import num2words
#import string
#from bs4 import BeautifulSoup
#import time
from textblob import TextBlob
#from pygments import highlight
#from pygments.lexers import get_lexer_by_name
#from pygments.formatters import HtmlFormatter
#from pygments.lexers import guess_lexer
#import emoji
#import demoji
import re
import contractions
#from contractions import contractions_dict
import nltk
from nltk.tokenize import word_tokenize
#nltk.download('stopwords')
#nltk.download('punkt')
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
#nltk.download('wordnet')
from nltk.corpus import stopwords
#import gensim
#from gensim.utils import simple_preprocess
#import gensim.corpora as corpora
# nltk.download('averaged_perceptron_tagger')
# nltk.download('tagsets')
# nltk.download('universal_tagset')
# download
# Scarica il corpus delle stopwords inglesi
#nltk.download('stopwords')
# Ottieni la lista delle stopwords inglesi
# stop_words_en = stopwords.words('english')
################# FUNZIONI #################
# TEXT NORMALIZATION
def split_string(string):
return string.split("</s><s>")
def remove_html_tags(sentence):
pattern = re.compile("<.*?>")
cleaned_sentence = re.sub(pattern,'',sentence).strip()
return cleaned_sentence
def remove_html_entities(sentence):
pattern = re.compile("&[a-z0-9]+|&#[0-9]{1,6}|&#x[0-9a-f]{1,6}")
cleaned_sentence = re.sub(pattern,'',sentence).strip()
return cleaned_sentence
def remove_extra_whitespaces(text):
cleaned_sentence = re.sub(r'^\s*|\s\s*', ' ', text).strip()
return cleaned_sentence
def remove_urls(sentence):
http_pattern = re.compile(r"http\S+")
cleaned_sentence = re.sub(http_pattern,'',sentence).strip()
www_pattern = re.compile(r"www\S+")
cleaned_sentence = re.sub(www_pattern,'',cleaned_sentence)
return cleaned_sentence
def remove_emoji(phrase):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", flags=re.UNICODE)
cleaned_sentence = emoji_pattern.sub(r'', phrase)
return cleaned_sentence
# Conversione ordinali 1st 2nd etc in words
def replace_ordinal_numbers(text):
re_results = re.findall('(\d+(st|nd|rd|th))', text)
for enitre_result, suffix in re_results:
num = int(enitre_result[:-len(suffix)])
text = text.replace(enitre_result, num2words(num, ordinal=True))
return text
# Replace numbers with words (lone numbers)
def replace_numbers(phrase):
cleaned_sentence = re.sub(r"\s\d+\s", lambda x: f" {num2words(x.group())} ", phrase)
return cleaned_sentence
# Rimuovi i rimanenti
def remove_numbers(phrase):
cleaned_sentence=re.sub(r'\d+', '',phrase)
return cleaned_sentence
# Funzione finale
def process_numbers(phrase):
phrase = replace_ordinal_numbers(phrase)
try:
phrase = replace_numbers(phrase)
phrase = remove_numbers(phrase)
except:
phrase = remove_numbers(phrase)
return phrase
def remove_reddit_tags(phrase):
cleaned_sentence = re.sub(r'(\\ u \\|u \/|r \/|\\ r \\)', '',phrase)
return cleaned_sentence
def remove_control_chars(phrase):
cleaned_sentence = re.sub(r'(\\ u |\\ n |\\ v |\\ m | \\ )', ' ',phrase)
return cleaned_sentence
def process_age(phrase):
cleaned_sentence = re.sub(r'(\b\d{2})f\b', r'\1 female',phrase)
cleaned_sentence = re.sub(r'(\b\d{2})m\b', r'\1 male',phrase)
return cleaned_sentence
# WORD PROCESSING
def to_lower(phrase):
phrase = phrase.lower()
return phrase
def character_repeatation(text):
# Pattern matching for all case alphabets
# \1 It refers to the first capturing group.
# {2,} It means we are matching for repetition that occurs more than two times (or equal).
# r’\1\1' → It limits all the repetition to two characters.
Pattern_alpha = re.compile(r"([A-Za-z])\1{2,}", re.DOTALL)
# Limiting all the repeatation to two characters.
Formatted_text = Pattern_alpha.sub(r"\1\1", text)
# Pattern matching for all the punctuations that can occur
Pattern_Punct = re.compile(r'([.,/#!$%^&*?;:{}=_`~()+-])\1{1,}')
# Limiting punctuations in previously formatted string to only one.
Combined_Formatted = Pattern_Punct.sub(r'\1', Formatted_text)
return Combined_Formatted
def fix_and_expand_eng_contradictions(phrase):
# Remove whitespaces before '
phrase = re.sub(r"\s+'", "'", phrase)
# Avvicina parole tipo "was n't" a "wasn't"
phrase = re.sub(r"\s+n't", "n't", phrase)
phrase = contractions.fix(phrase, slang=True)
return phrase
def correct_me(text):
textBlb = TextBlob(text)
textCorrected = str(textBlb.correct()) # Correcting the text
return textCorrected
def remove_special_characters_punctuations(sentence):
cleaned_text = re.sub(r"[^a-zA-Z]+",' ',sentence).strip()
return cleaned_text
def text_normalization(sentences):
# Text Cleaning
sentences = list(map(remove_html_tags, sentences))
sentences = list(map(remove_html_entities, sentences))
sentences = list(map(remove_extra_whitespaces, sentences))
sentences = list(map(remove_urls, sentences))
sentences = list(map(remove_emoji, sentences))
sentences = list(map(process_numbers, sentences))
#sentences = list(map(remove_reddit_tags, sentences))
#sentences = list(map(remove_control_chars, sentences))
sentences = list(map(process_age, sentences))
sentences = list(map(remove_extra_whitespaces, sentences))
# Word Processing
sentences = list(map(to_lower, sentences))
sentences = list(map(character_repeatation, sentences))
sentences = list(map(fix_and_expand_eng_contradictions, sentences))
sentences = list(map(correct_me, sentences))
sentences = list(map(remove_special_characters_punctuations, sentences))
return sentences
# WORD ANALYSIS
def stop_words_1char_removal(text, stopwords_list = stopwords.words('english')):
return ([word for word in text if (word not in stopwords_list) and (len(word) > 1)])
def lemmaSentence(token_words):
lemma_text=[]
for word in token_words:
lemma_text.append(WordNetLemmatizer().lemmatize(word))
return lemma_text
def text_Tokenization(sentences):
# Tokenization
return list(map(word_tokenize, sentences))
def text_stop_words_1char_removal(sentences):
# Stopwords removal
return list(map(stop_words_1char_removal, sentences))
def text_lemmatizer(senteces):
return list(map(lemmaSentence, senteces))
def text_normalization_full(sentences):
# Text Cleaning
try:
sentences = list(map(remove_html_tags, sentences))
sentences = list(map(remove_html_entities, sentences))
sentences = list(map(remove_extra_whitespaces, sentences))
sentences = list(map(remove_urls, sentences))
sentences = list(map(remove_emoji, sentences))
sentences = list(map(process_numbers, sentences))
#sentences = list(map(remove_reddit_tags, sentences))
#sentences = list(map(remove_control_chars, sentences))
sentences = list(map(process_age, sentences))
sentences = list(map(remove_extra_whitespaces, sentences))
# Word Processing
sentences = list(map(to_lower, sentences))
sentences = list(map(character_repeatation, sentences))
sentences = list(map(fix_and_expand_eng_contradictions, sentences))
#sentences = list(map(correct_me, sentences))
sentences = list(map(remove_special_characters_punctuations, sentences))
# Word analysis
sentences = list(map(word_tokenize, sentences))
sentences = list(map(stop_words_1char_removal, sentences))
sentences = list(map(lemmaSentence, sentences))
except:
print(sentences)
return sentences
def extract_tags(document_tags: list):
doc_tags = pd.Series(document_tags)
doc_tags = doc_tags.apply(lambda subList: pd.Series(subList))
doc_tags = doc_tags.applymap(lambda wordTagTuple: wordTagTuple[1] if type(wordTagTuple)==tuple else '')
return doc_tags.values
def POS_tagging(document: list, tagset:str = 'universal', lang:str='eng'):
POS_tags = nltk.tag.pos_tag_sents(document, tagset=tagset, lang=lang)
POS_tags = extract_tags(POS_tags)
return [list(filter(None, l.tolist())) for l in POS_tags]
################# MAIN #################
if __name__ == "__main__":
# Definizione parametri multiprocessing
#print(f"Core utilizzati:{num_process} \n")
### Import ###
print(" Starting processing ...\n")
prefix = "./Dataset_splitted/"
#files = [prefix+f for f in os.listdir(prefix)]
files = sorted(os.listdir(prefix))
print(files)
for file in files:
print(f"Importing, parsing, sentences splitting, text normalization, tokenization, stop words removal, lemmatization and POS for {file} ...\n")
ds = pd.read_json(f'./Dataset_splitted/{file}', orient="records", lines=True)
print(f"Dimensioni: {ds.shape}")
# Sentence splitting
ds["document"] = ds["document"].apply(split_string)
# Normalizing in multiprocessing
ds["document_normalized"] = ds["document"].swifter.apply(text_normalization_full)
# POS
print(f"POS tagging {file} ...\n")
ds["pos_tags"] = ds["document_normalized"].swifter.apply(POS_tagging)
# SAVING
print(f"Saving: ./Dataset_splitted/{file} \n")
ds.to_json(f"ProcessedData/{file}", orient="records", lines=True)