Skip to content

Instantly share code, notes, and snippets.

@gagejustins
Last active March 23, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagejustins/ccc48c53af403cb139f86d34b78d7022 to your computer and use it in GitHub Desktop.
Save gagejustins/ccc48c53af403cb139f86d34b78d7022 to your computer and use it in GitHub Desktop.
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