def drawResults(est, cnt, mnt, pac) : #scaling happiness scores to fit between 0 and 10 to make histogram maxVal = max(est, cnt, mnt, pac) def calcNewValue(value) : result = value * 10 / maxVal return result east = calcNewValue(est) centr = calcNewValue(cnt) mntn = calcNewValue(mnt) pacf = calcNewValue(pac) from happy_histogram import drawSimpleHistogram drawSimpleHistogram(east, centr, mntn, pacf)
def drawResults(est, cnt, mnt, pac): #scaling happiness scores to fit between 0 and 10 to make histogram maxVal = max(est, cnt, mnt, pac) def calcNewValue(value): result = value * 10 / maxVal return result east = calcNewValue(est) centr = calcNewValue(cnt) mntn = calcNewValue(mnt) pacf = calcNewValue(pac) from happy_histogram import drawSimpleHistogram drawSimpleHistogram(east, centr, mntn, pacf)
def main(): keyReader() tweetReader() # print all the information in our zoneValues dictionary for key in zoneValues: print( "The happiness score for the {} timezone is {}, and we found {} tweets in its range" .format(key, zoneValues[key][0], zoneValues[key][1])) # calling the drawSimpleHistogram function from happy_histogram happy_histogram.drawSimpleHistogram(zoneValues["pacific"][0], zoneValues["mountain"][0], zoneValues["central"][0], zoneValues["eastern"][0])
def main(): # Setting the initial empty 'happiness score' values for each zone. zone = time_zone_finder('tweets.txt') score = happiness_score('tweets.txt', 'keywords.txt') pacific_tweets = 0 mountain_tweets = 0 central_tweets = 0 eastern_tweets = 0 pacific = 0 mountain = 0 central = 0 eastern = 0 # Uses the previous functions to total up the score and the amount of tweets per zone. for x in range(0, len(zone)): if score[x] > 0: if zone[x] == "pacific": pacific += score[x] pacific_tweets += 1 elif zone[x] == "mountain": mountain += score[x] mountain_tweets += 1 elif zone[x] == "central": central += score[x] central_tweets += 1 elif zone[x] == "eastern": eastern += score[x] eastern_tweets += 1 # Rounds the values to 1 decimal point. pacific = round(pacific / pacific_tweets, 1) mountain = round(mountain / mountain_tweets, 1) central = round(central / central_tweets, 1) eastern = round(eastern / eastern_tweets, 1) # Final output messages. print("") print("Pacific Happiness Score: ", pacific) print("Pacific Total Tweets ", pacific_tweets) print("") print("Mountain Happiness Score: ", mountain) print("Mountain Total Tweets: ", mountain_tweets) print("") print("Central Happiness Score: ", central) print("Central Total Tweets: ", central_tweets) print("") print("Eastern Happiness Score: ", eastern) print("Eastern Total Tweets: ", eastern_tweets) # Draws the final picture display. happy_histogram.drawSimpleHistogram(eastern, central, mountain, pacific)
def main(): keywordsfile.close() tweetsfile.close() happy_score() for region in regionList: print(region + ': \n num of tweets: ' + str(happiness[region][3]) + ' num of valid tweets: ' + str(happiness[region][1]) + ' happiness score: ' + str(happiness[region][2])) print() # draw the happy histogram happy_histogram.drawSimpleHistogram(happiness['eastern'][2], happiness['central'][2], happiness['mountain'][2], happiness['pacific'][2])
def printResults(): # function used at the end to display results EasternScore = EasternTotal / EasternTweets # formula for each region's score is the total score divided by total tweets CentralScore = CentralTotal / CentralTweets MountainScore = MountainTotal / MountainTweets PacificScore = PacificTotal / PacificTweets print("The happiness score of the Eastern time zone is", round(EasternScore, 3), "and the total number of tweets counted is", EasternTweets) print("The happiness score of the Central time zone is", round(CentralScore, 3), "and the total number of tweets counted is", CentralTweets) print("The happiness score of the Mountain time zone is", round(MountainScore, 3), "and the total number of tweets counted is", MountainTweets) print("The happiness score of the Pacific time zone is", round(PacificScore, 3), "and the total number of tweets counted is", PacificTweets) drawSimpleHistogram( EasternScore, CentralScore, MountainScore, PacificScore) # calls the function that displays the emojis
def main(): #Main Class keywordList = (gettingKeywords() ) #Callling on the method to read the keywords file tweetScore = (getAndProcessTweetFile(keywordList) ) #Calling method to read the tweet file and process it tweetPerRegion = [ tweetsFromEach[0], tweetsFromEach[1], tweetsFromEach[2], tweetsFromEach[3] ] #Used as a replica of tweetsFromEach, however it serves the function of preventing a divde by zero error if 0 tweets are from region if ( tweetWSenti[0] == 0 ): #If the region had 0 tweets to avoid error. Make the tweet from each 1. As a region tweetPerRegion[ 0] = 1 #with 0 tweets by default the region will have a score of 0. Therefore, the tweet score if ( tweetWSenti[1] == 0 ): # 0 divided by the modified tweet count of the value for the region will result in 0 tweetPerRegion[1] = 1 if (tweetWSenti[2] == 0): tweetPerRegion[2] = 1 if (tweetWSenti[3] == 0): tweetPerRegion[3] = 1 finalScore = [ tweetScore[0] / tweetPerRegion[0], tweetScore[1] / tweetPerRegion[1], tweetScore[2] / tweetPerRegion[2], tweetScore[3] / tweetPerRegion[3] ] print("The hapinness score for the Pacific Time Zone is", finalScore[0], "from", tweetsFromEach[0], "tweets") print("The hapinness score for the Mountain Time Zone is", finalScore[1], "from", tweetsFromEach[1], "tweets") print("The hapinness score for the Central Time Zone is", finalScore[2], "from", tweetsFromEach[2], "tweets") print("The hapinness score for the Eastern Time Zone is", finalScore[3], "from", tweetsFromEach[3], "tweets") happy_histogram.drawSimpleHistogram(finalScore[3], finalScore[2], finalScore[1], finalScore[0]) #Drawing the histogram
def main(): keywordDict = readKeywords() # # for key in sorted(keywordDict): # # print(key, keywordDict[key]) eastern, central, mountain, pacific = readTweets() print("\neastern") score1, numTweets = calcHap(eastern, keywordDict) print("%-15s %5.2f" % ("happiness", score1)) print("%-15s %5.0d" % ("total tweets", numTweets)) # for n in eastern: # print(n) print("\ncentral") score2, numTweets = calcHap(central, keywordDict) print("%-15s %5.2f" % ("happiness", score2)) print("%-15s %5.0d" % ("total tweets", numTweets)) # for n in central: # print(n) print("\nmountain") score3, numTweets = calcHap(mountain, keywordDict) print("%-15s %5.2f" % ("happiness", score3)) print("%-15s %5.0d" % ("total tweets", numTweets)) # for n in mountain: # print(n) print("\npacific") score4, numTweets = calcHap(pacific, keywordDict) print("%-15s %5.2f" % ("happiness", score4)) print("%-15s %5.0d" % ("total tweets", numTweets)) # for n in pacific: # print(n) happy_histogram.drawSimpleHistogram(score1, score2, score3, score4)
closeFile(tweetsFile) #Displays empty lines and the happiness score in each time zone as well as the number of tweets in each time zone print() print("The happiness score in the Eastern timezone is:", round(easternHappiness/easternTweets, 2)) print("The number of tweets in the Eastern timezone is:", easternTweets) print() print("The happiness score in the Central timezone is:", round(centralHappiness/centralTweets, 2)) print("The number of tweets in the Central timezone is:", centralTweets) print() print("The happiness score in the Mountain timezone is:", round(mountainHappiness/mountainTweets, 2)) print("The number of tweets in the Mountain timezone is:", mountainTweets) print() print("The happiness score in the Pacific timezone is:", round(pacificHappiness/pacificTweets, 2)) print("The number of tweets in the Pacific timezone is:", pacificTweets) print() ##calls the function from the graphics file used in this program and sends the happiness value all the different #timezones to it happy_histogram.drawSimpleHistogram(easternHappiness/easternTweets, centralHappiness/centralTweets, mountainHappiness/mountainTweets,pacificHappiness/pacificTweets) #excpet IOErrors and ValueErrors and display and error message except IOError as exception: print("Error,", exception) except ValueError as exception: print("Error,", exception)
def main(): keywords_filename = input( 'Please enter the file name containing keywords: ') keywords_file_handle = open_if_file_exists(keywords_filename) keywordsList = readData(keywords_file_handle) keywords_file_handle.close() for i in range(len(keywordsList)): keywordsList[i] = keywordsList[i].split(',') keywordsList[i][1] = int(keywordsList[i][1]) tweets_filename = input('Please enter the file name containing tweets: ') tweets_file_handle = open_if_file_exists(tweets_filename) tweetsList = readData(tweets_file_handle) tweets_file_handle.close() for i in range(len(tweetsList)): tweetsList[i] = tweetsList[i].split(' ') stripped_full_list = [] for i in range(len(tweetsList)): stripped_tweet_list = [] for ch in tweetsList[i]: ch = ch.strip('[],') stripped_tweet_list.append(ch) stripped_full_list.append(stripped_tweet_list) four_lists = split_by_region(stripped_full_list) pacific_list = four_lists[0] mountain_list = four_lists[1] central_list = four_lists[2] eastern_list = four_lists[3] sentiment_list_pacific = compute_sentiment(pacific_list, keywordsList) sentiment_list_eastern = compute_sentiment(eastern_list, keywordsList) sentiment_list_mountain = compute_sentiment(mountain_list, keywordsList) sentiment_list_central = compute_sentiment(central_list, keywordsList) happinessScore = happiness_score_by_region(sentiment_list_pacific) print('The happiness score for the pacific region is:', happinessScore, 'and the number of tweets for this region is:', len(sentiment_list_pacific)) happinessScore = happiness_score_by_region(sentiment_list_eastern) print('\nThe happiness score for the eastern region is:', happinessScore, 'and the number of tweets for this region is:', len(sentiment_list_eastern)) happinessScore = happiness_score_by_region(sentiment_list_mountain) print('\nThe happiness score for the mountain region is:', happinessScore, 'and the number of tweets for this region is:', len(sentiment_list_mountain)) happinessScore = happiness_score_by_region(sentiment_list_central) print('\nThe happiness score for the central region is:', happinessScore, 'and the number of tweets for this region is:', len(sentiment_list_central)) drawSimpleHistogram(happiness_score_by_region(sentiment_list_eastern), happiness_score_by_region(sentiment_list_central), happiness_score_by_region(sentiment_list_mountain), happiness_score_by_region(sentiment_list_pacific))
def main(): keywords = [] #List words = [] #List twCntPacific = 0 scorePacific = 0 twCntMountain = 0 scoreMountain = 0 twCntCentral = 0 scoreCentral = 0 twCntEastern = 0 scoreEastern = 0 # Read keywords and store in list fileNameKeywords = input("Enter the file name for keyword file: ") try: # fileKeywords = open(fileNameKeywords, "r") fileKeywords = open("keywords.txt", "r") except IOError as err: # Error reading file. Quit print("Error, File not found!") quit() for line in fileKeywords: # print(line) line = line.split(",") keywords.append([line[0], int(line[1])]) keywords.sort() #incase the keywords are not sorted fileNameTweets = input("Enter the file name for Tweets file: ") try: # fileTweets = open(fileNameTweets, "r") fileTweets = open("tweets.txt", "r") except IOError as err: # Error reading file. Quit print("Error, File not found!") quit() for line in fileTweets: # print(line) lineContent = line.split(" ", 5) latitude = (float)(lineContent[0][1:-1]) longitude = (float)(lineContent[1][:-1]) # print(latitude) # print(longitude) words = lineContent[5].strip(".,@!?/\\:#'\"").split() # print(words) lineVal = 0 for x in words: lineVal = lineVal + binarySearch(keywords, x) lineScore = lineVal / len(words) #Select timezone #Latitude Range if 24.660845 <= latitude <= 49.189787: #Longitude Range if -125.242264 <= longitude <= -67.444574: #Pacific if -125.242264 <= longitude < -115.236428: # print("Pacific") twCntPacific = twCntPacific + 1 scorePacific = scorePacific + lineScore elif -115.236428 <= longitude < -101.998892: # print("Mountain") twCntMountain = twCntMountain + 1 scoreMountain = scoreMountain + lineScore elif -101.998892 <= longitude < -87.518395: # print("Central") twCntCentral = twCntCentral + 1 scoreCentral = scoreCentral + lineScore elif -87.518395 <= longitude < -67.444574: # print("Eastern") twCntEastern = twCntEastern + 1 scoreEastern = scoreEastern + lineScore if twCntPacific > 0: PacificHappiness = scorePacific * 10 / twCntPacific else: PacificHappiness = 0 if twCntMountain > 0: MountainHappiness = scoreMountain * 10 / twCntMountain else: MountainHappiness = 0 if twCntCentral > 0: CentralHappiness = scoreCentral * 10 / twCntCentral else: CentralHappiness = 0 if twCntEastern > 0: EasternHappiness = scoreEastern * 10 / twCntEastern else: PacificEastern = 0 print( "Happiness rating of Pacific timezone is %.3f out of 10 for %d tweets." % (round(PacificHappiness, 3), twCntPacific)) print( "Happiness rating of Mountain timezone is %.3f out of 10 for %d tweets." % (round(MountainHappiness, 3), twCntMountain)) print( "Happiness rating of Central timezone is %.3f out of 10 for %d tweets." % (round(CentralHappiness, 3), twCntCentral)) print( "Happiness rating of Eastern timezone is %.3f out of 10 for %d tweets." % (round(EasternHappiness, 3), twCntEastern)) # drawSimpleHistogram(eval,cval,mval,pval) happy_histogram.drawSimpleHistogram(EasternHappiness, CentralHappiness, MountainHappiness, PacificHappiness) fileTweets.close() fileKeywords.close()
def main(): #initialize variables used pacificScore =0 pacificTweets = 0 pacificKeywords = 0 mountainScore =0 mountainTweets = 0 mountainKeywords = 0 centralScore=0 centralTweets = 0 centralKeywords=0 easternScore=0 easternTweets = 0 easternKeywords = 0 total= 0 keywords={} #initialize dictionary to store keywords try: filename = input("enter keywords file") #checks for valid file infile = open(filename, 'r',encoding="utf-8") #can raise an IOError exception listOfKeywords = "" for line in infile: listOfKeywords = line.split(',') value = listOfKeywords[0] keywords[value] = listOfKeywords[1].strip() except IOError: print("error: file not found") exit() try: list = [] #list to temp store sections of the tweet regionList=[] #list to store longitude and latitude fileTweets = input("enter keywords file") infile = open(fileTweets, 'r',encoding="utf-8") #can raise an IOError exception for line in infile: list= line.split(']',1) #isolates longitude and latitude regionList = list[0].split(',') #splits tweet by comma regionList[0]= float(regionList[0].strip('[')) #strips bracket regionList[1] = float(regionList[1]) tweet = list[1].split(' ',4) #isolates tweet tweet = tweet[4] #takes text portion region = findRegion(regionList) total += 1 if region == 'pacific': #checks for pacific region listOfWords = tweet.split() #splits text into list found = False for word in listOfWords: word = word.strip('@#$%^&*') #removes extra characters word = word.lower() if word in keywords: pacificScore = pacificScore + int(keywords[word]) #adds to total sentiment values pacificKeywords = pacificKeywords +1 #Adds total keywords from this region found = True if found: pacificTweets = pacificTweets + 1 #if a keyword is found add to total tweets elif region == 'mountain': #checks for mountain region listOfWords = tweet.split() found = False for word in listOfWords: word = word.strip('@#$%^&*') word = word.lower() if word in keywords: mountainScore = mountainScore + int(keywords[word]) mountainKeywords = mountainKeywords +1 found =True if found: mountainTweets = mountainTweets +1 elif region == 'central': #checks for central region listOfWords = tweet.split() found = False for word in listOfWords: word = word.strip('@#$%^&*') word = word.lower() if word in keywords: centralScore = centralScore + int(keywords[word]) centralKeywords = centralKeywords +1 found = True if found: centralTweets = centralTweets +1 elif region == 'eastern': #checks for easter region listOfWords = tweet.split() found = False for word in listOfWords: word = word.strip('@#$%^&*') word = word.lower() if word in keywords: easternScore = easternScore + int(keywords[word]) easternKeywords = easternKeywords +1 found =True if found: easternTweets = easternTweets +1 #calculates total hapiness score and tweets for each region pacificHappy = round(pacificScore/pacificKeywords, 3) mountainHappy = round(mountainScore/mountainKeywords,3) centralHappy = round(centralScore/centralKeywords,3) easternHappy = round(easternScore/easternKeywords,3) print("The happiness score for the Pacific region is", pacificHappy, "with", pacificTweets, "tweets!") print("The happiness score for the Mountain region is", mountainHappy, "with", mountainTweets, "tweets!") print("The happiness score for the Central region is", centralHappy, "with", centralTweets, "tweets!") print("The happiness score for the Eastern region is", easternHappy, "with", easternTweets, "tweets!") drawSimpleHistogram(easternHappy, centralHappy, mountainHappy, pacificHappy) except IOError: #exception error print("error: file not found") exit()
def main(): #call function to create dictionary of keywords keyFile() #exception to see if file input is valid try: #input file name and open file fileInput=input("Enter a file name") file=open(fileInput,"r",encoding="utf-8") #while loop is true keep reading lines flag=True while flag==True: total=0 #read the line and split it into array line=file.readline() data=line.split() #if reach end of file set flag to false else keep looping through each line if line=="": flag=False else: #print(data) matches=0 for i in range (len(data)): lowerWord=data[i].lower() lowerWord=lowerWord.strip(".,!?#:;'") #print(lowerWord) if lowerWord in keywords: #print("sentiment for word",data[i],keywords[lowerWord]) total=total+keywords[lowerWord] matches=matches+1 #if the number of words matched is greater than 0 calculate sentiment and location using long and lat and add to specific array if matches>0: #print("number of matches",matches) avg=total/matches #print(total,avg) sentimentTotal.append(avg) lat=float(data[0].strip("[],")) long=float(data[1].strip("[],")) #print(lat) #print(long) if (lat <= 49.189787 and lat >= 24.6606845 and long>=-87.518395 and long <=-67.444574): #print("tweet is in eastern time zone") eastern.append(avg) elif (lat <=49.189787 and lat >= 24.6606845 and long >-101.998892 and long <-87.518395): #print("tweet is in central time zone") central.append(avg) elif (lat<= 49.189787 and lat >= 24.6606845 and long>-115.236428 and long <=-101.998892): #print("tweet is in Mountain time zone") mountain.append(avg) elif (lat <=49.189787 and lat >=24.6606845 and long>=-125.242264 and long<=-115.236428): #print("tweet is in Pacific time zone") pacific.append(avg) #if tweet has no matching words then do nothing else: pass eHappiness=sum(eastern)/len(eastern) cHappiness=sum(central)/len(central) mHappiness=sum(central)/len(central) pHappiness=sum(central)/len(central) #final output, output the happiness score and total number of tweets for each location print("The Happiness score for each Eastern is:", round(sum(eastern)/len(eastern),2)) print("The Happiness score for Central is:", round(sum(central)/len(central),2)) print("The Happiness score for Mountain is:", round(sum(mountain)/len(mountain),2)) print("The Happiness score for Pacific is:", round(sum(pacific)/len(pacific),2)) print("\n\n") print("The total Eastern Tweets are", len(eastern )) print("The total Central Tweets are",len(central)) print("The total Mountain Tweets are",len(mountain)) print("The total Pacific Tweets are", len(pacific)) print("\n\n") # print("The total Happiness score for Eastern is:",round(sum(eastern),2)) # print("The total Happiness score for Central is: ", round(sum(central),2)) # print("The total Happiness score for Mountain is:", round(sum(mountain),2)) # print("The total Happiness score for Pacific is:", round(sum(pacific),2)) #draw a histogram of the happiness value of each timezone drawSimpleHistogram(eHappiness,cHappiness,mHappiness,pHappiness) file.close() #exceptions for handling invalid file name and zero divison except IOError: print("Invalid file name") except ZeroDivisionError: print("Divided by zero")
currentNum = currentNum + 1 # #final happiness score calculation pScore = sum(pacificScore) / len(pacificScore) mScore = sum(mountainScore)/ len(mountainScore) cScore = sum(centralScore) / len(centralScore) eScore = sum(easternScore) / len(easternScore) pScore = round(pScore,1) mScore = round(mScore,1) cScore = round(cScore,1) eScore = round(eScore,1) #print statements(happiness score for each timezone and number of tweets.txt in that time zone) print(' ') print('The happiness score for Pacific time zone: ',pScore) print('Total number of Tweets in Pacific time zone: ', len(pacificScore)) print('') print('The happiness score for Mountain time zone: ', mScore) print('The total number of Tweets in Mountain time zone: ', len(mountainScore)) print(' ') print('The happiness score for Central time zone: ',cScore) print('Total number of Tweets in Central time zone: ', len(centralScore)) print(' ') print('The happiness score for Eastern time zone: ', eScore,) print('Total number of Tweets in Eastern time zone: ', len(easternScore)) happy_histogram.drawSimpleHistogram(pScore, mScore, cScore, eScore)
numberOfTweetsInCentral = len(happyscoreForATweetListCentralList) numberOfTweetsInMountain = len(happyscoreForATweetListMountainList) numberOfTweetsInPacific = len(happyscoreForATweetListPacificList) happyScoreEastern = sum( happyscoreForATweetListEasternList) / numberOfTweetsInEastern happyScoreCentral = sum( happyscoreForATweetListCentralList) / numberOfTweetsInCentral happyScoreMountain = sum( happyscoreForATweetListMountainList) / numberOfTweetsInMountain happyScorePacific = sum( happyscoreForATweetListPacificList) / numberOfTweetsInPacific print('The hapiness score for Eastern timesone is', happyScoreEastern) print('The hapiness score for Central timesone is', happyScoreCentral) print('The hapiness score for Mountain timesone is', happyScoreMountain) print('The hapiness score for Pacific timesone is', happyScorePacific) print('The number of tweets found in Eastern timezone is', numberOfTweetsInEastern) print('The number of tweets found in Central timezone is', numberOfTweetsInCentral) print('The number of tweets found in Mountain timezone is', numberOfTweetsInMountain) print('The number of tweets found in Pacific timezone is', numberOfTweetsInPacific) keywordsInfile.close() from happy_histogram import drawSimpleHistogram drawSimpleHistogram(happyScoreEastern, happyScoreCentral, happyScoreMountain, happyScorePacific)
except ZeroDivisionError: centralTweets = 0 centralScore = 0 try: easternScore = sum(easternScore) / easternTweets except ZeroDivisionError: easternTweets = 0 easternScore = 0 # Final print statements containing the happiness scores and number of tweets found in each timezone. print("The happiness score for the Pacific timezone is ", pacificScore, ", from", pacificTweets, "number of tweets!") print("The happiness score for the Mountain timezone is ", mountainScore, ", from", mountainTweets, "number of tweets!") print("The happiness score for the Central timezone is ", centralScore, ", from", centralTweets, "number of tweets!") print("The happiness score for the Eastern timezone is ", easternScore, ", from", easternTweets, "number of tweets!") # Graphics associated with the resulting scores from the program from happy_histogram import drawSimpleHistogram drawSimpleHistogram(eval=easternScore, cval=centralScore, mval=mountainScore, pval=pacificScore) # Exits the program if the file input is not found except IOError: print("Error: file not found.") exit()
def graphicsFun(east, cent, mount, paci): import happy_histogram as hp hp.drawSimpleHistogram(east, cent, mount, paci)