-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_preprocess.py
147 lines (105 loc) · 4.92 KB
/
data_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
import re
import csv
import numpy as np
import pandas as pd
from googletrans import Translator
from textblob import TextBlob
from nltk.corpus import stopwords
from rake_nltk import Metric, Rake
################################################################################################################################################
stop_words = set(stopwords.words('english')) ## ENGLISH STOP-WORD SET
regex = re.compile(r'[\n\r\t]') ## TO REMOVE UNNECESSARY CHARACTERS LIKE SPACE,TABS,etc.
"""
READING THE 'updated_train_data.csv' FILE AND TAKING THE DATA AND CREATING DATAFRAME
"""
dict = {'title':[],'link':[],'description':[],'long_description':[],'id':[]}
with open(r'E:\prog\Train_data\updated_train_data.csv', encoding="utf8") as csvfile:
for line in csvfile:
fields = line.split('|')
dict['title'].append(fields[0])
dict['link'].append(fields[1])
dict['description'].append(fields[2])
dict['long_description'].append(fields[3])
dict['id'].append(regex.sub(" ", fields[4]))
df1 = pd.DataFrame.from_dict(dict)
###############################################################################################################################################
"""
TRANSLATING THE TEXTS INTO ENGLISH LANGUAGE. TAKING ONLY TITLE AND DESCRIPTION COLUMNS FOR
EASYNESS. FOR TRANSLATION USING 'GOOGLETRANS' AND 'TEXTBLOB' PACKAGES FOR BETTER PERFORMANCE. THEN EXTRACTING
KEY PHRASES FROM THEM USING 'RAKE'(NLTK package) AND STORED IN A NEW DATAFRAME COLUMN 'keyword_set'.
"""
columns = ['title','description']
temp = []
translator = Translator()
r = Rake(stopwords=stop_words,ranking_metric=Metric.DEGREE_TO_FREQUENCY_RATIO,min_length=2, max_length=3)
n = 0
t = 500
dest_t = len(df1)
while n < dest_t :
for i in range(n,t) :
for j in columns :
print("df1[j][i] = ",df1[j][i],"i==",i)
if (len(df1[j][i]) >= 3):
blob = TextBlob(str(df1[j][i]))
try:
language = blob.detect_language()
if (language != 'en'):
translated = blob.translate(to='en')
df1[j][i] = translated
else:
df1[j][i] = df1[j][i]
except:
df1[j][i] = translator.translate(df1[j][i]).text
else :
df1[j][i] = translator.translate(df1[j][i]).text
r.extract_keywords_from_text(str(df1[j][i]))
for k in r.get_ranked_phrases_with_scores():
if k[0] >=4:
if k[1].find('https :// www') != -1 or k[1].find('https :// ') != -1 or k[1].find('...') != -1 :
temp.append([i,k[1].replace('https :// ','')])
else:
temp.append([i,k[1]])
print("Keyword-->",k[1])
n = n + 500
t = t + 500
######### STORING THE KEY PHRASES INTO DATAFRAME #########
df1["keyword_set"] = ""
for i in range(0,dest_t):
temp1 = []
del temp1[:]
element_index = list(filter(lambda x: temp[x][0] == i, range(0,len(temp)))) ## will give list of matched index
for j in element_index:
temp1.append(temp[j][1])
df1.at[i,'keyword_set'] = (temp1)
###########################################################################################################################################################################
"""
THE FUNCTION WILL CLEAN BACKLASHES,NONALPHABETS,WHITESPACES AND WILL TRANSFORM INTO LOWERCASE ALPHABETS. THIS FUNCTION HAS BEEN
IMPLEMENTED ON 3 DATAFRAME COLUMNS i.e. 'title','description','long_description' .
"""
def clean_text(text):
# remove backslash-apostrophe
text = re.sub("\'", "", text)
# remove everything except alphabets
text = re.sub("[^a-zA-Z]"," ",text)
text = re.sub(r"\s+[a-zA-Z]\s+", ' ', text)
# remove whitespaces
text = ' '.join(text.split())
# convert text to lowercase
text = text.lower()
return text
df1['title'] = df1['title'].apply(lambda x: clean_text(str(x)))
df1['description'] = df1['description'].apply(lambda x: clean_text(str(x)))
df1['long_description'] = df1['long_description'].apply(lambda x: clean_text(str(x)))
#################################################################################################################################################################
"""
THIS FUNCTION WILL REMOVE STOPWORDS FROM 3 DATAFRAME COLUMNS i.e. 'title','description','long_description' .
"""
# function to remove stopwords
def remove_stopwords(text):
no_stopword_text = [w for w in text.split() if not w in stop_words]
return ' '.join(no_stopword_text)
df1['title'] = df1['title'].apply(lambda x: remove_stopwords(x))
df1['description'] = df1['description'].apply(lambda x: remove_stopwords(x))
df1['long_description'] = df1['long_description'].apply(lambda x: remove_stopwords(x))
##################################################################################################################################################################
df1[1::].to_csv(r'E:\prog\Train_data\updated_train_data_update1.csv') ## CONVERTING THE DATAFRAME INTO CSV FILE