Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- p1 = (49.189787, -67.444574)
- p2 = (24.660845, -67.444574)
- p3 = (49.189787, -87.518395)
- p4 = (24.660845, -87.518395)
- p5 = (49.189787, -101.998892)
- p6 = (24.660845, -101.998892)
- p7 = (49.189787, -115.236428)
- p8 = (24.660845, -115.236428)
- p9 = (49.189787, -125.242264)
- p10 = (24.660845, -125.242264)
- latMax = 49.189787
- latMin = 24.660845
- easternLow = -87.518395
- easternHigh = -67.444574
- centralLow = -101.998892
- centralHigh = -87.518395
- mountainLow = -115.236428
- mountainHigh = -101.998892
- pacificLow = -125.242264
- pacificHigh = -115.236428
- keywordDict = {}
- tuplesList = []
- def compute_tweets(keywordFileInput, tweetFileInput):
- try:
- keywordFile = open(keywordFileInput, "r", encoding="utf-8") # opens keywordFile if it exists
- for w in keywordFile:
- key, value = w.split(",") # splits the words in the file
- keywordDict[key] = int(value) # each word is assigned a value, ex: good = 7
- keywordFile.close() # closes the file
- except IOError: # when the file does not exists, this statement will run
- exit("An error occurred! File not found! ")
- try:
- tweetFile = open(tweetFileInput, "r", encoding="utf-8") # opens tweetFile if it exists
- for line in tweetFile:
- tweet = 0 # initializing count and happyValue variables
- happyValue = 0
- line = line.lower() # lowers and splits each line
- line = line.split()
- line[0] = float(line[0].strip("[").strip(",")) # strips the [ and , in the first part of the coordinate (latitude in this case)
- line[1] = float(line[1].strip("]")) # strips the ] at the end of the longitude
- word = line[5:] # from element 5 and beyond, the tweet begins
- for i in word:
- i = i.strip(string.punctuation)
- for word in keywordDict:
- tweet = tweet + 1
- happyValue = happyValue + keywordDict[word]
- if tweet == 0:
- continue
- tweetTotal = happyValue / tweet # average sentiment value
- eastTweet = centralTweet = mountainTweet = pacificTweet = 0
- eastTotal = centralTotal = mountainTotal = pacificTotal = 0
- if latMin <= float(line[0]) <= latMax and happyValue != 0:
- if easternLow < float(line[1]) <= easternHigh:
- eastTweet = eastTweet + 1
- eastTotal = eastTotal + tweetTotal
- averageHappyValueEast = eastTotal/eastTweet
- Eastern = (averageHappyValueEast, eastTweet, eastTotal)
- if centralLow < float(line[1]) <= centralHigh:
- centralTweet = centralTweet + 1
- centralTotal = centralTotal + tweetTotal
- averageHappyValueCent = centralTotal/centralTweet
- Central = (averageHappyValueCent, centralTweet, centralTotal)
- if mountainLow < float(line[1]) <= mountainHigh:
- mountainTweet = mountainTweet + 1
- mountainTotal = mountainTotal + tweetTotal
- averageHappyValueMount = mountainTotal/mountainTweet
- Mountain = (averageHappyValueMount, mountainTweet, mountainTotal)
- if pacificLow < float(line[1]) <= pacificHigh:
- pacificTweet = pacificTweet + 1
- pacificTotal = pacificTotal + tweetTotal
- averageHappyValuePac = pacificTotal/pacificTweet
- Pacific = (averageHappyValuePac, pacificTweet, pacificTotal)
- tweetFile.close()
- tuplesList.append(Eastern)
- tuplesList.append(Central)
- tuplesList.append(Mountain)
- tuplesList.append(Pacific)
- return tuplesList
- except IOError:
- exit("An error occurred! File not found! ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement