Exemplo n.º 1
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
Exemplo n.º 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)'
    """
    "*** 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
Exemplo n.º 3
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
Exemplo n.º 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
Exemplo n.º 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)'
    """
    tweets_by_state = make_database() 
    "*** YOUR CODE HERE ***"
    states = []
    tweets_lst = []
    for tweet in tweets:
        tweet_state = nearest_state(tweet_location(tweet))
        if tweet_state in states:
            i = find_position(tweet_state, states)
            tweets_lst[i] += [tweet]
        else:
            states += [tweet_state]
            tweets_lst += [[tweet]]
    for i in range(len(states)):
        tweets_by_state = add_value(tweets_by_state, states[i], tweets_lst[i])
    return tweets_by_state
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
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:
        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
Exemplo n.º 10
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()
Exemplo n.º 11
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()
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 14
0
def load_states():
    """Load the coordinates of all the state outlines and return them
    in a database, from names to shapes lists.

    >>> len(get_value_from_key(load_states(), 'HI'))  # Hawaii has 5 islands
    5
    """
    json_data_file = open(DATA_PATH + 'states.json', encoding='utf8')
    states = JSONDecoder().decode(json_data_file.read())
    states_database = make_database()
    for state, shapes in states.items():
        states_database = add_value(states_database, state, shapes)
    for state, shapes in get_items(states_database):
        for index, shape in enumerate(shapes):
            if type(shape[0][0]) == list:  # the shape is a single polygon
                assert len(shape) == 1, 'Multi-polygon shape'
                shape = shape[0]
            shapes[index] = [make_position(*reversed(pos)) for pos in shape]
    return states_database
Exemplo n.º 15
0
def load_states():
    """Load the coordinates of all the state outlines and return them
    in a database, from names to shapes lists.

    >>> len(get_value_from_key(load_states(), 'HI'))  # Hawaii has 5 islands
    5
    """
    json_data_file = open(DATA_PATH + 'states.json', encoding='utf8')
    states = JSONDecoder().decode(json_data_file.read())
    states_database = make_database()
    for state, shapes in states.items():
        states_database = add_value(states_database, state, shapes)
    for state, shapes in get_items(states_database):
        for index, shape in enumerate(shapes):
            if type(shape[0][0]) == list:  # the shape is a single polygon
                assert len(shape) == 1, 'Multi-polygon shape'
                shape = shape[0]
            shapes[index] = [make_position(*reversed(pos)) for pos in shape]
    return states_database
Exemplo n.º 16
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
Exemplo n.º 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
Exemplo n.º 18
0
def make_states_database_with_empty_lists():
    database = make_database()
    us_state_key_list = get_keys(us_states)
    for i in range(get_len(us_states)):
        database = add_value(database, us_state_key_list[i], [])
    return database
Exemplo n.º 19
0
def make_states_database_with_empty_lists():
    database = make_database()
    us_state_key_list = get_keys(us_states)
    for i in range(get_len(us_states)):
        database = add_value(database, us_state_key_list[i], [])
    return database