def compute( dsconf, community, start, end ): """Compute community reciprocities with 'seconds' resolution.""" c.log( 'Starting' ) reciprocities = collections.defaultdict(float) # Get all messages and replies for 'users' messages = ds.get_messages(dsconf, start, end, community) messages = common.build_msgmap(messages, ds.field('messages','user_id')) replies = common.build_replies(messages, ds.fields('messages')) # Get requested users reciprocities c.log( 'Computing reciprocities ...' ) for (user,user_messages) in messages.iteritems(): # Calculate the sum of reply delays delays_sum = dt.timedelta( 0 ) for reply in replies[user]: if reply[2].total_seconds() >= 0: # Ignore negative delay replies delays_sum += reply[2] # txy component # Calculate reciprocity if not replies[user]: reciprocities[user] = float('inf') else: reciprocities[user] = delays_sum.total_seconds() * 1.0 / \ len( replies[user] ) # Return users reciprocities c.log( 'Finished' ) return reciprocities
def get_communities(dsname): """Use datasource get_communities() facility.""" c.log( 'Starting' ) # Build the datasource configuration dsconfig = ds.build_conf( dsname, DATASOURCES_CONF ) # Get the cache directory config = ConfigParser.RawConfigParser() config.read(GENERAL_CONF) dsconfig['cache_dir'] = config.get('paths','cache_dir') communities = ds.get_communities( dsconfig ) fields = ds.fields('communities') for community in communities: id = community[fields['id']] title = (community[fields['title']], '(no title)')\ [community[fields['title']] is None] start_date = (community[fields['start_date']], '(no start date)')\ [community[fields['start_date']] is None] end_date = (community[fields['end_date']], '(no end date)')\ [community[fields['end_date']] is None] print '{0}\t{1}\t{2}\t{3}'.format( id, title.encode('UTF-8'), start_date, end_date ) c.log( 'Finished' )
def compute( dsconf, community, start, end ): """Compute community popularities.""" c.log( 'Starting' ) popularities = collections.defaultdict(float) # Get all messages and replies for the community messages = ds.get_messages(dsconf, start, end, community) messages = common.build_msgmap(messages, ds.field('messages','user_id')) replies = common.build_replies(messages, ds.fields('messages')) # Get requested users popularities c.log( 'Computing users popularities ...' ) for (user,user_messages) in messages.iteritems(): popularities[user] = len( replies[user] ) * 1.0 / \ len( user_messages ) # Return users popularities c.log( 'Finished' ) return popularities