This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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']] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Transformamos el grafo a No dirigido para realizar esta comprobación | |
GU = G.to_undirected() | |
print(nx.is_connected(GU)) | |
# False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
OlderNewer