<jupyter_start><jupyter_text>1. Imports<jupyter_code>import tweepy
import pandas    as pd
import numpy     as np
from IPython.display       import display
import matplotlib.pyplot    as plt
import seaborn             as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>**References**https://github.com/D-Favari/Analise-de-Sentimentos---Twitter/blob/master/Analise%20-%20Teste.ipynbhttps://www.dataquest.io/blog/tutorial-text-classification-in-python-using-spacy/https://medium.com/analytics-vidhya/training-your-own-sentiment-analyzer-with-spacy-9b924df1514c **You have to add your own keys here**<jupyter_code># Cliente:
CONSUMER_KEY        = ""
CONSUMER_SECRET     = ""

# Acesso:
ACCESS_TOKEN        = ""
ACCESS_SECRET       = ""
# API
def twitter_setup():
    # Autenticação e acesso
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
    
    # Retorna API com autenticação:
    api = tweepy.API(auth)
    return api
extractor = twitter_setup()

# Lista de perfis a terem dados extraídos
#tweets = extractor.user_timeline(screen_name="elonmusk", count=200)
# Lista de assuntos a serem extraídos
tweets = extractor.search(input("Topic you want to analyze: "), count=100,date_since = "2015-02-07",tweet_mode='extended')

print("Number of tweets extracted: {}.\n".format(len(tweets)))

# Printa as tweets mais recentes
print("5 recent tweets:\n")
for tweet in tweets[:5]:
    print(tweet.full_text)
    print()
data = pd.DataFrame(data=[tweet.full_text for tweet in tweets], columns=['Tweets'])
print(dir(tweets[0]))

print("\n")
# Exibe as informações de cada tweet
print(tweets[0].retweet_count)
print(tweets[0].retweeted)
print(tweets[0].full_text)
print(tweets[0].author.screen_name)
#print(tweets[0].retweets)
data['Len']   = np.array([len(tweet.full_text) for tweet in tweets])
data['ID']    = np.array([(tweet.id) for tweet in tweets])
data['Date']  = np.array([(tweet.created_at) for tweet in tweets])
data['Likes'] = np.array([(tweet.favorite_count) for tweet in tweets])
data['RTs']   = np.array([(tweet.retweet_count) for tweet in tweets])
data['User']  = np.array([(tweet.author.screen_name) for tweet in tweets])
data['Location'] = np.array([(tweet.author.location) for tweet in tweets])
# Exibe 10 primeiros elementos da tabela
display(data.tail(10))
data.shape<jupyter_output><empty_output><jupyter_text>2. Saving and reading the database<jupyter_code>data.to_csv('tweets_unconventional.csv')
data = pd.read_csv('tweets_unconventional2.csv')<jupyter_output><empty_output><jupyter_text>3. Starting the analysis<jupyter_code># Extraindo os tweets com mais likes e com mais RT

fav_max   = np.max(data['Likes'])
rt_max    = np.max(data['RTs'])

fav       = data[data.Likes == fav_max].index[0]
rt        = data[data.RTs == rt_max].index[0]

# Mais Liks:
print("O tweet com mais likes é: \n{}".format(data['Tweets'][fav]))
print("Número de likes: {}".format(fav_max))

# Break line
print("\n")

# Mais RTs:
print("O tweet com mais RTs é: \n{}".format(data['Tweets'][rt]))
print("Número de RTs:: {}".format(rt_max))
tfav = pd.Series(data=data['Likes'].values, index=data['Date'])
tret = pd.Series(data=data['RTs'].values, index=data['Date'])

tfav.plot(figsize=(16,4), label="Likes", legend=True)
tret.plot(figsize=(16,4), label="Retweets", legend=True)
import re
from textblob import TextBlob

def clean_tweet(tweet):
    # Remover links e caracteres especiais
    return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
    
def analize_sentiment(tweet):
    # Classificar a polaridade(positivo negativo) do tweet
    analysis = TextBlob(clean_tweet(tweet))
    if analysis.sentiment.polarity > 0:
        return 1
    elif analysis.sentiment.polarity == 0:
        return 0
    else:
        return -1
data.Tweets[15]
# Adicionando mais uma coluna à tabela com os resultados obtidos acima
data['SA'] = np.array([analize_sentiment(tweet) for tweet in data['Tweets']])
display(data.head(20))
# Classificando os tweets
positive = [tweet for index, tweet in enumerate(data["Tweets"]) if data['SA'][index] > 0]
neutral  = [tweet for index, tweet in enumerate(data["Tweets"]) if data['SA'][index] == 0]
negative = [tweet for index, tweet in enumerate(data["Tweets"]) if data['SA'][index] < 0]

# Exibindo as porcentagens
print("Porcentagem de tweets positivos: {}%".format(len(positive)*100/len(data['Tweets'])))
print("Porcentagem de tweets neutros: {}%".format(len(neutral)*100/len(data['Tweets'])))
print("Porcentagem de tweets negativos: {}%".format(len(negative)*100/len(data['Tweets'])))<jupyter_output>Porcentagem de tweets positivos: 26.41509433962264%
Porcentagem de tweets neutros: 15.09433962264151%
Porcentagem de tweets negativos: 58.490566037735846%<jupyter_text>4. Checking polarity and subjectivity<jupyter_code>def clean(text):
    # Removal of certain words and symbols
    # Re.sub = substitute any pattern we find that we want removed
    # Find characters with @
    # Look for characters [A-Z] & [a-z] & [0-9]
    # r tells python the expression is a raw string

    # Remove @
    text = re.sub(r'@[A-Za-z0-9]+', '', text) # Replace with Empty String

    # Remove #
    text = re.sub(r'#', '', text)

    # Remove Re-Tweets followed by one or more spaces
    text = re.sub('RT[\s]+', '', text)

    # Remove Hyperlinks from tweets followed by one or more white spaces
    text = re.sub(r'https?:\/\/\S+', '', text)

    text = text.lower()
    return text

data['Tweets'] = data['Tweets'].apply(clean)
# Subjectivity & Polarity

# Create a function to get the subjectivity
# How subjective or opinionated a text is
def getSubjectivity(text):
    return TextBlob(text).sentiment.subjectivity


# Create a function to get the polarity
# How positive or negative the a text is
def getPolarity(text):
    return TextBlob(text).sentiment.polarity


# Create two new columns (Subjectivity & Polarity)
data['Subjectivity'] = data['Tweets'].apply(getSubjectivity)
data['Polarity'] = data['Tweets'].apply(getPolarity)


# Show the updated dataframe with the new columns
data.head()
from wordcloud import WordCloud
# Plot Word Cloud
all_words = ' '.join( [twts for twts in data['Tweets']] )
wordCloud = WordCloud(width=500, height=300, random_state=21, max_font_size=110).generate(all_words)

plt.imshow(wordCloud, interpolation="bilinear")
plt.axis('off')
plt.show()
# Create a function to compute the negative, neutral and positive analysis

def getAnalysis(score):
    if score < 0:
        return 'Negative'
    elif score == 0:
        return 'Neutral'
    else:
        return 'Positive'

data['Analysis'] = data['Polarity'].apply(getAnalysis)
data.head()
plt.figure(figsize=(8, 6))
for i in range(0, data.shape[0]):
    # Plot Tweets
    plt.scatter(data['Polarity'][i], data['Subjectivity'][i], color='Blue')

# Label Plot
plt.title('Sentiment Analysis')
plt.xlabel('Polarity')
plt.ylabel('Subjectivity')

# Display Plot
plt.show()
# Show the value counts
data['Analysis'].value_counts()

# Plot and visualize the counts
plt.title('Sentiment Analysis')
plt.xlabel('Sentiment')
plt.ylabel('Counts')

# Plot as a BarPlot
data['Analysis'].value_counts().plot(kind='bar')
plt.show()
from wordcloud import WordCloud,STOPWORDS
def plot_wordcloud(df):
    words = '' 
    stopwords = set(STOPWORDS)  
    for val in df.values:  
        val = str(val) 
        tokens = val.split()
        for i in range(len(tokens)): 
            tokens[i] = tokens[i].lower() 
        words += " ".join(tokens)+" "
    wordcloud = WordCloud(width = 800, height = 800, 
                    background_color = "white", 
                    stopwords = stopwords, 
                    min_font_size = 10).generate(words)                     
    plt.figure(figsize = (10,8), facecolor = None) 
    plt.imshow(wordcloud) 
    plt.axis("off") 
    plt.tight_layout(pad = 0) 
    plt.show()
plot_wordcloud(data["Tweets"].where(data["Analysis"]=="Positive").dropna())
plot_wordcloud(data["Tweets"].where(data["Analysis"]=="Negative").dropna())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14855519881452212, 'fraction_numerical': 0.019510990367992097, 'mean_word_length': 4.472297297297297, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '338596', 'license': 'MIT', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2798, 'n_words': 648, 'path': 'Twitter.ipynb', 'repo_name': 'smalvar/GASBRAS-CNPq', 'size': '8098'}