コード例 #1
0
ファイル: trends.py プロジェクト: lorettachanlt/trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    location = tweet_location(tweet)
    lst = []
    for item in state_centers:
        lst.append(geo_distance(state_centers[item], location))
    x = min(lst)
    for item in state_centers:
        if geo_distance(state_centers[item], location) == x:
            return item
コード例 #2
0
ファイル: trends.py プロジェクト: vgoklani/Twitter-Trends-1
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a immutable dictionary from state names to positions

    >>> us_centers = make_idict()
    >>> for n, s in idict_items(us_states):
    ...     us_centers = idict_insert(us_centers, n, find_center(s)) 
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    
    distance_tup = ()
    tweet_position = tweet_location(tweet)
    for n, s in idict_items(state_centers):
        distance_tup += geo_distance(tweet_position, idict_select(state_centers, n)),
   
    want = min(distance_tup)
    
        

    for n, s in idict_items(state_centers):
        if geo_distance(tweet_position, idict_select(state_centers, n)) == want:
            return n
コード例 #3
0
ファイル: trends.py プロジェクト: podfog/SchoolWork
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    for tweet in tweets:
        minimum_distance = 5000
        this_key = 0
        for key in us_states.keys():
            if geo_distance(tweet_location(tweet), \
                find_state_center(us_states[key])) < minimum_distance:
                minimum_distance = geo_distance(tweet_location(tweet), \
                                    find_state_center(us_states[key]))
                this_key = key
        if this_key in tweets_by_state:
            tweets_by_state[this_key].append(tweet)
        else:
            tweets_by_state[this_key] = [tweet]
    return tweets_by_state
コード例 #4
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    loc = tweet_location(tweet)
    closest_state = 'CA'  #initialize to CA
    min_dist = geo_distance(state_centers['CA'], loc)
    for state in state_centers:
        dist = geo_distance(state_centers[state], loc)
        if dist < min_dist:
            closest_state = state
            min_dist = dist
    return closest_state
コード例 #5
0
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    for tweet in tweets:
        minimum_distance = 5000
        this_key = 0
        for key in us_states.keys():
            if geo_distance(tweet_location(tweet), \
                find_state_center(us_states[key])) < minimum_distance:
                minimum_distance = geo_distance(tweet_location(tweet), \
                                    find_state_center(us_states[key]))
                this_key = key
        if this_key in tweets_by_state:
            tweets_by_state[this_key].append(tweet)
        else:
            tweets_by_state[this_key] = [tweet]
    return tweets_by_state
コード例 #6
0
ファイル: trends.py プロジェクト: jesseyli/OldProjects
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    closest_state = 'CA'
    closest_distance = geo_distance(tweet_location(tweet), state_centers[closest_state])
    for state in state_centers:
        if geo_distance(tweet_location(tweet), state_centers[state]) < closest_distance:
            closest_state = state
            closest_distance = geo_distance(tweet_location(tweet), state_centers[state])
    return closest_state
コード例 #7
0
ファイル: trends.py プロジェクト: joshperline/Twitter-Trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    diclist = list(state_centers)
    i, smallest = 1, 1e12
    while i<len(state_centers):
        first = geo_distance(tweet_location(tweet), state_centers[diclist[i-1]])
        sec = geo_distance(tweet_location(tweet), state_centers[diclist[i]])
        if first < sec:
            if first<smallest:
                smallest = first
                state = diclist[i - 1]
        elif sec < first:
            if sec < smallest:
                smallest = sec
                state = diclist[i]
        i += 1
    return state
コード例 #8
0
ファイル: trends.py プロジェクト: alvinsgithub/TwitterTrends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    loc = tweet_location(tweet)
    closest_state = 'CA' #initialize to CA
    min_dist = geo_distance(state_centers['CA'], loc)
    for state in state_centers:
        dist = geo_distance(state_centers[state], loc)
        if dist < min_dist:
            closest_state = state
            min_dist = dist
    return closest_state
コード例 #9
0
ファイル: trends.py プロジェクト: DevelopingCoder/Projects
def group_tweets_by_state(tweets):
    """Return a dictionary that groups tweets by their nearest state center.

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

    Arguments:
    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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    State_Centroids = {State:find_state_center(us_states[State]) for State in us_states.keys()} #makes a dictionary in the format: {State: Centroid}
    current_lst = []
    for tweet in tweets:
        closest_tweet = 1 + geo_distance(tweet_location(tweet), State_Centroids['CA']) # Default Comparison
        for state in us_states.keys():
            distance = geo_distance(tweet_location(tweet), State_Centroids[state])
            if distance < closest_tweet:
                closest_tweet = distance
                closest_state = state
        current_lst += [[closest_state] + [tweet]]
    return group_by_key(current_lst)
コード例 #10
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to state shapes

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_pos = make_position(tweet['latitude'], tweet['longitude'])
    minimum = 9999999
    flag = ''
    for item in state_centers:
        if minimum > geo_distance(tweet_pos, state_centers[item]):
            minimum = geo_distance(tweet_pos, state_centers[item])
            flag = item
    return flag
コード例 #11
0
ファイル: trends.py プロジェクト: shamhub/python_programming
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    best_distance = None
    closest_state = None	
    for state, centre_position_of_state in state_centers.items():
        if best_distance == None:
            best_distance =  geo_distance(centre_position_of_state, tweet_location(tweet))
            closest_state = state			
            continue
        else:
            distance = geo_distance(centre_position_of_state, tweet_location(tweet)) 
            if  distance < best_distance:
                best_distance = distance
                closest_state = state			
    return closest_state
コード例 #12
0
ファイル: trends.py プロジェクト: PMX10/projects
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to state shapes

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_pos = make_position(tweet['latitude'], tweet['longitude'])
    minimum = 9999999
    flag = ''
    for item in state_centers:
        if minimum > geo_distance(tweet_pos, state_centers[item]):
            minimum = geo_distance(tweet_pos, state_centers[item])
            flag = item
    return flag
コード例 #13
0
ファイル: trends.py プロジェクト: akhilnambiar/trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    origin=tweet_location(tweet)
    dist=make_position(1000000000000000,100000000000000000)
    for x in state_centers.values():
        if geo_distance(origin,dist)>geo_distance(origin,x):
            dist=x
    dict_keys=state_centers.keys()
    for key in dict_keys:
        if state_centers[key]==dist:
            return key
コード例 #14
0
ファイル: felix trends.py プロジェクト: phorust/howmanygiven
def find_closest_state(tweet, state_centers):
	"""Return the name of the state closest to the given tweet's location.

	Use the geo_distance function (already provided) to calculate distance
	in miles between two latitude-longitude positions.

	Arguments:
	tweet -- a tweet abstract data type
	state_centers -- a dictionary from state names to positions.

	>>> us_centers = {n: find_center(s) for n, s in us_states.items()}
	>>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
	>>> ny = make_tweet("welcome to new York", None, 41, -74)
	>>> find_closest_state(sf, us_centers)
	'CA'
	>>> find_closest_state(ny, us_centers)
	'NJ'
	"""
	"*** YOUR CODE HERE ***"
	#such a shame that this code can't be used T________T
	d1 = tweet_location(tweet)
	s = list(state_centers.items())
	i = 1
	name = ''
	winner = geo_distance(d1, s[0][1])
	while i < len(s) - 1:
		if winner > geo_distance(d1, s[i][1]):
			winner = geo_distance(d1, s[i][1])
			name = s[i][0]
		i += 1
	return name
	"""# please help me eddie
コード例 #15
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    melhor = None
    EstadoProximo = None
    for estado, centre_position_of_state in state_centers.items():
        if melhor == None:
            melhor = geo_distance(centre_position_of_state,
                                  tweet_location(tweet))
            EstadoProximo = estado
            continue
        else:
            distancia = geo_distance(centre_position_of_state,
                                     tweet_location(tweet))
            if distance < melhor:
                melhor = distance
                EstadoProximo = estado

    return EstadoProximo
コード例 #16
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    origin = tweet_location(tweet)
    dist = make_position(1000000000000000, 100000000000000000)
    for x in state_centers.values():
        if geo_distance(origin, dist) > geo_distance(origin, x):
            dist = x
    dict_keys = state_centers.keys()
    for key in dict_keys:
        if state_centers[key] == dist:
            return key
コード例 #17
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """

    tweet_geo = tweet_location(tweet)
    menor_distancia = float('inf')
    for chave in state_centers:
        if geo_distance(tweet_geo, state_centers[chave]) < menor_distancia:
            menor_distancia = geo_distance(tweet_geo, state_centers[chave])
            estado = chave
    return estado
コード例 #18
0
ファイル: trends.py プロジェクト: Jtcollins/TwitterTimeline
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    if "AA" in us_states:
        del us_states["AA"]
    inverse_state_centers = {state: coord for coord, state in state_centers.items()}
    location = tweet_location(tweet)
    closest = make_position(200, 200)
    closest_state = "AK"
    for state in state_centers:
        center = make_position(state_centers[state][0], state_centers[state][1])
        if abs(geo_distance(center, location)) < abs(geo_distance(closest, location)):
            closest = center
            closest_state = inverse_state_centers[center]
    return closest_state
コード例 #19
0
ファイル: trends.py プロジェクト: ylsac/YLSA--YLSAC---YVES
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"

    posicao = tweet_location(tweet)
    for x in state_centers:
        menor = (state_centers[x][0], state_centers[x][1])
        local_tweet = x
        menorDist = geo_distance(posicao, menor)
        for y in state_centers:
            estado = (state_centers[y][0], state_centers[y][1])
            if geo_distance(posicao, estado) < menorDist:
                menorDist = geo_distance(posicao, estado)
                local_tweet = y
        break
    return local_tweet
コード例 #20
0
def group_tweets_by_state(tweets):
    """Return a dictionary that groups tweets by their nearest state center.

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

    Arguments:
    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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    "*** YOUR CODE HERE ***"
    min = 1000000
    lst = []
    for tweet in tweets:
        position = tweet_location(tweet)
        for key in us_states.keys():
            center = find_state_center(us_states[key])
            if geo_distance(center, position) < min:
                min = geo_distance(center, position)
                state = key
        lst = lst + [[state, tweet]]
    return group_by_key(lst)
コード例 #21
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    place=tweet_location(tweet)
    shortest=None
    for name,pos in state_centers.items():
        if shortest==None or shortest>=geo_distance(place,state_centers[name]):
            shortest=geo_distance(place,pos)
            name_shortest=name
    return name_shortest
コード例 #22
0
ファイル: trends.py プロジェクト: shivasupraj/Twitter-Trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    min_distance = None
    min_distance_state = None
    for state, center in state_centers.items():
        if min_distance is None:
            #print(tweet_location(tweet), center)
            min_distance = geo_distance(tweet_location(tweet), center)
            min_distance_state = state
        elif min_distance > geo_distance(tweet_location(tweet), center):
            min_distance = geo_distance(tweet_location(tweet), center)
            min_distance_state = state
    return min_distance_state
コード例 #23
0
ファイル: trends.py プロジェクト: phorust/howmanygiven
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    # please help me eddie
    mindiststate = "CA"
    tweetloc = tweet_location(tweet)
    mindist = geo_distance(tweetloc, state_centers["CA"])
    for state in state_centers.items():
        dist = geo_distance(tweetloc, state[1])
        if dist <= mindist:
            mindist = dist
            mindiststate = state[0]
    return mindiststate
コード例 #24
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """

    distancias = []
    for x in state_centers:
        dist = geo_distance(
            make_position(tweet["latitude"], tweet["longitude"]),
            make_position(state_centers[x][0], state_centers[x][1]))
        distancias.append(dist)
    for x in state_centers:
        if geo_distance(
                make_position(tweet["latitude"], tweet["longitude"]),
                make_position(state_centers[x][0],
                              state_centers[x][1])) == min(distancias):
            estado = x

    return estado
コード例 #25
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    tweet_position = tweet_location(tweet)
    result = ['CA', geo_distance(tweet_position, state_centers['CA'])]
    for n, center in state_centers.items():
        distance = geo_distance(tweet_position, center)
        if distance < result[1]:
            result[0] = n
            result[1] = distance
    return result[0]
コード例 #26
0
ファイル: trends.py プロジェクト: ha0k/CS61A-Projects
def group_tweets_by_state(tweets):
    """Return a dictionary that groups tweets by their nearest state center.

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

    Arguments:
    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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    centers = {state: find_state_center(us_states[state]) for state in us_states}
    
    state_tweets = []
    for tweet in tweets:
        distance = geo_distance(centers['CA'], tweet_location(tweet))
        location = 'CA'
        for state in centers:
            if geo_distance(centers[state], tweet_location(tweet)) < distance:
                location = state
                distance = geo_distance(centers[state], tweet_location(tweet))
        state_tweets.append([location, tweet])
    final = group_by_key(state_tweets)
    return final
コード例 #27
0
ファイル: trends.py プロジェクト: vivek8943/GeoProject
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a immutable dictionary from state names to positions

    >>> us_centers = make_idict()
    >>> for n, s in idict_items(us_states):
    ...     us_centers = idict_insert(us_centers, n, find_center(s)) 
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"

    loc=tweet_location(tweet)

    keys = idict_keys(state_centers)
    minDist = 1000000
    for key in keys:
        centerloc=idict_select(state_centers,key)
        if(geo_distance(loc, centerloc)<minDist):
            minDist= geo_distance(loc, centerloc)
            closest = key

    return closest
コード例 #28
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """

    i = 0
    minn = 0
    min_state = None
    for s in state_centers:
        if i == 0:
            minn = geo_distance(state_centers.get(s),
                                (tweet['latitude'], tweet['longitude']))
            min_state = s
            i += 1
        else:
            if geo_distance(state_centers.get(s),
                            (tweet['latitude'], tweet['longitude'])) < minn:
                minn = geo_distance(state_centers.get(s),
                                    (tweet['latitude'], tweet['longitude']))
                min_state = s
    return min_state
コード例 #29
0
ファイル: trends.py プロジェクト: marcuscarr/UCB_cs61a
 def closest_state(location):
     closest_state = 'AK'
     for state in us_states.keys():
         if geo_distance(location, centroids[state]) \
                         < geo_distance(location, centroids[closest_state]):
             closest_state = state
     return closest_state
コード例 #30
0
ファイル: trends.py プロジェクト: uknowmath/PROJETO-IF968
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    local = tweet_location(tweet)
    for inicio in state_centers:
        minimo = (state_centers[inicio][0],state_centers[inicio][1])
        state = inicio
        distanciaMin = geo_distance(local,minimo)
        for coord in state_centers:
            stateLocal = (state_centers[coord][0],state_centers[coord][1])
            if geo_distance(local,stateLocal) < distanciaMin:
                distanciaMin = geo_distance(local,stateLocal)
                state = coord
        break
    return state
コード例 #31
0
 def find_closest_state(tweet):
     loc = tweet_location(tweet)
     min = 123456789
     for state in state_centers_dict:
         if geo_distance(loc, state_centers_dict[state]) <= min:
             min = geo_distance(loc, state_centers_dict[state])
             min_state = state
     return min_state
コード例 #32
0
 def closest_state(position):
     states_center = {n: find_state_center(s) for n, s in us_states.items()}
     distance = 3963.2  #earth_radius in miles
     closest = ''
     for state, center in states_center.items():
         if geo_distance(position, center) < distance:
             distance = geo_distance(position, center)
             closest = state
     return closest.upper()
コード例 #33
0
ファイル: trends.py プロジェクト: fanjinhua/CS61A
 def close_to_state(location):
     distance = float_info.max
     closest_state = ''
     for s in us_states:
         # print(geo_distance(find_state_center(us_states[s]), location), '---------------------')
         if geo_distance(find_state_center(us_states[s]), location) < distance:
             distance = geo_distance(find_state_center(us_states[s]), location)
             closest_state = s
     return closest_state   # if closest_state else None
コード例 #34
0
ファイル: trends.py プロジェクト: Mengxinqian/Projects
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
コード例 #35
0
ファイル: trends.py プロジェクト: bianyin102938/cs61a
 def add_nearest_state_to_tweet(tweet):
     """ Return a pair of a tweet and its nearest state. """
     location = tweet_location(tweet)
     nearest_state = 'CA'
     for s in us_states:
         min_distance = geo_distance(location, find_state_center(us_states[nearest_state]))
         distance = geo_distance(location, find_state_center(us_states[s]))
         if distance < min_distance:
             nearest_state = s
     return (nearest_state, tweet)
コード例 #36
0
ファイル: trends.py プロジェクト: mandytrinh/61a-projects
 def closest_state(a_tweet,states_centers):
     tweet_loc = tweet_location(a_tweet)
     current_closest = geo_distance(tweet_loc, states_centers['CA']) # or whatever is syntax
     current_name = 'CA'
     for state_name,state_pos in states_centers.items():
         r = geo_distance(tweet_loc,state_pos)
         if r < current_closest:
             current_closest = r
             current_name = state_name
     return [current_name, a_tweet]
コード例 #37
0
ファイル: trends.py プロジェクト: xiongcunzhang/cs61a
 def add_nearest_state_to_tweet(tweet):
     """ Return a pair of a tweet and its nearest state. """
     location = tweet_location(tweet)
     nearest_state = 'CA'
     for s in us_states:
         min_distance = geo_distance(
             location, find_state_center(us_states[nearest_state]))
         distance = geo_distance(location, find_state_center(us_states[s]))
         if distance < min_distance:
             nearest_state = s
     return (nearest_state, tweet)
コード例 #38
0
ファイル: trends.py プロジェクト: hshwoo04/CS61A
 def nearest_state(tweet):
     tweet_state = 0
     shortest_distance = 100000000000
     for state in us_states:
         centroid = find_state_center(us_states[state])
         if geo_distance(centroid,
                         tweet_location(tweet)) < shortest_distance:
             shortest_distance = geo_distance(centroid,
                                              tweet_location(tweet))
             tweet_state = state
     return tweet_state
コード例 #39
0
ファイル: trends.py プロジェクト: YP-Ye/CS61A-1
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    location = tweet_location(tweet)
    # a dictionary of state name and it's distance to the tweet's location
    distances = {
        name: geo_distance(state_centers[name], location)
        for name in state_centers
    }
    min_distance = min([distances[name] for name in distances])
    for name in distances:
        if distances[name] == min_distance: return name
コード例 #40
0
ファイル: trends.py プロジェクト: willguo/trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    
    distance = 100000000000000
    new_distance = 0
    closest_state = None
    
    for state in state_centers:
        new_distance = geo_distance(make_position(tweet['latitude'], tweet['longitude']), state_centers[state])

        if new_distance < distance:
            distance = new_distance
            closest_state = state
            
    return closest_state
コード例 #41
0
ファイル: trends.py プロジェクト: jackzhao-mj/CS61A-1
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    location = tweet_location(tweet)
    # a dictionary of state name and it's distance to the tweet's location
    distances = {name: geo_distance(state_centers[name], location) for name in state_centers}
    min_distance = min([distances[name] for name in distances])
    for name in distances:
        if distances[name] == min_distance: return name
コード例 #42
0
ファイル: Trends Final.py プロジェクト: nmalboubi/Trends
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    location = tweet_location(tweet)
    closest_distance = None
    closest_state = 'CA'  # Arbitrary starting value
    for state_name, state_position in state_centers.items():
        distance = geo_distance(location, state_position)
        if closest_distance == None or distance < closest_distance:
            closest_distance, closest_state = distance, state_name
    return closest_state
コード例 #43
0
ファイル: trends.py プロジェクト: lyjdwh/sicp-python
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    "*** YOUR CODE HERE ***"
    from collections import defaultdict
    tweets_by_state = defaultdict(lambda: None)
    us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    for tweet in tweets:
        dist_from_center = lambda name: geo_distance(tweet_location(tweet),
                                                     us_centers[name])
        state = sorted(us_states.keys(), key=dist_from_center)[0]
        if tweets_by_state[state] is None:
            tweets_by_state[state] = [tweet]
        else:
            tweets_by_state[state].append(tweet)
    return tweets_by_state
コード例 #44
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
コード例 #45
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.


    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.


    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.


    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    
    lista_distancias = []
    for x in state_centers:
        distancia = geo_distance(tweet_location(tweet), state_centers[x]) #atribui a 'distancia' a distancia entre as coordenadas do tweet e de cada estado analisado pelo for
        lista_distancias.append((distancia, x)) #coloca esse valor numa lista 
    lista_distancias.sort() #após inserir todas as distancias entre o tweet_location e todos os estados americanos, dá um sort na lista
    menor_distancia = lista_distancias.pop(0) #retira o primeiro elemento (de índice 0), que consequentemente é o de menor valor (menor distância)
    return menor_distancia[1] #retorna da variavel 'menor_distancia' o elemento de índice 1 (que corresponde ao estado, como atribuido anteriormente) ex: [(270, 'ca')]
コード例 #46
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    menordist = 10000000
    estado = ''
    for i in state_centers:  #state_centers devolve lat/lon dos estados
        distancia = geo_distance(tweet_location(tweet), state_centers[i])
        if distancia <= menordist:
            menordist = distancia
            estado = i
    return estado
コード例 #47
0
ファイル: trends.py プロジェクト: m0cahxD/cs61a
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    "*** YOUR CODE HERE ***"
    min_so_far = float('inf')
    going_the_distance = ''
    for x in tweets:
        for y in us_states.keys():
            dist = geo_distance(tweet_location(x), find_state_center(us_states[y]))
            if dist < min_so_far:
                going_the_distance = y
                min_so_far = dist
        if going_the_distance not in tweets_by_state.keys():
            tweets_by_state[going_the_distance] = []
        tweets_by_state[going_the_distance].append(x)

    return tweets_by_state
コード例 #48
0
ファイル: trendswcomments.py プロジェクト: zarachiara/Twitter
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    distance_dictionary = {}
    for state in state_centers: 
        locate = tweet_location(tweet)
        center = state_centers[state] # what should state_centers be taking in as its element? 
        distance_dictionary[state] = geo_distance(locate, center)
    return min(distance_dictionary, key = distance_dictionary.get) # returns default value if key is missing
コード例 #49
0
ファイル: trends.py プロジェクト: MicBrain/Twitter_trends
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    states_centers = {state: find_state_center(us_states[state]) for state in us_states.keys()} # creates the dictionary state_centers that contains each state it's its respective state center   
    for tweet in tweets: # goes through every tweet and assigns it to the dictionary tweets_by_state by its closest state center
      closest, state_name = 0, '' 
      for state in states_centers:
        distance = geo_distance(tweet_location(tweet), states_centers[state]) # adds the distance from tweet to each state center to the list called distances 
        if closest == 0 or distance < closest:
          closest = distance  # if current state is closer than previous closest state, reassigns closest and state_name accordingly
          state_name = state
      if state_name in tweets_by_state:   # if this state is already defined in the dictionary then the tweets is added to the existing state key
        tweets_by_state[state_name].append(tweet) 
      else: # if this state does not already contain a position in the directoy, then one is created
        tweets_by_state[state_name] = [tweet]
    return tweets_by_state   # Returns the dictionary that has aggregated tweets by their nearest state center
コード例 #50
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    location = tweet_location(tweet)
    state = ""
    distance = 0.0
    distances = []
    for key, value in state_centers.items():
        distances += [[geo_distance(location, value), key]]
    distance = distances[0][0]
    for i, j in distances:
        if distance > i:
            distance = i
    for i, j in distances:
        if distance == i:
            state += j
    return state
コード例 #51
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> lbg = make_tweet("welcome to lewisburg!", None, 40.96, -76.89)
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    >>> find_closest_state(lbg, us_centers)
    'PA'
    """

    tweetPosition = tweet_location(tweet)
    stateClose = 'XX'
    minDistance = 10000.0
    for stateCode in us_states.keys():
        
        distance = geo_distance(tweetPosition, state_centers[stateCode])
        if distance < minDistance:
            minDistance = distance
            stateClose = stateCode
            
    return stateClose   
コード例 #52
0
ファイル: trends.py プロジェクト: AlfiyaZi/trends
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    states_centers = {state: find_state_center(us_states[state]) for state in us_states.keys()} #generates dictionary with states and their center positions   
    for tweet in tweets:
      closest = 999999999999 #initialize to very large distance value
      name = '' #initialize closest state name
      for state in states_centers:
        distance = geo_distance(tweet_location(tweet), states_centers[state]) #calculates distance to  all state centers 
        if distance < closest:
          closest = distance #saves closest distance and state name if new state is closer than previous best
          name = state
      #add tweet to appropriate entry or create new entry if nonexistent:
      if name not in tweets_by_state:
        tweets_by_state[name] = [tweet]
      elif name in tweets_by_state:
        tweets_by_state[name].append(tweet)
    return tweets_by_state
コード例 #53
0
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = defaultdict(list)
    state_centers = {}
    for elem in us_states:
        state_centers[elem] = find_state_center(us_states[elem])
    for elm in tweets:
        dist = 999999999999
        state = ''
        for elem in state_centers:
            dist1 = geo_distance(tweet_location(elm), state_centers[elem])
            if dist1 < dist:
                dist = dist1
                state = elem
        tweets_by_state[state].append(elm)
    return tweets_by_state
コード例 #54
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    #posicao = tweet_location(tweet)
    #state_centers.items()
    #comparar e retornar o menor
    #for
    position = tweet_location(tweet)
    aux = 0
    aux1 = 99999999999999999999999999
    closest = 0
    for x in state_centers.items():
        aux = geo_distance(position, x[1])
        if aux < aux1:
            closest = x[0]
            aux1 = aux
    return closest
コード例 #55
0
 def i_am_closest_state(tweet):
     for other_states in state_locations:
         if geo_distance(tweet_loc,
                         state_locations[other_states]) < my_distance:
             # print(other_states, " is closer to    ", tweet_text(i) ,"    tweet than ", state)
             return False
     return True
コード例 #56
0
ファイル: trends.py プロジェクト: drixta/61a
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    length = 99999999 #arbitrarily large number
    closest_state = ""
    for state in state_centers:
        distance = geo_distance(tweet_location(tweet),state_centers[state])
        if distance < length:
            length = distance #reassignment
            closest_state = state
    return closest_state
コード例 #57
0
ファイル: trends.py プロジェクト: FZSS/trends
def group_tweets_by_state(tweets):
    """Return a dictionary that aggregates tweets by their nearest state center.

    The keys of the returned dictionary 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])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = {}
    for tweet in tweets:
        nearest_distance = 100000000000
        nearest_state = ""
        for state in us_states.keys():
            distance = geo_distance(find_state_center(us_states[state]), tweet_location(tweet))
            if distance < nearest_distance:
                nearest_distance = distance
                nearest_state = state
        if nearest_state in tweets_by_state.keys():
            tweets_by_state[nearest_state].append(tweet)
        else:
            tweets_by_state[nearest_state] = [tweet] 
    return tweets_by_state
コード例 #58
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_loc=tweet_location(tweet)
    us_state_list=list(state_centers.keys())
    dis_prev=0;dis_curr=0
    for n in range (0,len(us_state_list)):
        dis_prev=dis_curr
        dis_curr=geo_distance(tweet_loc,state_centers[us_state_list[n]])
        if n==0:
            dis_min=dis_curr
        else:
            if dis_prev <= dis_min:
                dis_min=dis_prev
                cl_state=us_state_list[n-1]
            elif dis_curr <= dis_min:
                dis_min=dis_curr
                cl_state=us_state_list[n]
    return cl_state