forked from edcoMooc/Compiladores_Ciencia_Hackathon2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Frame.py
359 lines (286 loc) · 12.9 KB
/
Data_Frame.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Libraries
import tweepy
import pandas as pd
import numpy as np
import pickle
import tensorflow
from tensorflow import keras
import re
import string
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import TweetTokenizer
import requests
from bs4 import BeautifulSoup
from instagramy import InstagramUser
from py3pin.Pinterest import Pinterest
# Input Tokens of Development Twitter
flag = True
while flag:
try:
CONSUME_KEY = input('Consume Key: ')
CONSUME_SECRET = input('Consume Secret: ')
ACCESS_TOKEN = input('Acces Token: ')
ACCESS_TOKEN_SECRET = input('Acces Token Secret: ')
# Authorization
auth = tweepy.OAuthHandler(CONSUME_KEY, CONSUME_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# Querry Try
data = api.me()
flag = False
except:
# Error Message
print('Incorrect tokens, try again')
print('')
# Pinterest
flag = True
while flag:
try:
email = input('Pinterest Email: ')
password = input('Pinterest Password: ')
username = input('Pinterest User Name: ')
# Authorization
pinterest = Pinterest(email=email,
password=password,
username=username,
cred_root='cred root dir')
user_profile = pinterest.get_user_overview('grupobbva')
flag = False
except:
# Error Message
print('Incorrect tokens, try again')
print('')
# Number of desire tweets
num_tweets = 70
# Screen Names
sn_bbva = ['@BBVAInnovation', '@BBVA_Mex', '@bbva', '@BBVAresponde_es', '@BBVARe_mx', '@BBVA_Colombia', '@BBVASeguros_mx',
'@BBVAProvincial', '@BBVAResearch', '@bbva_argentina', '@BBVA_espana', '@Citibanamex', '@SantanderMx']
sn_banco = ['bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'citibanamex', 'santander']
sn_pais = ['GLOBAL', 'MX', 'GLOBAL', 'ES', 'MX', 'COL', 'MX', 'GLOBAL', 'GLOBAL', 'AR', 'ES', 'MX', 'MX', 'MX']
# Data Frame
tweet = tweepy.Cursor(api.user_timeline, screen_name = '@bbva_peru', tweet_mode = "extended", include_rts = False).items(num_tweets)
tl = pd.DataFrame(t.__dict__ for t in tweet)
tl['banco'] = ['bbva' for x in range(len(tl))]
tl['pais'] = ['PE' for x in range(len(tl))]
tl['procedencia'] = ['oficial' for x in range(len(tl))]
tl['tema'] = ['banco' for x in range(len(tl))]
# Screen Name and number of desired tweets inputs
flag2 = True
while flag2:
try:
for i in range(len(sn_bbva)):
# Querry
tweet = tweepy.Cursor(api.user_timeline, screen_name = sn_bbva[i],
tweet_mode = "extended", include_rts = False).items(num_tweets)
tl_aux = pd.DataFrame(t.__dict__ for t in tweet)
tl_aux['banco'] = [sn_banco[i] for x in range(len(tl_aux))]
tl_aux['pais'] = [sn_pais[i] for x in range(len(tl_aux))]
tl_aux['procedencia'] = ['oficial' for x in range(len(tl_aux))]
tl_aux['tema'] = ['banco' for x in range(len(tl_aux))]
tl = pd.concat([tl, tl_aux])
flag2 = False
except:
# Error Message
print('Incorrect screen name or integer, try again')
print('')
# Querries
q_bbva = ['bbva mexico', 'bbva tarjeta', 'bbva credito', 'bbva españa', 'bbva perú', 'bbva servico', 'bbva colombia',
'bbva pago', 'bbva telefono', 'bbva sucursal', 'bbva viral', 'bbva cobrar', 'bbva cuenta', 'bbva app',
'santander mexico', 'santander tarjeta', 'santander servicio', 'santander app', 'citibanamex tarjeta',
'citibanamex mexico', 'citibanamex servicio', 'citibanamex app', 'coronavirus bbva', 'pandemia bbva', 'sanitaria bbva']
q_banco = ['bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva', 'bbva',
'santander', 'santander', 'santander', 'santander', 'citibanamex', 'citibanamex', 'citibanamex', 'citibanamex',
'bbva', 'bbva', 'bbva']
q_pais = ['MX', 'GLOBAL', 'GLOBAL', 'ES', 'PE', 'GLOBAL', 'COL', 'GLOBAL', 'GLOBAL', 'GLOBAL', 'GLOBAL', 'GLOBAL', 'GLOBAL',
'GLOBAL', 'MX', 'GLOBAL', 'GLOBAL', 'GLOBAL','GLOBAL', 'MX', 'GLOBAL', 'GLOBAL', 'GLOBAL', 'GLOBAL', 'GLOBAL']
q_tema = ['MX', 'tarjeta', 'credito', 'ES', 'PE', 'servicio', 'CL', 'pago', 'telefono', 'sucursal', 'viral', 'cobrar', 'cuenta',
'app', 'MX', 'tarjeta', 'servicio', 'app','tarjeta', 'MX', 'servicio', 'app', 'coronavirus', 'coronavirus', 'coronavirus']
# Data Frame
tweet2 = tweepy.Cursor(api.search, q = 'bbva', tweet_mode = "extended", lang = 'es').items(num_tweets)
bbva = pd.DataFrame(t.__dict__ for t in tweet2)
bbva['banco'] = ['bbva' for x in range(len(bbva))]
bbva['pais'] = ['GLOBAL' for x in range(len(bbva))]
bbva['procedencia'] = ['usuario' for x in range(len(bbva))]
bbva['tema'] = ['banco' for x in range(len(bbva))]
flag2 = True
while flag2:
try:
for i in range(len(q_bbva)):
tweet2 = tweepy.Cursor(api.search, q = q_bbva[i], tweet_mode = "extended", lang = 'es').items(num_tweets)
bbva_aux = pd.DataFrame(t.__dict__ for t in tweet2)
bbva_aux['banco'] = [q_banco[i] for x in range(len(bbva_aux))]
bbva_aux['pais'] = [q_pais[i] for x in range(len(bbva_aux))]
bbva_aux['procedencia'] = ['usuario' for x in range(len(bbva_aux))]
bbva_aux['tema'] = [q_tema[i] for x in range(len(bbva_aux))]
bbva = pd.concat([bbva, bbva_aux])
flag2 = False
except: # Exception
print('Querry not found or too many tweets requested, try again')
print('')
bbva = bbva[["author", "created_at", "entities", "favorite_count", "full_text", 'lang', 'retweet_count', 'source', 'banco', 'pais', 'procedencia', 'tema']]
tl = tl[["author", "created_at", "entities", "favorite_count", "full_text", 'lang', 'retweet_count', 'source', 'banco', 'pais', 'procedencia', 'tema']]
df = pd.concat([bbva, tl])
# Extracting name and location
s_name = [x.screen_name for x in df.author]
locat = [x.location for x in df.author]
follo = [x.followers_count for x in df.author]
friends = [x.friends_count for x in df.author]
veri = [x.verified for x in df.author]
# Adding extracted features
df["screen_Name"] = s_name
df["location"] = locat
df["followers"] = follo
df["friends"] = friends
df["verified"] = veri
# Dropping Author feature
df.drop('author', axis = 1, inplace=True)
# Extrating hashtags from tweets
hashtags = []
for x in df.entities:
try:
hashtags.append(dict(x["hashtags"][0])["text"])
except:
hashtags.append("NONE")
# Dummy Variable if tweet has a mention
mentions = []
for x in df.entities:
try:
aux = dict(x["user_mentions"][0])["screen_name"]
mentions.append(1)
except:
mentions.append(0)
# Adding new features
df["hashtag"] = hashtags
df["mention"] = mentions
# Ordering Features
df = df[["screen_Name", "location", "created_at", "lang", "full_text", "favorite_count",
"retweet_count", "hashtag", "mention", "source", "followers", "friends", "verified", 'banco', 'pais',
'procedencia', 'tema']]
def process_tweet(tweet):
"""Process tweet function.
Input:
tweet: a string containing a tweet
Output:
tweets_clean: a list of words containing the processed tweet
"""
stemmer = PorterStemmer()
stopwords_english = stopwords.words('spanish')
# remove stock market tickers like $GE
tweet = re.sub(r'\$\w*', '', tweet)
# remove old style retweet text "RT"
tweet = re.sub(r'^RT[\s]+', '', tweet)
# remove hyperlinks
tweet = re.sub(r'https?:\/\/.*[\r\n]*', '', tweet)
# remove hashtags
# only removing the hash # sign from the word
tweet = re.sub(r'#', '', tweet)
# tokenize tweets
tokenizer = TweetTokenizer(preserve_case=False, strip_handles=True,
reduce_len=True)
tweet_tokens = tokenizer.tokenize(tweet)
tweets_clean = []
for word in tweet_tokens:
if (word not in stopwords_english and # remove stopwords
word not in string.punctuation): # remove punctuation
# tweets_clean.append(word)
stem_word = stemmer.stem(word) # stemming word
tweets_clean.append(stem_word)
return tweets_clean
preprocess_list = np.array([process_tweet(x) for x in df.full_text])
# Neural Network
model = tensorflow.keras.models.load_model("C:/Users/olver/Documents/Edgar/Hackathon_BBVA/Models/Modelo_Ian_7.h5")
# Tokenizer
with open('C:/Users/olver/Documents/Edgar/Hackathon_BBVA/Models/tokenizer_bbva3.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
# Sequence convertion
maxlen = 100
sequences = tokenizer.texts_to_sequences(preprocess_list)
data = keras.preprocessing.sequence.pad_sequences(sequences, maxlen=maxlen)
# Prediction
predict = (model.predict_proba(data)[:,1] >= 0.448)
polarity = []
for i in predict:
if i:
polarity.append('Negativo')
else:
polarity.append('Positivo')
# Sentiments append
df['Sentiment'] = polarity
df.reset_index(drop = True, inplace = True)
# Adding Characters
df['caracteres'] = [len(x) for x in df.full_text]
df['id'] = [str(df.screen_Name[i] + '_' + str(df.created_at[i]) + '_' + str(df.caracteres[i])).replace(" ", ";") for i in range(len(df))]
# Reset Index
df.drop_duplicates('id', inplace = True)
df.reset_index(drop = True, inplace = True)
df_final = df[['id', 'screen_Name', 'location', 'created_at', 'lang', 'full_text',
'favorite_count', 'retweet_count', 'hashtag', 'mention', 'source',
'followers', 'friends', 'verified', 'banco', 'pais', 'Sentiment',
'caracteres', 'procedencia', 'tema']]
#Engagement per post
engage=[]
engage=np.array([((df_final.iloc[x].retweet_count + df_final.iloc[x].favorite_count)/(df_final.iloc[x].followers + 0.0001))*100 for x in range(df.shape[0])])
df_final['Engagement'] = engage
# Droping BBVA League
df_final = df_final[~df_final.full_text.str.contains("@LigaBBVAMX")]
df_final.reset_index(drop = True, inplace = True)
df_final['red_social'] = ['twitter' for x in range(len(df_final))]
# Other Social Network
user_profile = pinterest.get_user_overview('grupobbva')
BBVA_BOARDS= pinterest.search(scope='boards', query='bbva')
BBVA_PINS = pinterest.search(scope='pins', query='bbva')
engage_bbva = (len(BBVA_BOARDS) + len(BBVA_PINS) + user_profile['profile_reach'])/user_profile['follower_count']
# Intagram
bancos = ["bbva_mex", "citibanamex", "santander_mex"]
instametric = []
datas = []
for cuenta in bancos:
html = requests.get('https://www.instagram.com/' + cuenta + '/')
soup = BeautifulSoup(html.text, 'lxml')
item = soup.select_one("meta[property='og:description']")
user = InstagramUser(cuenta)
name = user.username
biografia = user.biography
seguidores = user.number_of_followers
seguidos = user.number_of_followings
posts = user.number_of_posts
datosins = [name, biografia, seguidores, seguidos, posts]
#obtenemos las métricas generales de cada banco
instametric.append(datosins)
instapost = pd.DataFrame(user.posts)
instapost.insert(0, "Banco", [cuenta for i in range(12)] )
datas.append(instapost)
instapostF = pd.concat(datas)
instapostF = instapostF.drop(['url'], axis=1)
instametric = pd.DataFrame(instametric)
instametric.columns = ['Banco','Biografía','Seguidores','Seguidos','Post']
foll = dict(zip(instametric.Banco, instametric.Seguidores))
instapostF['followers'] = [foll[x] for x in instapostF.Banco]
instapostF.reset_index(drop = True, inplace = True)
eng = []
for i in range(len(instapostF)):
eng.append((instapostF.comment[i] + instapostF.likes[i])/instapostF.followers[i])
instapostF['engagement'] = eng
# New Ids
ids = []
for i in range(len(instapostF)):
ids.append(str(instapostF.Banco[i]) + str(instapostF.timestamp[i]))
instapostF['id'] = ids
# Dictionary bank engagement
pint_dict = dict(zip(instapostF['Banco'].unique(), [engage_bbva, 0, 0]))
# Pinterest
instapostF['Pinterest'] = [pint_dict[x] for x in instapostF.Banco]
# Saving file
try:
base = pd.read_excel('BBVA.xlsx', sheet_name = 'BBVA')
base_final = pd.concat([df_final, base])
base_final.drop_duplicates('id', inplace = True)
base_final.reset_index(drop = True, inplace = True)
x = pd.ExcelWriter('BBVA.xlsx', engine = 'openpyxl')
base_final.to_excel(x, sheet_name = 'BBVA', index = False)
x.save()
except:
x = pd.ExcelWriter('BBVA.xlsx', engine = 'openpyxl')
df_final.to_excel(x, sheet_name = 'BBVA', index = False)
x.save()