Skip to content

Instantly share code, notes, and snippets.

View JLFDataScience's full-sized avatar

Jose Luis Fernández Nuevo JLFDataScience

  • FGCSIC
View GitHub Profile
@JLFDataScience
JLFDataScience / Tweets_extracction.py
Last active January 24, 2020 14:16
Tweets extracción advertools
#Extracción de tweets con "advertools" 💾¶
import advertools as adv
adv.twitter.set_auth_params(cred['consumer_key'],
cred['consumer_secret'],
cred['access_token'],
cred['access_token_secret'])
search_words = ['envejecimiento OR longevidad OR personasMayores']#terminos de búsqueda en twitter
df = adv.twitter.search(q=search_words, count=10000, tweet_mode='extended', lang='es');
@JLFDataScience
JLFDataScience / Clean_tweets.py
Last active January 24, 2020 14:16
Clean_tweets
#Nos quedamos con las columnas que parecen más interesantes
df_TWenv = df_TWenv[['tweet_created_at','tweet_id','user_screen_name','tweet_entities_hashtags',
'tweet_entities_mentions','tweet_full_text','tweet_retweet_count', 'user_followers_count','user_location']]
@JLFDataScience
JLFDataScience / Count_Retweets.py
Last active January 24, 2020 14:15
Count_Retweets
#Añadimos una columna para contabilizar si es RT (ReTweet)
pattern = re.compile(r'RT ')
df_TWenv['rt'] = df_TWenv['tweet_full_text'].apply(lambda x: True if pattern.findall(x) else False)
@JLFDataScience
JLFDataScience / New_rows.py
Last active January 24, 2020 14:15
New_rows
#Genero un nuevo df añadiendo filas nuevas cuando hay más de un mencionado
df_grafo = df_grafo.assign(mencionado=df_grafo['mencionado'].str.split(',')).explode('mencionado').reset_index(drop=True)
#Compruebo que no hay más de un nombre en la columna 'mencionado'
len(df_grafo[df_grafo['mencionado'].str.contains(',', na=False)])
#Generamos el df para el grafo quedando tres campos usuario (meción), mencionado
df_grafo =df_TWenv[['user_screen_name', 'tweet_entities_mentions', 'rt']]
df_grafo.columns = ['emisor','mencionado','rt']
df_grafo.head()
@JLFDataScience
JLFDataScience / cleaning_Tweets.py
Last active January 24, 2020 14:14
cleaning_Tweets
#Eliminamos las @
df_grafo['mencionado'] = df_grafo['mencionado'].str.replace(r'@', '')
df_grafo.head()
#Al haber dividido por la ',', quedarán espacios en el inicio del nombre. Intentamos eliminarnos para homogeneizar nombres
df_grafo['emisor'] = df_grafo['emisor'].str.replace(r' ', '')
#Eliminamos también los guión bajo
df_grafo['mencionado'] = df_grafo['mencionado'].str.replace(r'_', ' ')
df_grafo['emisor'] = df_grafo['emisor'].str.replace(r'_', ' ')
df_grafo[:10]
@JLFDataScience
JLFDataScience / Grapho_made.py
Last active January 24, 2020 14:14
Grapho_made
#Se genera el Grafo
import networkx as nx
#Formamos el grafo con el dataframe: emisor, mencionado. Si hay RT-> mismo comportamiento que si no lo hay
G = nx.DiGraph()
for i in df_grafo.index.values:
G.add_edge(df_grafo.loc[i]['emisor'],df_grafo.loc[i]['mencionado'])
nx.write_graphml(G, "Tweet_envejecimiento.graphml")
@JLFDataScience
JLFDataScience / Grapho_is_connected.py
Last active January 24, 2020 14:14
Grapho_is_connected
#Transformamos el grafo a No dirigido para realizar esta comprobación
GU = G.to_undirected()
print(nx.is_connected(GU))
# False
@JLFDataScience
JLFDataScience / TextBlob_traslation.py
Last active January 24, 2020 14:13
TextBlob Traslation
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from textblob import TextBlob
##TextBlob
text="Hola Mundo, qué divertido es vivir aquí!"
#test_traslate = TextBlob(text).translate(to='en')
#print(test_traslate)
analysis = TextBlob(text)
eng=analysis.translate(to='en')
@JLFDataScience
JLFDataScience / Vader_traslation.py
Last active January 24, 2020 14:13
Vader Traslation with Google traslator api
#For the use of this library we have to first perform a translation from Spanish to English
#Import the Google traslator API into Python (Googletrans): https://py-googletrans.readthedocs.io/en/latest/
#In order to install it must be done with the appropriate pip version, for this it is used at the conda prompt::
#>python -m pip install googletrans
from googletrans import Translator
translator = Translator()
##Biblioteca VADER
text = 'Hola Mundo, qué divertido es vivir aquí!'
text = translator.translate(text).text