back-end/social_network_analyzer/markets_7days_categorical_analysis.py
def findMostFrequentMentions():

    print("Finding tweets with included mentions from the Database, over the last 7 days.")
    print('Querying database and retrieving the data.')
    # computing the datetime now - 7 days ago
    sevenDaysAgo = datetime.datetime.utcnow() - datetime.timedelta(days=7)

    # Mongo Shell query
    # db.twitterQuery2.find({'entities.user_mentions.screen_name': {$exists : true}}, {'entities.user_mentions.screen_name': 1, '_id': 0}).limit(5)

    # creating query + projection for MongoDB
    query = {'$and': [{'entities.user_mentions.screen_name': {'$exists': 'true'}}, {'mongoDate': {'$gte': sevenDaysAgo}}]}
    projection = {'entities.user_mentions.screen_name': 1, '_id': 0}

    # running query
    cursor = []
    try:
        cursor = twitterOutput1.find(query, projection)
        # cursor = cursor.limit(20)

    except Exception as e:
        print("Unexpected error:", type(e), e)

    print("Finding used mentions frequency..")
    # Listing countered mentions coming from tweets for storing later the corresponding query in a CSV file
    countAllMentions = Counter()
    mentionsList = []
    for doc in cursor:
        screenNameKey = doc['entities']['user_mentions']
        for item in screenNameKey:
            # print(item['screen_name'])
            mentionsList.append(item['screen_name'])
            # countAllMentions.update(item['screen_name'])

    # print(mentionsList)
    countAllMentions.update(mentionsList)

    print('Most 10 frequently used mentions:')
    print(countAllMentions.most_common(10))

    """
        CATEGORICAL ANALYSIS (BAR-PLOT) PANDAS SECTION
    """
    print('Starting data analysis with Pandas.')
    print('Creating data frame:')
    mentions_freq = countAllMentions.most_common(10)
    mentions = pd.DataFrame(mentions_freq)
    mentions.set_index(0, inplace=True)
    print('Data frame:')
    print(mentions.head())
    print('Writing CSV file..')
    mentions.to_csv('/var/www/html/saint/twitterSNA-Aug17/mentionsMarkets.csv')
    print('Writing Mentions Categorical Analysis to CSV file completed!')

        CATEGORICAL ANALYSIS (BAR-PLOT) PANDAS SECTION
    
CATEGORICAL ANALYSIS (BAR-PLOT) PANDAS SECTION
CATEGORICAL ANALYSIS ( BAR - PLOT ) PANDAS SECTION
def findMostFrequentMentions():
    print("Finding tweets with included mentions from the Database, over the last 7 days.")
    print('Querying database and retrieving the data.')
    sevenDaysAgo = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    query = {'$and': [{'entities.user_mentions.screen_name': {'$exists': 'true'}}, {'mongoDate': {'$gte': sevenDaysAgo}}]}
    projection = {'entities.user_mentions.screen_name': 1, '_id': 0}
    cursor = []
    try:
        cursor = twitterOutput1.find(query, projection)
    except Exception as e:
        print("Unexpected error:", type(e), e)
    print("Finding used mentions frequency..")
    countAllMentions = Counter()
    mentionsList = []
    for doc in cursor:
        screenNameKey = doc['entities']['user_mentions']
        for item in screenNameKey:
            mentionsList.append(item['screen_name'])
    countAllMentions.update(mentionsList)
    print('Most 10 frequently used mentions:')
    print(countAllMentions.most_common(10))
    print('Starting data analysis with Pandas.')
    print('Creating data frame:')
    mentions_freq = countAllMentions.most_common(10)
    mentions = pd.DataFrame(mentions_freq)
    mentions.set_index(0, inplace=True)
    print('Data frame:')
    print(mentions.head())
    print('Writing CSV file..')
    mentions.to_csv('/var/www/html/saint/twitterSNA-Aug17/mentionsMarkets.csv')
    print('Writing Mentions Categorical Analysis to CSV file completed!')
def findMostFrequentMentions ( ) : print ( "Finding tweets with included mentions from the Database, over the last 7 days." ) print ( 'Querying database and retrieving the data.' ) sevenDaysAgo = datetime . datetime . utcnow ( ) - datetime . timedelta ( days = 7 ) query = { '$and' : [ { 'entities.user_mentions.screen_name' : { '$exists' : 'true' } } , { 'mongoDate' : { '$gte' : sevenDaysAgo } } ] } projection = { 'entities.user_mentions.screen_name' : 1 , '_id' : 0 } cursor = [ ] try : cursor = twitterOutput1 . find ( query , projection ) except Exception as e : print ( "Unexpected error:" , type ( e ) , e ) print ( "Finding used mentions frequency.." ) countAllMentions = Counter ( ) mentionsList = [ ] for doc in cursor : screenNameKey = doc [ 'entities' ] [ 'user_mentions' ] for item in screenNameKey : mentionsList . append ( item [ 'screen_name' ] ) countAllMentions . update ( mentionsList ) print ( 'Most 10 frequently used mentions:' ) print ( countAllMentions . most_common ( 10 ) ) print ( 'Starting data analysis with Pandas.' ) print ( 'Creating data frame:' ) mentions_freq = countAllMentions . most_common ( 10 ) mentions = pd . DataFrame ( mentions_freq ) mentions . set_index ( 0 , inplace = True ) print ( 'Data frame:' ) print ( mentions . head ( ) ) print ( 'Writing CSV file..' ) mentions . to_csv ( '/var/www/html/saint/twitterSNA-Aug17/mentionsMarkets.csv' ) print ( 'Writing Mentions Categorical Analysis to CSV file completed!' )
CATEGORICAL ANALYSIS (BAR-PLOT) PANDAS SECTION
CATEGORICAL ANALYSIS ( BAR - PLOT ) PANDAS SECTION
# computing the datetime now - 7 days ago # Mongo Shell query # db.twitterQuery2.find({'entities.user_mentions.screen_name': {$exists : true}}, {'entities.user_mentions.screen_name': 1, '_id': 0}).limit(5) # creating query + projection for MongoDB # running query # cursor = cursor.limit(20) # Listing countered mentions coming from tweets for storing later the corresponding query in a CSV file # print(item['screen_name']) # countAllMentions.update(item['screen_name']) # print(mentionsList) """
        CATEGORICAL ANALYSIS (BAR-PLOT) PANDAS SECTION
    """