Instantly share code, notes, and snippets.

@gagejustins /call.py
Last active Mar 23, 2018

Embed
What would you like to do?
Code for sentiment analysis with Twitter demo
#Define your API Key from Algorithmia
apikey = 'YOUR_API_KEY'
#Initialize the Algorithmia client
client = Algorithmia.client(apikey)
#Create an instance of the RetrieveTweetsWithKeyword algorithm
algo = client.algo('diego/RetrieveTweetsWithKeyword/0.1.2')
#Call the algorithm for both of our keywords and store the results
tesla_tweets = algo.pipe(keyword1).result
comcast_tweets = algo.pipe(keyword2).result
#Convert the tweets into pandas dataframes
tesla = pd.DataFrame(tesla_sentiment)
comcast = pd.DataFrame(comcast_sentiment)
#Show descriptive statistics
tesla.describe()
comcast.describe()
#Import the packages we'll need
import pandas as pd
import Algorithmia
#Define the two companies who's sentiment we want to compare
keyword1 = "tesla"
keyword2 = "comcast"
for tweet in tesla_tweets:
print(tweet)
#Create an instance of the SocialSentimentAnalysis algorithm
algo = client.algo('nlp/SocialSentimentAnalysis/0.1.4')
#Call the algorithm on both of our sets of tweets and store the results
tesla_sentiment = algo.pipe(tesla_tweets_cleaned).result
comcast_sentiment = algo.pipe(comcast_tweets_cleaned).result
#Create an instance of the RemoveStopwords algorithm
algo = client.algo('nlp/RemoveStopwords/0.1.0')
#Call the algorithm on the two sets of tweets we gathered
tesla_tweets_cleaned = []
for tweet in tesla_tweets:
wordList = tweet.split(" ")
wordsToKeep = algo.pipe(wordList).result
tesla_tweets_cleaned.append(" ".join(wordsToKeep))
comcast_tweets_cleaned = []
for tweet in comcast_tweets:
wordList = tweet.split(" ")
wordsToKeep = algo.pipe(wordList).result
comcast_tweets_cleaned.append(" ".join(wordsToKeep))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment