예제 #1
0
    if len(listOfWords) > 3:
        numTweets[tweetLocationKey] += 1
        sentiTweets[tweetLocationKey] += total / len(listOfWords)

for state in numTweets:
    if numTweets[state] != 0:
        sentiTweets[state] = sentiTweets[state] / numTweets[state]
    if sentiTweets[state] == 0:
        sentiTweets[state] = None

nationalAverage = totalNational / numWords

for stateCode in sentiTweets:
    color = get_sentiment_color(sentiTweets[stateCode])
    usa.setFillColor(stateCode, color)

maxSenti = max(sentiTweets.itervalues())

minSenti = min(x for x in sentiTweets.itervalues() if x != None)

for state in sentiTweets:
    if sentiTweets[state] == maxSenti:
        maxState = state
    if sentiTweets[state] == minSenti:
        minState = state

print 'National sentiment value {}'.format(('%.2f' % nationalAverage))
print 'Highest sentiment value of {} in {}'.format(('%.2f' % maxSenti),
                                                   maxState)
print 'Lowest sentiment values of {} in {}'.format(('%.2f' % minSenti),
예제 #2
0
class SentimentAnalysis:
    def __init__(self):
        self.sentiments = load_sentiments()
        self.states = load_states()
        self.tweets = load_tweets()
        self.usa = None  # variables should always be defined in __init__

    def show_country(self):  # function names should not have capital letters
        self.usa = Country(self.states, 1200)

    def update_sentiments(self, query_list):
        state_totals = dict()
        for tweet in self.tweets:
            if all(query_word.lower() in tweet.message().lower() for query_word
                   in query_list):  # if every query word is in the tweet
                sent = self.weigh_message(tweet.message().lower())
                place = self.place_tweet(tweet.position())
                if sent != -1:  # word was actually weighted
                    if place not in state_totals:
                        state_totals[place] = [0, 0]
                    state_totals[place][0] += sent  # total sentiment value
                    state_totals[place][
                        1] += 1  # total number of tweets from state

        for state in self.states:
            if state.abbrev() in state_totals:
                average_sent = state_totals[state.abbrev()][0] / state_totals[
                    state.abbrev()][1]
                self.usa.setFillColor(state.abbrev(),
                                      get_sentiment_color(average_sent))
                print(state.abbrev(), state_totals[state.abbrev()][1])
            else:
                self.usa.setFillColor(state.abbrev(),
                                      get_sentiment_color(None))

    def place_tweet(self, position):
        min_dist = 999999999999  # a really big number
        close_state = None
        geo_pos = GeoPosition(position[1], position[0])
        for state in self.states:
            dist = state.centroid().distance(geo_pos)
            if dist < min_dist:
                min_dist = dist
                close_state = state

        return close_state.abbrev(
        )  # using abbrev as keys instead of reference makes debugging a bit easier

    def weigh_message(self, message):
        total = 0
        split_str = message.split()
        num_words = 0
        for word in split_str:
            if word in self.sentiments:
                num_words += 1
                total += self.sentiments[word]
        if not num_words:
            return -1
        total /= num_words

        return total