From a39323f583e1878d3cdafed8de99b4614dab7682 Mon Sep 17 00:00:00 2001 From: s123188 Date: Sat, 31 May 2014 15:44:18 +0200 Subject: made a method for the posnegVisualizer --- src/main/Analyzor.java | 78 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java index b896f62..89a8403 100644 --- a/src/main/Analyzor.java +++ b/src/main/Analyzor.java @@ -188,7 +188,7 @@ public class Analyzor { while (data.next()) { //get brand - brand=data.getString("brand"); + brand = data.getString("brand"); //make hashmap for each brand if(!wordcloud.containsKey(brand)){ wordcloud.put(brand, new HashMap()); @@ -216,7 +216,7 @@ public class Analyzor { } } //print the words and their frequency in a csv file - mapToCSV(wordcloud, "wordcloud.csv", "brand,word,count"); + ssiMapToCSV(wordcloud, "wordcloud.csv", "brand,word,count"); } //generate csv for disco from the query @@ -284,8 +284,6 @@ public class Analyzor { } } - - //hashmap timezone, brand, amount HashMap> timeMap = new HashMap<>(); String timezone; @@ -299,6 +297,7 @@ public class Analyzor { timezone="other"; } brand = data.getString("brand"); + //if the timezone is already in the map if(timeMap.containsKey(timezone)){ //if the brand for that timezone is already in the map @@ -320,9 +319,55 @@ public class Analyzor { } } - //make the CSV out of the map - mapToCSV(timeMap, "timezone.csv", "timezone,brand,count"); + ssiMapToCSV(timeMap, "timezone.csv", "timezone,brand,count"); + } + + //gets the positivity of the tweets about a brand + //makes a csv file for posnegVisualizer + void posNeg(String query) throws SQLException, FileNotFoundException, UnsupportedEncodingException{ + query(query); + + String brand; + int rating; + int ratingInterval; + + int intervalSize = 10; + //brand, ratingInterval, amount + HashMap> posnegMap = new HashMap<>(); + /* + the rating interval is given by an integer, which is the result of the + tweets sentiment value divided by interval size rounded down. + This puts all data in boxes for the histogram. + */ + + while(data.next()){ + + brand = data.getString("brand"); + rating = data.getInt("rating"); + ratingInterval = rating/intervalSize; + + //if the brand is already in the map + if(posnegMap.containsKey(brand)){ + //if the brand for that brand is already in the map + if(posnegMap.get(brand).containsKey(ratingInterval)){ + //increment the amount + posnegMap.get(brand).put(ratingInterval, posnegMap.get(brand).get(ratingInterval) + 1); + } + //if the brand for that brand is not yet in the map + else{ + //make a new entry for that brand with amount = 1 + posnegMap.get(brand).put(ratingInterval, 1); + } + } + //if the brand is not yet in the map + else{ + //make a new hashmap for this map and fill it with the brand and the amount + posnegMap.put(brand, new HashMap()); + posnegMap.get(brand).put(ratingInterval, 1); + } + } + siiMapToCSV(posnegMap, "posneg.csv", "brand,ratingInterval,count"); } //replaces punctuation so it will be splitted @@ -347,7 +392,26 @@ public class Analyzor { //prints a hashmap into a csv for a html application //Hashmap> becomes key1, key2, value //only for String, String, Integer - void mapToCSV(HashMap> map, String fileName, String firstLine) + void ssiMapToCSV(HashMap> map, String fileName, String firstLine) + throws FileNotFoundException, UnsupportedEncodingException{ + + PrintWriter writer = new PrintWriter(fileName, "UTF-8"); + + writer.println(firstLine); + + //loop over brands + for(Entry en : map.entrySet()){ + //loop over words + for(Entry e : map.get(en.getKey()).entrySet()){ + writer.println(en.getKey() + "," + e.getKey() + "," + e.getValue()); + } + } + + writer.close(); + System.out.println("csv file made, please put it next to html file and run this"); + } + + void siiMapToCSV(HashMap> map, String fileName, String firstLine) throws FileNotFoundException, UnsupportedEncodingException{ PrintWriter writer = new PrintWriter(fileName, "UTF-8"); -- cgit v1.2.1