# 2015. May 4th # Author: Jin, Yilong jin28@vt.edu # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . class mention: # these 2 stores a bunch of maps #mentioned_user_dict = { # 'from_user_1' : { # {'to_user_1': 1}, # {'to_user_2': 1}, # {'to_user_3': 1}, # }, # 'from_user_0' : { # {'to_user_1': 1}, # {'to_user_2': 1}, # {'to_user_3': 1}, # } #} #same goes to mentioned_by_user_dict #key: user, say A : value: a dictionary of users that is mentioned by user # value dictionary: # key: user mentioned by A : value, # of times mentioned by A mention_user_dict = {} #key: user mentioned by others, say A : value: a dictionary of users that have ever # mentioned A in their tweets # value dictionary: # key: user, say B, mentions A : value, # of times B mentinos A mentioned_by_user_dict = {} counter = 0 @staticmethod def get_pair_count(): return mention.count @staticmethod def get_mention_dict(): return mention.mention_user_dict @staticmethod def add(u_from, u_to): mention.counter += 1 # u_from mentions u_to entry = mention.mention_user_dict.get(u_from) if entry is not None: mentioned_user = entry.get(u_to) if mentioned_user is not None: num = mention.mention_user_dict.get(u_from).get(u_to) mention.mention_user_dict[u_from][u_to] = num + 1 else: mention.mention_user_dict[u_from][u_to] = 1 else: mention.mention_user_dict[u_from] = dict() mention.mention_user_dict[u_from][u_to]= 1 entry2 = mention.mentioned_by_user_dict.get(u_to) if entry2 is not None: mention_user = entry2.get(u_from) if mention_user is not None: num2 = mention.mentioned_by_user_dict.get(u_to).get(u_from) mention.mentioned_by_user_dict[u_to][u_from] = num2 + 1 else: mention.mentioned_by_user_dict[u_to][u_from] = 1 else: mention.mentioned_by_user_dict[u_to] = dict() mention.mentioned_by_user_dict[u_to][u_from] = 1 return True @staticmethod def verify(): for user, mention_dict in mention.mention_user_dict.items(): for mentioned_user, times in mention_dict.items(): try: assert (times == mention.mentioned_by_user_dict.get(mentioned_user).get(user)) except: print'[verify error:] user: %s, mentioned_user: %s , times: %s' % (user, mentioned_user, times) raw_input('continue') return True @staticmethod def get_total_mentions_by_users(u): """this method returns the total # of times user(u) mentioned by other users""" """one may consider this as the # of inlink of u""" retval = 0 values = mention.mentioned_by_user_dict.get(u) if values is not None: for v in values.values(): retval += v return retval @staticmethod def get_total_mention(u): """this method returns the total # of times user(u) mention other users""" """one may consider this as the # of outlinks of u""" retval = 0 values = mention.mention_user_dict.get(u) if values is not None: for v in values.values(): retval += v return retval @staticmethod def get_mention_stat(u_from_l, u_to_l): u_from = str(u_from_l) u_to = str(u_to_l) retval = 0 entry = mention.mention_user_dict.get(u_from) if entry: mentioned_user = entry.get(u_to) if mentioned_user: retval = mention.mention_user_dict.get(u_from).get(u_to) return retval @staticmethod def get_mentioned_by_stat(u_from, u_to): retval = 0 entry = mention.mentioned_by_user_dict.get(u_to) if entry: mentioned_by_user = entry.get(u_from) if mentioned_by_user: retval = mention.mention_user_dict.get(u_from).get(u_to) return retval