From 00baf4ffc86dac7b723c4fc3d2c963d1fa84729b Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 26 May 2014 12:03:33 +0200 Subject: Formatting, drop useless license header Sam Hocevar doesn't know us nor does he have copyright about this work... --- src/analysis/BrandChecker.java | 32 ++++++--------------- src/database/QueryUtils.java | 8 +++--- src/main/Analyzor.java | 57 ++++++++++++++++++------------------- test/analysis/BrandCheckerTest.java | 4 +-- 4 files changed, 42 insertions(+), 59 deletions(-) diff --git a/src/analysis/BrandChecker.java b/src/analysis/BrandChecker.java index 10e22b4..ee9c7b4 100644 --- a/src/analysis/BrandChecker.java +++ b/src/analysis/BrandChecker.java @@ -1,17 +1,3 @@ -/* - * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 - * - * Copyright (C) 2004 Sam Hocevar - * - * Everyone is permitted to copy and distribute verbatim or modified copies - * of this license document, and changing it is allowed as long as the name is - * changed. - * - * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, - * DISTRIBUTION AND MODIFICATION - * - * 0. You just DO WHAT THE FUCK YOU WANT TO. - */ package analysis; import java.io.FileInputStream; @@ -93,7 +79,7 @@ public class BrandChecker { if (line.isEmpty()) { return; } - + if (!line.contains("-")) { System.err.println("illformatted rule: " + line + ", missing -"); } else { @@ -110,13 +96,13 @@ public class BrandChecker { // Read the line. String name = parts[0].trim(); - + // Read the positive words. - String positive = parts[1].replaceAll(" ",""); + String positive = parts[1].replaceAll(" ", ""); String[] sequence = positive.split(","); - + if (parts.length == 3) { - String negative = parts[2].replaceAll(" ", ""); + String negative = parts[2].replaceAll(" ", ""); String[] blacklist = negative.split(","); ruleset.add(new BrandRule(name, sequence, blacklist)); } else { @@ -139,7 +125,7 @@ public class BrandChecker { * The words that should be in the text. */ private final HashMap names; - + /** * A blacklist of words that are not interesting. */ @@ -164,7 +150,7 @@ public class BrandChecker { } else { this.blacklist = null; } - + for (String name : names) { this.names.put(name, Boolean.FALSE); } @@ -177,7 +163,7 @@ public class BrandChecker { */ public boolean analyze(String[] words) { reset(); - + int found = 0; for (String word : words) { @@ -201,7 +187,7 @@ public class BrandChecker { public String getBrand() { return brand; } - + private void reset() { for (String name : this.names.keySet()) { this.names.put(name, Boolean.FALSE); diff --git a/src/database/QueryUtils.java b/src/database/QueryUtils.java index 2cc6fd6..b95903f 100644 --- a/src/database/QueryUtils.java +++ b/src/database/QueryUtils.java @@ -1,7 +1,6 @@ package database; import java.sql.SQLException; -import java.util.Locale; /** * Utilities to create queries. @@ -9,8 +8,9 @@ import java.util.Locale; * @author Maurice Laveaux */ public class QueryUtils { - public final static String insertRating - = buildQuery("mentionsbrand", new String[]{"tweetid","brand"},"tweetid","brand", "rating"); + + public final static String insertRating + = buildQuery("mentionsbrand", new String[]{"tweetid", "brand"}, "tweetid", "brand", "rating"); public final static String insertProfile = buildQuery("twitteruser", new String[]{"userid"}, "userid", "displayname", "timezone", "tweetcount", "followercount", @@ -96,7 +96,7 @@ public class QueryUtils { statement.setLong("tweetid", tweetid); statement.setInt("rating", rating); statement.setString("brand", brand); - + } public static void setInsertBrandParams(NamedPreparedStatement brandStmt, diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java index 5385a79..ffd9a5b 100644 --- a/src/main/Analyzor.java +++ b/src/main/Analyzor.java @@ -188,14 +188,14 @@ public class Analyzor { String text; String brand; String[] words; - HashMap> wordcloud = new HashMap<>(); + HashMap> wordcloud = new HashMap<>(); 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()); + if (!wordcloud.containsKey(brand)) { + wordcloud.put(brand, new HashMap()); } //get the text text = data.getString("text"); @@ -206,15 +206,14 @@ public class Analyzor { //for all words for (String word : words) { //if it is empty, a space or a stripe, skip it - if(word.equals("") || word.equals(" ") || word.equals("-")){ + if (word.equals("") || word.equals(" ") || word.equals("-")) { continue; } //if the word is already in the map, increment the amount - if(wordcloud.get(brand).containsKey(word)){ + if (wordcloud.get(brand).containsKey(word)) { wordcloud.get(brand).put(word, wordcloud.get(brand).get(word) + 1); - } - //if the word is not already in the map, make an entry with amount = 1 - else{ + } //if the word is not already in the map, make an entry with amount = 1 + else { wordcloud.get(brand).put(word, 1); } } @@ -275,31 +274,29 @@ public class Analyzor { //gets the amount of users that tweet about a brand in a timezone //makes a csv file timezone, brand, amount - public void timezone(String query) throws SQLException, FileNotFoundException, UnsupportedEncodingException{ + public void timezone(String query) throws SQLException, FileNotFoundException, UnsupportedEncodingException { query(query); //hashmap timezone, brand, amount HashMap> timeMap = new HashMap<>(); String timezone; String brand; - - while(data.next()){ + + while (data.next()) { timezone = data.getString("timezone"); brand = data.getString("brand"); //if the timezone is already in the map - if(timeMap.containsKey(timezone)){ + if (timeMap.containsKey(timezone)) { //if the brand for that timezone is already in the map - if(timeMap.get(timezone).containsKey(brand)){ + if (timeMap.get(timezone).containsKey(brand)) { //increment the amount timeMap.get(timezone).put(brand, timeMap.get(timezone).get(brand) + 1); - } - //if the brand for that timezone is not yet in the map - else{ + } //if the brand for that timezone is not yet in the map + else { //make a new entry for that brand with amount = 1 timeMap.get(timezone).put(brand, 1); } - } - //if the timezone is not yet in the map - else{ + } //if the timezone is not yet in the map + else { //make a new hashmap for this map and fill it with the brand and the amount timeMap.put(timezone, new HashMap()); timeMap.get(timezone).put(brand, 1); @@ -308,7 +305,7 @@ public class Analyzor { //make the CSV out of the map mapToCSV(timeMap, "timezone.csv", "timezone,brand,count"); } - + //replaces punctuation so it will be splitted //also removes urls private String splitPunctToWords(String text) { @@ -327,25 +324,25 @@ public class Analyzor { text = text.replaceAll("[^a-zA-Z0-9#_-]", " "); return text; } - + //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) - throws FileNotFoundException, UnsupportedEncodingException{ - + void mapToCSV(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()){ + for (Entry en : map.entrySet()) { //loop over words - for(Entry e : map.get(en.getKey()).entrySet()){ + 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"); } diff --git a/test/analysis/BrandCheckerTest.java b/test/analysis/BrandCheckerTest.java index 23d8445..f55035b 100644 --- a/test/analysis/BrandCheckerTest.java +++ b/test/analysis/BrandCheckerTest.java @@ -82,10 +82,10 @@ public class BrandCheckerTest { public void testBullshit() { doTest("This applepie is delicious", new String[]{}); } - + @Test public void multipleBrands() { - doTest("This tweet contains both iphone 4s,galaxy s5 and iphone", new String[]{"iphone 4s","galaxy s5"}); + doTest("This tweet contains both iphone 4s,galaxy s5 and iphone", new String[]{"iphone 4s", "galaxy s5"}); } } -- cgit v1.2.1