Last active
March 14, 2020 09:41
-
-
Save ssaurel/f2320bebdee4fde46daac14f1ceefdd6 to your computer and use it in GitHub Desktop.
Bitcoin Sentiment Analyzer for the SSaurel's Blog
This file contains 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
package com.ssaurel.bitcoinsentiment; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map.Entry; | |
import java.util.Properties; | |
import edu.stanford.nlp.ling.CoreAnnotations; | |
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations; | |
import edu.stanford.nlp.pipeline.Annotation; | |
import edu.stanford.nlp.pipeline.StanfordCoreNLP; | |
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations; | |
import edu.stanford.nlp.trees.Tree; | |
import edu.stanford.nlp.util.CoreMap; | |
import twitter4j.Query; | |
import twitter4j.QueryResult; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.conf.ConfigurationBuilder; | |
public class BitcoinSentimentAnalyzer { | |
enum TypeSentiment { | |
VERY_NEGATIVE(0), NEGATIVE(1), NEUTRAL(2), POSITIVE(3), VERY_POSITIVE(4); | |
int index; | |
private TypeSentiment(int index) { | |
this.index = index; | |
} | |
public static TypeSentiment fromIndex(int index) { | |
for (TypeSentiment typeSentiment: values()) { | |
if (typeSentiment.index == index) { | |
return typeSentiment; | |
} | |
} | |
return TypeSentiment.NEUTRAL; | |
} | |
} | |
public static List < Status > searchTweets(String keyword) { | |
List < Status > tweets = Collections.emptyList(); | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setDebugEnabled(true).setOAuthConsumerKey("UiLHCETjD1SLKb4EL6ixm90Mv") | |
.setOAuthConsumerSecret("fDAqCfMQ6Azj1BbvXqS3f9HoPNM6BIGSV7jw3SUBu8TAaPPnBx") | |
.setOAuthAccessToken("58410144-m5F3nXtyZGNXFZzofNhYp3SQdNMrbfDLgZSvFMdOq") | |
.setOAuthAccessTokenSecret("PxlJJ3dRMlMiUf7rFAqEo4n0yLbiC6FC4hyvKF7ISBgdW"); | |
TwitterFactory tf = new TwitterFactory(cb.build()); | |
Twitter twitter = tf.getInstance(); | |
Query query = new Query(keyword + " -filter:retweets -filter:links -filter:replies -filter:images"); | |
query.setCount(100); | |
query.setLocale("en"); | |
query.setLang("en");; | |
try { | |
QueryResult queryResult = twitter.search(query); | |
tweets = queryResult.getTweets(); | |
} catch (TwitterException e) {} | |
return tweets; | |
} | |
public static TypeSentiment analyzeSentiment(String text) { | |
Properties props = new Properties(); | |
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); | |
StanfordCoreNLP pipeline = new StanfordCoreNLP(props); | |
int mainSentiment = 0; | |
if (text != null && text.length() > 0) { | |
int longest = 0; | |
Annotation annotation = pipeline.process(text); | |
for (CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) { | |
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); | |
int sentiment = RNNCoreAnnotations.getPredictedClass(tree); | |
String partText = sentence.toString(); | |
if (partText.length() > longest) { | |
mainSentiment = sentiment; | |
longest = partText.length(); | |
} | |
} | |
} | |
return TypeSentiment.fromIndex(mainSentiment); | |
} | |
public static void main(String[] args) { | |
HashMap < TypeSentiment, Integer > sentiments = new HashMap < BitcoinSentimentAnalyzer.TypeSentiment, Integer > (); | |
List < Status > list = searchTweets("bitcoin"); | |
for (Status status: list) { | |
String text = status.getText(); | |
TypeSentiment sentiment = analyzeSentiment(text); | |
Integer value = sentiments.get(sentiment); | |
if (value == null) { | |
value = 0; | |
} | |
value++; | |
sentiments.put(sentiment, value); | |
} | |
int size = list.size(); | |
System.out.println("Sentiments about Bitcoin on " + size + " tweets"); | |
for (Entry < TypeSentiment, Integer > entry: sentiments.entrySet()) { | |
System.out.println(entry.getKey() + " => " + (entry.getValue() * 100) / size + " %"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 42, Can you explain to me why you add :
return TypeSentiment.NEUTRAL;
Thanks,