|
| 1 | +package processing; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author spujari |
| 15 | + * |
| 16 | + */ |
| 17 | +public class ProcessFrequencyRecency { |
| 18 | + |
| 19 | + private String sampleDir; |
| 20 | + |
| 21 | + public void ProcessTagAnalytics(String sampleDir, HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap) { |
| 22 | + this.sampleDir = sampleDir; |
| 23 | + |
| 24 | + // frequency |
| 25 | + //HashMap<Integer, Integer> tagFrequency = getTagFrequency(userTagTimestampMap); |
| 26 | + //saveHashMap(tagFrequency, "./tagfrequency"); |
| 27 | + // recency in duration |
| 28 | + //HashMap< Integer, Integer> tagRecency = getRecencyInDuration(userTagTimestampMap); |
| 29 | + |
| 30 | + //saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.SECOND), "./tagrecency" + "_" + "Seconds"); |
| 31 | + //saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.MINUTE), "./tagrecency" + "_" + "MINUTE"); |
| 32 | + saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.HOUR), "recencyHours" ); |
| 33 | + //saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.DAY), "./tagrecency" + "_" + "DAY" ); |
| 34 | + //saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.FIFTEEN_DAYS), "./tagrecency" + "_" + "FIFTEEN_DAYS"); |
| 35 | + //saveHashMap(getRecencyInDuration(userTagTimestampMap, TimeUtil.MONTH), "./tagrecency" + "_" + "MONTH"); |
| 36 | + // user unique tag count |
| 37 | + //HashMap< Integer, Integer> uniqueTagCount = getUserUniqueTagCount(userTagTimestampMap); |
| 38 | + //saveHashMap(uniqueTagCount, "./uniqueTagCount"); |
| 39 | + // user tag count |
| 40 | + //HashMap< Integer, Integer> userTagCount = getUserTagCount(userTagTimestampMap); |
| 41 | + // tag tag count |
| 42 | + // HashMap< Integer, Integer> tagTagCount = getTagTagCount(userTagTimestampMap); |
| 43 | + // tag user count |
| 44 | + //HashMap< Integer, Integer> tagUserCount = getTagUserCount(userTagTimestampMap); |
| 45 | + } |
| 46 | + |
| 47 | + private void saveHashMap(HashMap<Integer, Integer> saveHashMap, String filename){ |
| 48 | + File file = new File("./data/metrics/" + this.sampleDir + "/" + filename); |
| 49 | + try { |
| 50 | + if (!file.exists()){ |
| 51 | + file.createNewFile(); |
| 52 | + } |
| 53 | + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); |
| 54 | + for (Integer key : saveHashMap.keySet()){ |
| 55 | + for(int i=0; i < saveHashMap.get(key) ; i++){ |
| 56 | + bw.write(key + "\n"); |
| 57 | + } |
| 58 | + } |
| 59 | + bw.close(); |
| 60 | + } catch (IOException e) { |
| 61 | + // TODO Auto-generated catch block |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * get how frequent is length of timestamp list. |
| 68 | + * @param userTagTimestampMap user map to tag to list of timestamps of tag usage. |
| 69 | + * @return a map of timestamp list lenght to number of times its occuring in the dataset. |
| 70 | + */ |
| 71 | + private HashMap<Integer, Integer> getTagFrequency(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap){ |
| 72 | + |
| 73 | + HashMap<Integer, Integer> hashMapInteger = new HashMap<Integer, Integer>(); |
| 74 | + for (String user : userTagTimestampMap.keySet()){ |
| 75 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 76 | + List<Long> timestampList = userTagTimestampMap.get(user).get(tag); |
| 77 | + Integer timestampListLen = timestampList.size(); |
| 78 | + if (!hashMapInteger.containsKey(timestampListLen)) { |
| 79 | + hashMapInteger.put(timestampListLen, 0); |
| 80 | + }else{ |
| 81 | + |
| 82 | + } |
| 83 | + hashMapInteger.put(timestampListLen, hashMapInteger.get(timestampListLen) + 1); |
| 84 | + } |
| 85 | + } |
| 86 | + return hashMapInteger; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * how recent the tags are used in the dataset. |
| 91 | + * @param userTagTimestampMap user map to tag to list of timestamps of tag usage. |
| 92 | + * @return the duration count between the 2 consecutive use of hastags. |
| 93 | + */ |
| 94 | + private HashMap<Integer, Integer> getRecencyInDuration(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap, |
| 95 | + int durationType){ |
| 96 | + |
| 97 | + HashMap<Integer, Integer> durationCountMap = new HashMap<Integer, Integer>(); |
| 98 | + for (String user : userTagTimestampMap.keySet()){ |
| 99 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 100 | + Long lastTimestamp = new Long(0); |
| 101 | + List<Long> timestampList = userTagTimestampMap.get(user).get(tag); |
| 102 | + Collections.sort(timestampList); |
| 103 | + |
| 104 | + for (Long currentTimestamp : timestampList){ |
| 105 | + if (lastTimestamp == 0){ |
| 106 | + lastTimestamp = currentTimestamp; |
| 107 | + continue; |
| 108 | + }else{ |
| 109 | + int duration = (int)(currentTimestamp - lastTimestamp); |
| 110 | + duration = TimeUtil.getDurationAtGranularity(duration, durationType); |
| 111 | + duration++; |
| 112 | + if (!durationCountMap.containsKey(duration)){ |
| 113 | + durationCountMap.put(duration, 0); |
| 114 | + }else{ |
| 115 | + |
| 116 | + } |
| 117 | + durationCountMap.put(duration, durationCountMap.get(duration) + 1); |
| 118 | + lastTimestamp = currentTimestamp; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return durationCountMap; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * How many unique tags does a user use. |
| 128 | + * @param userTagTimestampMap |
| 129 | + * @return |
| 130 | + */ |
| 131 | + private HashMap<Integer, Integer> getUserUniqueTagCount(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap){ |
| 132 | + |
| 133 | + HashMap<Integer, Integer> userUniqueTagCount = new HashMap<Integer, Integer>(); |
| 134 | + for (String user : userTagTimestampMap.keySet()){ |
| 135 | + int numberUniqueTags = userTagTimestampMap.get(user).size(); |
| 136 | + if (!userUniqueTagCount.containsKey(numberUniqueTags)){ |
| 137 | + userUniqueTagCount.put(numberUniqueTags, 0); |
| 138 | + }else{ |
| 139 | + |
| 140 | + } |
| 141 | + userUniqueTagCount.put(numberUniqueTags, userUniqueTagCount.get(numberUniqueTags) + 1); |
| 142 | + } |
| 143 | + return userUniqueTagCount; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * The total number of tags used by the user |
| 148 | + * @param userTagTimestampMap |
| 149 | + * @return |
| 150 | + */ |
| 151 | + private HashMap<Integer, Integer> getUserTagCount(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap){ |
| 152 | + |
| 153 | + HashMap<Integer, Integer> userTotalTagCountMap = new HashMap<Integer, Integer>(); |
| 154 | + for (String user : userTagTimestampMap.keySet()){ |
| 155 | + int userTotalTagCount = 0; |
| 156 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 157 | + List<Long> timestampList = userTagTimestampMap.get(user).get(tag); |
| 158 | + int lenTimestampList = timestampList.size(); |
| 159 | + userTotalTagCount = userTotalTagCount + lenTimestampList; |
| 160 | + } |
| 161 | + if (!userTotalTagCountMap.containsKey(userTotalTagCount)){ |
| 162 | + userTotalTagCountMap.put(userTotalTagCount, 0); |
| 163 | + } |
| 164 | + userTotalTagCountMap.put(userTotalTagCount, userTotalTagCountMap.get(userTotalTagCount) + 1); |
| 165 | + } |
| 166 | + return userTotalTagCountMap; |
| 167 | + } |
| 168 | + |
| 169 | + private HashMap<Integer, Integer> getTagTagCount(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap){ |
| 170 | + |
| 171 | + /* |
| 172 | + * Count of tags in the system. Some tags are frequent the others. |
| 173 | + * */ |
| 174 | + HashMap<Integer, Integer> tagTagCount = new HashMap<Integer, Integer>(); |
| 175 | + for (String user : userTagTimestampMap.keySet()){ |
| 176 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 177 | + List<Long> timestampList = userTagTimestampMap.get(user).get(tag); |
| 178 | + if (!tagTagCount.containsKey(tag)){ |
| 179 | + tagTagCount.put(tag, 0); |
| 180 | + }else{ |
| 181 | + |
| 182 | + } |
| 183 | + tagTagCount.put(tag, tagTagCount.get(tag) + timestampList.size()); |
| 184 | + } |
| 185 | + } |
| 186 | + // get the count of tags that are used particular number of time. |
| 187 | + HashMap<Integer, Integer> tagCountCountMap = new HashMap<Integer, Integer>(); |
| 188 | + for (Integer tag : tagTagCount.keySet()){ |
| 189 | + Integer count = tagTagCount.get(tag); |
| 190 | + if (!tagCountCountMap.containsKey(count)){ |
| 191 | + tagCountCountMap.put(count, 0); |
| 192 | + }else{ |
| 193 | + |
| 194 | + } |
| 195 | + tagCountCountMap.put(count,tagCountCountMap.get(count) + 1); |
| 196 | + } |
| 197 | + return tagCountCountMap; |
| 198 | + } |
| 199 | + |
| 200 | + private HashMap<Integer, Integer> getTagUserCount(HashMap<String, HashMap<Integer, ArrayList<Long>>> userTagTimestampMap){ |
| 201 | + |
| 202 | + /** |
| 203 | + * Some tags have more users then the others. |
| 204 | + * */ |
| 205 | + HashMap<Integer, Integer> tagUserCount = new HashMap<Integer, Integer>(); |
| 206 | + for (String user : userTagTimestampMap.keySet()){ |
| 207 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 208 | + if(!tagUserCount.containsKey(tag)){ |
| 209 | + tagUserCount.put(tag, 0); |
| 210 | + }else{ |
| 211 | + |
| 212 | + } |
| 213 | + tagUserCount.put(tag, tagUserCount.get(tag)); |
| 214 | + } |
| 215 | + } |
| 216 | + HashMap<Integer, Integer> tagUserCountCount = new HashMap<Integer, Integer>(); |
| 217 | + for (Integer tag : tagUserCount.keySet()){ |
| 218 | + Integer count = tagUserCount.get(tag); |
| 219 | + if (!tagUserCountCount.containsKey(count)){ |
| 220 | + tagUserCountCount.put(count, 0); |
| 221 | + }else{ |
| 222 | + |
| 223 | + } |
| 224 | + tagUserCountCount.put(count,tagUserCountCount.get(count) + 1); |
| 225 | + } |
| 226 | + return tagUserCountCount; |
| 227 | + } |
| 228 | + |
| 229 | + private void getSocialExternalHashTagCount(HashMap<Integer, HashMap<Integer, List<Long>>> userTagTimestampMap, |
| 230 | + HashMap<Integer, HashSet<Integer>> network){ |
| 231 | + /** |
| 232 | + * get the first use of hashtag was external or social. |
| 233 | + * */ |
| 234 | + for (Integer user : userTagTimestampMap.keySet()){ |
| 235 | + for (Integer tag : userTagTimestampMap.get(user).keySet()){ |
| 236 | + List<Long> timestampList = userTagTimestampMap.get(user).get(tag); |
| 237 | + } |
| 238 | + } |
| 239 | + return; |
| 240 | + } |
| 241 | + } |
0 commit comments