public static void printResultToFile(String folder, List<AlgorithmResult> results) throws IOException { File file = new File(folder + "/result-" + System.currentTimeMillis()); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); for (AlgorithmResult result : results) { bw.write(result.toString()); bw.newLine(); } bw.close(); }
public static void calcStats(String directoryPath, AlgorithmResult result) throws IOException { Set<Long> userIds = new HashSet<>(); Set<Long> tweetIds = new HashSet<>(); String line = null; try (BufferedReader br = new BufferedReader(new FileReader(directoryPath + "/stats"))) { while ((line = br.readLine()) != null) { JsonObject tweet = new JsonParser().parse(line).getAsJsonObject(); JsonArray hashtagsElem = tweet.getAsJsonArray("hashtags"); Set<String> uniqueHashtags = new HashSet<>(); for (JsonElement hashtagElem : hashtagsElem) { String hashtag = hashtagElem.getAsString(); uniqueHashtags.add(hashtag.toLowerCase()); } boolean contributed = false; for (String resultHashtag : result.getHashtags()) { if (uniqueHashtags.contains(resultHashtag)) { contributed = true; break; } } if (contributed) { long userId = tweet.get("userId").getAsLong(); userIds.add(userId); long tweetId = tweet.get("tweetId").getAsLong(); tweetIds.add(tweetId); } } } catch (Exception ex) { // System.err.println(line); } result.setUniqueUsers(userIds.size()); result.setUniqueTweets(tweetIds.size()); }