Last active
March 23, 2018 14:52
-
-
Save gagejustins/ccc48c53af403cb139f86d34b78d7022 to your computer and use it in GitHub Desktop.
Code for sentiment analysis with Twitter demo
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
#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 |
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
#Convert the tweets into pandas dataframes | |
tesla = pd.DataFrame(tesla_sentiment) | |
comcast = pd.DataFrame(comcast_sentiment) | |
#Show descriptive statistics | |
tesla.describe() | |
comcast.describe() |
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 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" |
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 tweet in tesla_tweets: | |
print(tweet) |
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
#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 |
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
#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