def map_election_results(filename): with open(filename) as fin: df = pd.read_csv(fin, delimiter='\t', header=0) colors = ['#0000FF', '#ff0000'] states = {} for i in xrange(df.shape[0]): states[df['State'][i]] = 0 if df['Winner'][i]=='Obama' else 1 smp.make_us_state_map(data=states, colors=colors)
def main(): stateCenterDict = {} #Key: state abbrev Value: 2-tuple of (lat,long) of state center tweetList = [] #list of two items, first is 2-tuple (lat,long), second is tweet stateCountDict = {} #Key: state abbrev Value: count for word stateColor = {} #Key, state abbrev Value: frequency count readStateCenterFile(stateCenterDict) readTweetFile(tweetList) for key in stateCenterDict: stateCountDict[key] = 0 stateColor[key] = 0 wordOfInterest = input("what word are you looking for? ") wordOfInterest = wordOfInterest.lower() # find location of tweet and assign word count for item in tweetList: state= None minDist = 400 pos = item[0] tweet = item[1] lat = pos[0] long = pos[1] if lat > 55: state = 'AK' elif lat < 26: state = 'HI' else: for s in stateCenterDict.keys(): sLat = stateCenterDict[s][0] sLong = stateCenterDict[s][1] d = distance(sLat, sLong, lat, long) if d < minDist: state = s minDist = d # assign value and sentiment score to each state if wordOfInterest in tweet.lower(): if state in stateCenterDict.keys(): stateCountDict[state]+=1 assignStateColor(stateCountDict, stateColor) print(stateCountDict) print(stateColor) COLORS = ["#FFFFFF", "#EBAD99", "#E08566", "#D65C33", "#CC3300", "#A32900", "#7A1F00"] make_us_state_map(stateColor, COLORS)
stateSentimentScoreDict[ st] = stateSentimentScoreDict[st] + fullDict[sent] ########## Average Sentiment Score ########### for State in stateCountDict: if stateCountDict[State] != 0: stateSentimentScoreDict[State] = (stateSentimentScoreDict[State] / stateCountDict[State]) #print(State, stateCountDict[State], stateSentimentScoreDict[State]) for state in stateAbbrevList: print(state, "\n" "state score: ", stateSentimentScoreDict[state], "\n" "state count: ", stateCountDict[state]) ########### Color Assignment ############## minimum = findMin(stateSentimentScoreDict) maximum = findMax(stateSentimentScoreDict) for ST in stateSentimentScoreDict: stateSentimentScoreDict[ST] = assignColor(ST, stateSentimentScoreDict, maximum, minimum) make_us_state_map(stateSentimentScoreDict, SENTIMENTCOLORS)
def main(): #initilizing dictionaries and lists stateCenterDict = {} #Key: state abbrev Value: 2-tuple of (lat,long) of state center tweetList = [] #list of two items, first is 2-tuple (lat,long), second is tweet sentimentDict = {} #Key: words/phrases Value: sentiment score from -1 .. 1 for each word #Initilizing these two dictionaries stateSentimentScore = {} stateCountDict = {} #opening the sentimentDictionary using the readSentimentFile function readSentimentFile(sentimentDict) #reading the stateCenterDict file with the function readStateCenterFile readStateCenterFile(stateCenterDict) #reading and opening the tweetList file using the readTweetFile function readTweetFile(tweetList) #Setting the values of the stateCountDict and stateSentimentScore to 0 at each index for future use for state in stateCenterDict.keys(): stateCountDict[state] = 0 stateSentimentScore[state] = 0 #Getting user input wordOfInterest=getWordOfInterest() #creating the InterestList of tweets that have the word of Interest in it. #Using the findInterestList function InterestList=findInterestList(tweetList,wordOfInterest) #creating the interestState list, from findInterestState function. which is states that the tweets of interest are in. InterestState=findInterestState(InterestList,stateCenterDict) #if there is no data in index i, add 'addToDict' to the dictionary at that index position. for i in InterestState: if i != None: g=InterestState.count(i) addToDict={i:g} stateCountDict.update(addToDict) #updating the sentimentScore, and the statSentimentScore by calling the updateScores function sentimentScore, stateSentimentScore=updateScores(InterestList,sentimentDict,InterestState,stateSentimentScore) #sorts the stateCountDict keys by alphabetical order. for each state,divides the score by how many tweets in that state, creating the average #print out the data for state in sorted(stateCountDict.keys()): if stateCountDict[state] != 0: stateSentimentScore[state] = stateSentimentScore[state]/stateCountDict[state] print(state, stateCountDict[state], stateSentimentScore[state]) else: print(state, stateCountDict[state], stateSentimentScore[state]) #calling the findMaximum and findMinimum functions to determine the min and max scores of the list. values=stateSentimentScore.values() minimum=findMinimum(values) maximum=findMaximum(values) #This calls the colorDictionary to create the final stateSentimentScore to be used in the coloring of the US map stateSentimentScore=colorDictionary(minimum,maximum,stateSentimentScore) ################## #I used color brewer to help me pick the colors. #Red represents positive sentiment, shading to orange and yellow for less positive senitment. #Light gray means no data. #Dark blue represents negative sentiment, shading to light blue for less negative sentiment. ####INDEX#########[ 0 1 2 3 4 5 6 7 8 ] SENTIMENTCOLORS = ["#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#bababa", "#fee090", "#fdae61", "#f46d43", "#d73027"] ####COLOR#########["darkblue", "blue", "lightblue", "lightcyan", "lightgray", "yellow", "orange", "darkorange", "red"] make_us_state_map(stateSentimentScore, SENTIMENTCOLORS)
def main(): #initilizing dictionaries and lists stateCenterDict = { } #Key: state abbrev Value: 2-tuple of (lat,long) of state center tweetList = [ ] #list of two items, first is 2-tuple (lat,long), second is tweet sentimentDict = { } #Key: words/phrases Value: sentiment score from -1 .. 1 for each word #Initilizing these two dictionaries stateSentimentScore = {} stateCountDict = {} #opening the sentimentDictionary using the readSentimentFile function readSentimentFile(sentimentDict) #reading the stateCenterDict file with the function readStateCenterFile readStateCenterFile(stateCenterDict) #reading and opening the tweetList file using the readTweetFile function readTweetFile(tweetList) #Setting the values of the stateCountDict and stateSentimentScore to 0 at each index for future use for state in stateCenterDict.keys(): stateCountDict[state] = 0 stateSentimentScore[state] = 0 #Getting user input wordOfInterest = getWordOfInterest() #creating the InterestList of tweets that have the word of Interest in it. #Using the findInterestList function InterestList = findInterestList(tweetList, wordOfInterest) #creating the interestState list, from findInterestState function. which is states that the tweets of interest are in. InterestState = findInterestState(InterestList, stateCenterDict) #if there is no data in index i, add 'addToDict' to the dictionary at that index position. for i in InterestState: if i != None: g = InterestState.count(i) addToDict = {i: g} stateCountDict.update(addToDict) #updating the sentimentScore, and the statSentimentScore by calling the updateScores function sentimentScore, stateSentimentScore = updateScores(InterestList, sentimentDict, InterestState, stateSentimentScore) #sorts the stateCountDict keys by alphabetical order. for each state,divides the score by how many tweets in that state, creating the average #print out the data for state in sorted(stateCountDict.keys()): if stateCountDict[state] != 0: stateSentimentScore[ state] = stateSentimentScore[state] / stateCountDict[state] print(state, stateCountDict[state], stateSentimentScore[state]) else: print(state, stateCountDict[state], stateSentimentScore[state]) #calling the findMaximum and findMinimum functions to determine the min and max scores of the list. values = stateSentimentScore.values() minimum = findMinimum(values) maximum = findMaximum(values) #This calls the colorDictionary to create the final stateSentimentScore to be used in the coloring of the US map stateSentimentScore = colorDictionary(minimum, maximum, stateSentimentScore) ################## #I used color brewer to help me pick the colors. #Red represents positive sentiment, shading to orange and yellow for less positive senitment. #Light gray means no data. #Dark blue represents negative sentiment, shading to light blue for less negative sentiment. ####INDEX#########[ 0 1 2 3 4 5 6 7 8 ] SENTIMENTCOLORS = [ "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#bababa", "#fee090", "#fdae61", "#f46d43", "#d73027" ] ####COLOR#########["darkblue", "blue", "lightblue", "lightcyan", "lightgray", "yellow", "orange", "darkorange", "red"] make_us_state_map(stateSentimentScore, SENTIMENTCOLORS)
def simplemap(): colors = ["#FF0000", "#0000FF", "#800080"] # 0 = Red, 1 = Blue, 2 = Purple states = {} blue_states = {} red_states = {} purple_states = {} states["AL"] = 0 states["AK"] = 0 states["AZ"] = 0 states["AR"] = 0 states["CA"] = 1 states["CO"] = 1 states["CT"] = 1 states["DE"] = 1 states["FL"] = 2 states["GA"] = 0 states["HI"] = 1 states["ID"] = 0 states["IL"] = 1 states["IN"] = 0 states["IA"] = 0 states["KS"] = 0 states["KY"] = 0 states["LA"] = 0 states["ME"] = 1 states["MD"] = 1 states["MA"] = 1 states["MI"] = 1 states["MN"] = 1 states["MS"] = 0 states["MO"] = 0 states["MT"] = 0 states["NE"] = 0 states["NV"] = 2 states["NH"] = 1 states["NJ"] = 1 states["NM"] = 1 states["NY"] = 1 states["NC"] = 2 states["ND"] = 0 states["OH"] = 2 states["OK"] = 0 states["OR"] = 1 states["PA"] = 1 states["RI"] = 1 states["SC"] = 0 states["SD"] = 0 states["TN"] = 0 states["TX"] = 0 states["UT"] = 0 states["VT"] = 1 states["VA"] = 1 states["WA"] = 1 states["WV"] = 0 states["WI"] = 1 states["WY"] = 0 simplemapplot.make_us_state_map(data=states, colors=colors)
def main(): i = 0 stateCenterDict = { } #Key: state abbrev Value: 2-tuple of (lat,long) of state center tweetList = [ ] #list of two items, first is 2-tuple (lat,long), second is tweet sentimentDict = { } #Key: words/phrases Value: sentiment score from -1 .. 1 for each word stateSentimentScore = { } #Key: state abbrev Value: sentiment score for word stateCountDict = {} #Key: state abbrev Value: count for word readStateCenterFile(stateCenterDict) readTweetFile(tweetList) for key in stateCenterDict: stateCountDict[key] = 0 stateSentimentScore[key] = 0 wordOfInterest = input("what word are you looking for? ") wordOfInterest = wordOfInterest.lower() tweet_list_sandwich = display_tweetlist( tweetList) #list of text from all tweets found_Tweets = find_tweet( wordOfInterest, tweet_list_sandwich ) #list of positions of tweets that had the wordOfInterest states_tuple = display_stateDict(stateCenterDict, found_Tweets, tweetList) states_list = states_tuple[0] # list of every state used find_state = states_tuple[ 1] # a dictionary where key is position of tweet, and a value of state updated_Found_Tweets = states_tuple[ 2] #updated list of positions of tweets that have the word of interest and were assigned states counting_states(states_list, stateCenterDict, stateCountDict) # builds stateCountDict readSentimentFile(sentimentDict) #builds sentimentDict assign_sentiment_state(updated_Found_Tweets, tweet_list_sandwich, sentimentDict, stateSentimentScore, find_state, stateCountDict) state_sentiment_scores = stateSentimentScore #creating new variable SORTED = sorted(stateSentimentScore.keys() ) #organizing by keys aka state abbreviations for organize in SORTED: print(organize, stateCountDict[organize], state_sentiment_scores[organize]) Colors = assignSentimentColors( stateSentimentScore ) #dictionary how states and their index for color of map ########################################################################### # FILL IN YOUR MAIN CODE HERE - Add 3-5 functions outside of main to help. ########################################################################### ################## #I used color brewer to help me pick the colors. #Red represents positive sentiment, shading to orange and yellow for less positive senitment. #Light gray means no data. #Dark blue represents negative sentiment, shading to light blue for less negative sentiment. ####INDEX#########[ 0 1 2 3 4 5 6 7 8 ] SENTIMENTCOLORS = [ "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#bababa", "#fee090", "#fdae61", "#f46d43", "#d73027" ] ####COLOR#########["darkblue", "blue", "lightblue", "lightcyan", "lightgray", "yellow", "orange", "darkorange", "red"] make_us_state_map(stateSentimentScore, SENTIMENTCOLORS)