From 7939c4d84a6969e03c686b379400e7bf56bc4401 Mon Sep 17 00:00:00 2001 From: Sofia Yfantidou Date: Wed, 11 May 2016 14:44:14 +0300 Subject: [PATCH] Incorporates the Location Detector class --- src/analytics/AnalyticsExtractor.java | 24 +++++++++++++++++++++--- src/combiner/Combiner.java | 6 +++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/analytics/AnalyticsExtractor.java b/src/analytics/AnalyticsExtractor.java index dccd049..fac5566 100644 --- a/src/analytics/AnalyticsExtractor.java +++ b/src/analytics/AnalyticsExtractor.java @@ -42,8 +42,25 @@ private void getTwitterMentionFrequencies() { * Gets the location frequencies in collected tweets and Youtube comments */ private void getLocationFrequencies() { - writeToTagcloudFile(calculateFrequenciesSimple("geo","twitter"),"location_frequencies_twitter.txt"); - writeToTagcloudFile(calculateFrequenciesSimple("location","youtube"),"location_frequencies_youtube.txt"); + writeToTagcloudFile(getCountriesFrequencies(calculateFrequenciesSimple("geo","twitter")),"location_frequencies_twitter.txt"); + writeToTagcloudFile(getCountriesFrequencies(calculateFrequenciesSimple("location","youtube")),"location_frequencies_youtube.txt"); + } + + private HashMap getCountriesFrequencies(HashMap locations) { + HashMap countriesFrequencies = new HashMap<>(); + for(String location : locations.keySet()) { + String city = ""; + if(location.contains(",")) { + city = location.substring(0, location.indexOf(",")).replaceAll(" ", "_"); + } + city = city.replaceAll(" ","_"); + String country = LocationDetector.getCountryOf(city); + if(!country.isEmpty()) { + countriesFrequencies.putIfAbsent(country, 0); + countriesFrequencies.computeIfPresent(country, (k, v) -> v + 1); + } + } + return countriesFrequencies; } /** @@ -67,6 +84,7 @@ private void getYoutubeUsersFrequencies() { writeToTagcloudFile(calculateFrequenciesSimple("authorID","youtube"),"user_frequencies_youtube.txt"); } + /** * Calculates frequencies of field by hashing, without preprocessing * @param field The field, which frequency is counted @@ -139,7 +157,7 @@ private void writeToTagcloudFile(HashMap map, String filename) { try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(path), "utf-8"))) { for(String s : map.keySet()) { - String key = s.replace(", ",";"); + String key = s.replace(", ",";"); // If there are commas in the initial key String writer.write(key + " , " + map.get(s)); writer.write(System.lineSeparator()); } diff --git a/src/combiner/Combiner.java b/src/combiner/Combiner.java index 6c0215a..2e7e264 100644 --- a/src/combiner/Combiner.java +++ b/src/combiner/Combiner.java @@ -29,8 +29,8 @@ public static void main(String[] args) throws JSONException, IOException { if(args != null) { // Collect Youtube comments and insert them to DB - YoutubeExporter.main(args); - Preprocessor.preprocessComments(args[4].split("=")[1], fc); + /*YoutubeExporter.main(args); + Preprocessor.preprocessComments(args[4].split("=")[1], fc);*/ // Collect tweets and insert them to DB @@ -42,7 +42,7 @@ public static void main(String[] args) throws JSONException, IOException { if(args.length >= 5) { analyticsExtractor = new AnalyticsExtractor(args[4].split("=")[1]); - analyticsExtractor.analyze(); + //analyticsExtractor.analyze(); } }