Beispiel #1
0
def average_sentiments(tweets_by_state):
    """Calculate the average sentiment of the states by averaging over all
    the tweets from each state. Return the result as a database from state
    names to average sentiment values (numbers).

    If a state has no tweets with sentiment values, leave it out of the
    database entirely.  Do NOT include states with no tweets, or with tweets
    that have no sentiment, as 0. 0 represents neutral sentiment, not unknown
    sentiment.

    tweets_by_state -- A database from state names to lists of tweets
    """
    averaged_state_sentiments = make_database() 
    "*** YOUR CODE HERE ***"
    for i in range(get_len(tweets_by_state)):
        list_of_tweets_by_state = get_value_from_key(tweets_by_state, get_keys(tweets_by_state)[i])
        list_of_all_sentiments = []
        for j in range(len(list_of_tweets_by_state)):
            if has_sentiment(analyze_tweet_sentiment(list_of_tweets_by_state[j])):
                list_of_all_sentiments.append(sentiment_value(analyze_tweet_sentiment(list_of_tweets_by_state[j])))
        if len(list_of_all_sentiments) > 0:
            average_sentiment_for_state = sum(list_of_all_sentiments)/len(get_value_from_key(tweets_by_state, get_keys(tweets_by_state)[i]))
            averaged_state_sentiments = add_value(averaged_state_sentiments, get_keys(tweets_by_state)[i], average_sentiment_for_state)


    return averaged_state_sentiments
Beispiel #2
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_database() 
    "*** YOUR CODE HERE ***"
    for tweet in tweets:
        distance =  [(geo_distance(tweet_location(tweet),find_state_center(get_value_from_key(us_states,state))),state) for state in get_keys(us_states)]
        distance =sorted(distance,key=lambda x:x[0])
        if distance[0][1] not in get_keys(tweets_by_state):
            tweets_by_state = add_value(tweets_by_state, distance[0][1], [tweet,])
        else:
            new_tweet = get_value_from_key(tweets_by_state, distance[0][1]) + [tweet,]
            tweets_by_state = add_value(tweets_by_state, distance[0][1], new_tweet)

    return tweets_by_state
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_database()
    "*** YOUR CODE HERE ***"
    for tweet in tweets:
        minimum = 9999999999
        for state in get_keys(us_states):
            state_coor = find_state_center(get_value_from_key(
                us_states, state))
            distance = geo_distance(tweet_location(tweet), state_coor)
            if distance < minimum:
                minimum, state_name = distance, state
        if state_name not in get_keys(tweets_by_state):
            tweets_by_state = add_value(tweets_by_state, state_name, [tweet])
        else:
            get_value_from_key(tweets_by_state, state_name).append(tweet)
    return tweets_by_state
Beispiel #4
0
def average_sentiments(tweets_by_state):
    """Calculate the average sentiment of the states by averaging over all
    the tweets from each state. Return the result as a database from state
    names to average sentiment values (numbers).

    If a state has no tweets with sentiment values, leave it out of the
    database entirely.  Do NOT include states with no tweets, or with tweets
    that have no sentiment, as 0. 0 represents neutral sentiment, not unknown
    sentiment.

    tweets_by_state -- A database from state names to lists of tweets
    """
    averaged_state_sentiments = make_database()
    "*** YOUR CODE HERE ***"
    for i in range(get_len(tweets_by_state)):
        list_of_tweets_by_state = get_value_from_key(
            tweets_by_state,
            get_keys(tweets_by_state)[i])
        list_of_all_sentiments = []
        for j in range(len(list_of_tweets_by_state)):
            if has_sentiment(
                    analyze_tweet_sentiment(list_of_tweets_by_state[j])):
                list_of_all_sentiments.append(
                    sentiment_value(
                        analyze_tweet_sentiment(list_of_tweets_by_state[j])))
        if len(list_of_all_sentiments) > 0:
            average_sentiment_for_state = sum(list_of_all_sentiments) / len(
                get_value_from_key(tweets_by_state,
                                   get_keys(tweets_by_state)[i]))
            averaged_state_sentiments = add_value(averaged_state_sentiments,
                                                  get_keys(tweets_by_state)[i],
                                                  average_sentiment_for_state)

    return averaged_state_sentiments
Beispiel #5
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    "*** YOUR CODE HERE ***"
    tweets_by_state = make_database()
    for tweet in tweets:
        checker = 1111111111
        tweet_loc = tweet_location(tweet)
        for state in get_keys(us_states):
            state_center_loc = find_state_center(get_value_from_key(us_states, state))
            geo_dist = geo_distance(tweet_loc, state_center_loc)
            if geo_dist < checker:
                checker = geo_dist
                closest_state = state
        if closest_state in get_keys(tweets_by_state):
            tweets_by_state = add_value(tweets_by_state, closest_state, [tweet] + get_value_from_key(tweets_by_state, closest_state))
        else: 
            tweets_by_state = add_value(tweets_by_state, closest_state, [tweet])     
    return tweets_by_state
Beispiel #6
0
def nearest_state(position):
    states = get_keys(us_states)
    dist_min = geo_distance(position, find_state_center(get_value_from_key(us_states, states[0])))
    state_min = states[0]
    for state in states:
        dist = geo_distance(position, find_state_center(get_value_from_key(us_states, state)))
        if dist < dist_min:
            dist_min = dist
            state_min = state
    return state_min
Beispiel #7
0
def draw_centered_map(center_state='TX', n=10):
    """Draw the n states closest to center_state."""
    us_centers = make_database()
    for state, s in get_items(us_states):
        us_centers = add_value(us_centers, state, find_state_center(s))
    center = get_value_from_key(us_centers, center_state.upper()) 
    dist_from_center = lambda name: geo_distance(center, get_value_from_key(us_centers, name))
    for name in sorted(get_keys(us_centers), key=dist_from_center)[:int(n)]:
        draw_state(get_value_from_key(us_states, name))
        draw_name(name, get_value_from_key(us_centers, name))
    draw_dot(center, 1, 10)  # Mark the center state with a red dot
    wait()
Beispiel #8
0
def draw_centered_map(center_state='TX', n=10):
    """Draw the n states closest to center_state."""
    us_centers = make_database()
    for state, s in get_items(us_states):
        us_centers = add_value(us_centers, state, find_state_center(s))
    center = get_value_from_key(us_centers, center_state.upper())
    dist_from_center = lambda name: geo_distance(
        center, get_value_from_key(us_centers, name))
    for name in sorted(get_keys(us_centers), key=dist_from_center)[:int(n)]:
        draw_state(get_value_from_key(us_states, name))
        draw_name(name, get_value_from_key(us_centers, name))
    draw_dot(center, 1, 10)  # Mark the center state with a red dot
    wait()
Beispiel #9
0
def closest_state_to_tweet(tweet):
    #takes in a tweet and returns the two-letter state postal code for the
    #closest state center to that tweet
    database = us_states
    new_dynamic_database = make_database()
    list_of_keys = get_keys(database)
    for i in range(get_len(database)):
        dist = geo_distance(tweet_location(tweet), find_state_center(get_value_from_key(database, list_of_keys[i])))
        new_dynamic_database = add_value(new_dynamic_database, list_of_keys[i], dist)
    distances_from_states = get_values(new_dynamic_database)
    closest_state = ""
    for i in range(get_len(new_dynamic_database)):
        if get_value_from_key(new_dynamic_database, get_keys(new_dynamic_database)[i]) == min(distances_from_states):
            closest_state = get_keys(new_dynamic_database)[i]
    return closest_state
Beispiel #10
0
def average_sentiments(tweets_by_state):
    """Calculate the average sentiment of the states by averaging over all
    the tweets from each state. Return the result as a database from state
    names to average sentiment values (numbers).

    If a state has no tweets with sentiment values, leave it out of the
    database entirely.  Do NOT include states with no tweets, or with tweets
    that have no sentiment, as 0. 0 represents neutral sentiment, not unknown
    sentiment.

    tweets_by_state -- A database from state names to lists of tweets
    """
    "*** YOUR CODE HERE ***" 
    averaged_state_sentiments = make_database()
    for states in get_keys(tweets_by_state):
        state_tweet = get_value_from_key(tweets_by_state, states)
        if state_tweet != 0:
            counter = 0
            average_counter = 0 
            for tweet in state_tweet:
                if has_sentiment(analyze_tweet_sentiment(tweet)):
                    average_counter += sentiment_value(analyze_tweet_sentiment(tweet))
                    counter += 1
        if counter != 0:
            average_counter = average_counter / counter
            averaged_state_sentiments = add_value(averaged_state_sentiments, states, average_counter)    
    return averaged_state_sentiments
Beispiel #11
0
def average_sentiments(tweets_by_state):
    """Calculate the average sentiment of the states by averaging over all
    the tweets from each state. Return the result as a database from state
    names to average sentiment values (numbers).

    If a state has no tweets with sentiment values, leave it out of the
    database entirely.  Do NOT include states with no tweets, or with tweets
    that have no sentiment, as 0. 0 represents neutral sentiment, not unknown
    sentiment.

    tweets_by_state -- A database from state names to lists of tweets
    """
    averaged_state_sentiments = make_database() 
    "*** YOUR CODE HERE ***"
    states = get_keys(tweets_by_state)
    for state in states:
        total_sentiment, i = 0, 0
        list_tweets = get_value_from_key(tweets_by_state, state)
        for tweet in list_tweets:
            if has_sentiment(analyze_tweet_sentiment(tweet)):
                total_sentiment += sentiment_value(analyze_tweet_sentiment(tweet))
                i += 1
        if i != 0:
            aver = total_sentiment/i
            averaged_state_sentiments = add_value(averaged_state_sentiments, state, aver)
    return averaged_state_sentiments
Beispiel #12
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_states_database_with_empty_lists()
    "*** YOUR CODE HERE ***"
    for i in range(len(tweets)):
        state_of_tweet = closest_state_to_tweet(tweets[i])
        state_list = get_value_from_key(tweets_by_state, state_of_tweet)
        state_list.append(tweets[i])
        tweets_by_state = add_value(tweets_by_state, state_of_tweet, state_list)
    final_tweets_by_state_list = make_database()
    for i in range(get_len(tweets_by_state)):
        if get_values(tweets_by_state)[i] != []:
            final_tweets_by_state_list = add_value(final_tweets_by_state_list, get_keys(tweets_by_state)[i], get_values(tweets_by_state)[i])

    return final_tweets_by_state_list
Beispiel #13
0
def closest_state_to_tweet(tweet):
    #takes in a tweet and returns the two-letter state postal code for the
    #closest state center to that tweet
    database = us_states
    new_dynamic_database = make_database()
    list_of_keys = get_keys(database)
    for i in range(get_len(database)):
        dist = geo_distance(
            tweet_location(tweet),
            find_state_center(get_value_from_key(database, list_of_keys[i])))
        new_dynamic_database = add_value(new_dynamic_database, list_of_keys[i],
                                         dist)
    distances_from_states = get_values(new_dynamic_database)
    closest_state = ""
    for i in range(get_len(new_dynamic_database)):
        if get_value_from_key(new_dynamic_database,
                              get_keys(new_dynamic_database)[i]) == min(
                                  distances_from_states):
            closest_state = get_keys(new_dynamic_database)[i]
    return closest_state
Beispiel #14
0
def draw_state_sentiments(state_sentiments):
    """Draw all U.S. states in colors corresponding to their sentiment value.

    Unknown state names are ignored; states without values are colored grey.

    state_sentiments -- A database from state strings to sentiment values
    """
    for name, shapes in get_items(us_states):
        if name in get_keys(state_sentiments):
            sentiment = get_value_from_key(state_sentiments, name)
        else:
            sentiment = None
        draw_state(shapes, sentiment)
    for name, shapes in get_items(us_states):
        center = find_state_center(shapes)
        if center is not None:
            draw_name(name, center)
Beispiel #15
0
def draw_state_sentiments(state_sentiments):
    """Draw all U.S. states in colors corresponding to their sentiment value.

    Unknown state names are ignored; states without values are colored grey.

    state_sentiments -- A database from state strings to sentiment values
    """
    for name, shapes in get_items(us_states):
        if name in get_keys(state_sentiments):
            sentiment = get_value_from_key(state_sentiments, name)
        else:
            sentiment = None
        draw_state(shapes, sentiment)
    for name, shapes in get_items(us_states):
        center = find_state_center(shapes)
        if center is not None:
            draw_name(name, center)
Beispiel #16
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_states_database_with_empty_lists()
    "*** YOUR CODE HERE ***"
    for i in range(len(tweets)):
        state_of_tweet = closest_state_to_tweet(tweets[i])
        state_list = get_value_from_key(tweets_by_state, state_of_tweet)
        state_list.append(tweets[i])
        tweets_by_state = add_value(tweets_by_state, state_of_tweet,
                                    state_list)
    final_tweets_by_state_list = make_database()
    for i in range(get_len(tweets_by_state)):
        if get_values(tweets_by_state)[i] != []:
            final_tweets_by_state_list = add_value(
                final_tweets_by_state_list,
                get_keys(tweets_by_state)[i],
                get_values(tweets_by_state)[i])

    return final_tweets_by_state_list
Beispiel #17
0
def average_sentiments(tweets_by_state):
    """Calculate the average sentiment of the states by averaging over all
    the tweets from each state. Return the result as a database from state
    names to average sentiment values (numbers).

    If a state has no tweets with sentiment values, leave it out of the
    database entirely.  Do NOT include states with no tweets, or with tweets
    that have no sentiment, as 0. 0 represents neutral sentiment, not unknown
    sentiment.

    tweets_by_state -- A database from state names to lists of tweets
    """
    averaged_state_sentiments = make_database() 
    "*** YOUR CODE HERE ***"
    for state in get_keys(tweets_by_state):
        sentiments_list = [ sentiment_value(analyze_tweet_sentiment(tweet)) for tweet in get_value_from_key(tweets_by_state,state) if has_sentiment(analyze_tweet_sentiment(tweet))]
        if sentiments_list:
                averaged = sum(sentiments_list)/len(sentiments_list)
                averaged_state_sentiments = add_value(averaged_state_sentiments, state, averaged)

    return averaged_state_sentiments