Skip to content

Commit

Permalink
Adds sentiment analysis for whole case studies for youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
syfantid committed Sep 19, 2016
1 parent 723ec4e commit 2c1823e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/combiner/Combiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public static void main(String[] args) throws JSONException, IOException {
}*/

Analysis analysis = new Analysis(args[4].split("=")[1]);
//analysis.analyze("twitter");
//analysis.analyze("youtube");
analysis.analyzeCase();

analysis.SentimentAnalysis();
}

}
Expand Down
56 changes: 51 additions & 5 deletions src/sentiment/Analysis.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package sentiment;

import analytics.AnalyticsExtractor;
import mongo.MongoConnector;
import org.bson.types.ObjectId;
import javafx.util.Pair;
Expand Down Expand Up @@ -47,10 +46,39 @@ public Analysis(String collectionName) throws IOException {
}

/**
* Sentiment analysis of a case's tweets
* Sentiment analysis for the whole case
* @throws JSONException In case a field cannot be found
*/
public void analyzeCase() throws JSONException {
private void analyzeCaseTotal(String dbType) throws JSONException {
HashMap<ObjectId, JSONObject> objects;
Month caseTotal;
String key;
if(dbType.equals("twitter")) {
objects = mongoConnector.getFullTweets(); // Get all tweets
caseTotal = new Month("twitter_sentiment", "total");
} else {
objects = mongoConnector.getFullComments(); // Get all tweets
caseTotal = new Month("youtube_sentiment", "total");
}
for (JSONObject object : objects.values()) { // For each tweet
// Increment feelings
caseTotal.addFeelingCount(object.getJSONObject("emScores").getDouble("ANGER"),
object.getJSONObject("emScores").getDouble("DISGUST"),
object.getJSONObject("emScores").getDouble("FEAR"),
object.getJSONObject("emScores").getDouble("JOY"),
object.getJSONObject("emScores").getDouble("SADNESS"),
object.getJSONObject("emScores").getDouble("SURPRISE"));
}

caseTotal.finalizeFeelings();
writeFeelingsToFile(caseTotal);
}

/**
* Sentiment analysis of a case's tweets per month
* @throws JSONException In case a field cannot be found
*/
private void analyzeCase() throws JSONException {
HashMap<ObjectId, JSONObject> tweets = mongoConnector.getFullTweets(); // Get all tweets
HashMap<String, Month> months = new HashMap<>(); // To save emotions per month
String month;
Expand Down Expand Up @@ -97,7 +125,7 @@ private void writeFeelingsToFile(Month monthObject) {
writer.write(System.lineSeparator());
writer.write("SURPRISE" + " , " + monthObject.getSurpriseCount());
writer.write(System.lineSeparator());
writer.write("Total month tweets" + " : " + monthObject.getCount());
writer.write("Total tweets" + " : " + monthObject.getCount());
writer.write(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -135,7 +163,7 @@ private String getYear(String date) {
* @param dbType The type of the database; can be either twitter or youtube
* @throws IOException
*/
public void analyze(String dbType) throws IOException {
private void analyze(String dbType) throws IOException {
if(dbType.equals("twitter")) { // Analyze tweets
HashMap<ObjectId, String> tweetsToParse = mongoConnector.getParsedTweets();

Expand Down Expand Up @@ -207,5 +235,23 @@ private List<Pair<String, Double>> sentiment(String tweet) throws IOException {

return scores;
}

/**
* Calls all methods needed for sentiment analysis
* @throws IOException In case the file cannot open
* @throws JSONException In case the specified JSON field does not exist
*/
public void SentimentAnalysis() throws IOException, JSONException {
System.out.println("Calculating tweets' emotions...");
analyze("twitter"); // Calculate emotion scores for tweets
System.out.println("Calculating comments' emotions...");
analyze("youtube"); // Calculate emotion scores for comments
System.out.println("Calculating Twitter's emotions per month...");
analyzeCase(); // Analyze case's tweets per month
System.out.println("Calculating Twitter's emotions in total...");
analyzeCaseTotal("twitter"); // Analyze case's tweets in total
System.out.println("Calculating YouTube's emotions in total...");
analyzeCaseTotal("youtube"); // Analyze case's comments in total
}
}

0 comments on commit 2c1823e

Please sign in to comment.