Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import string
  2.  
  3. p1 = (49.189787, -67.444574)
  4. p2 = (24.660845, -67.444574)
  5. p3 = (49.189787, -87.518395)
  6. p4 = (24.660845, -87.518395)
  7. p5 = (49.189787, -101.998892)
  8. p6 = (24.660845, -101.998892)
  9. p7 = (49.189787, -115.236428)
  10. p8 = (24.660845, -115.236428)
  11. p9 = (49.189787, -125.242264)
  12. p10 = (24.660845, -125.242264)
  13.  
  14. latMax = 49.189787
  15. latMin = 24.660845
  16.  
  17. easternLow = -87.518395
  18. easternHigh = -67.444574
  19. centralLow = -101.998892
  20. centralHigh = -87.518395
  21. mountainLow = -115.236428
  22. mountainHigh = -101.998892
  23. pacificLow = -125.242264
  24. pacificHigh = -115.236428
  25.  
  26.  
  27. keywordDict = {}
  28. tuplesList = []
  29.  
  30. def compute_tweets(tweetFileInput, keywordFileInput):
  31.  
  32.     try:
  33.         keywordFile = open(keywordFileInput, "r", encoding="utf-8")     # opens keywordFile if it exists
  34.         for w in keywordFile:
  35.             key, value = w.split(",")     # splits the words in the file
  36.             keywordDict[key] = int(value)    # each word is assigned a value, ex: good = 7
  37.         keywordFile.close()     # closes the file
  38.     except IOError:     # when the file does not exists, this statement will run
  39.         exit("An error occurred! File not found! ")
  40.  
  41.     try:
  42.         tweetFile = open(tweetFileInput, "r", encoding="utf-8")     # opens tweetFile if it exists
  43.  
  44.         eastTweet = centralTweet = mountainTweet = pacificTweet = 0
  45.         eastTotal = centralTotal = mountainTotal = pacificTotal = 0
  46.  
  47.         for line in tweetFile:
  48.             tweet = 0     # initializing count and happyValue variables
  49.             happyValue = 0
  50.             line = line.lower()     # lowers and splits each line
  51.             line = line.split()
  52.             line[0] = float(line[0].strip("[").strip(","))     # strips the [ and , in the first part of the coordinate (latitude in this case)
  53.             line[1] = float(line[1].strip("]"))                # strips the ] at the end of the longitude
  54.             word = line[5:]    # from element 5 and beyond, the tweet begins
  55.             for i in word:
  56.                 i = i.strip(string.punctuation)
  57.                 for word in keywordDict:
  58.                     tweet = tweet + 1
  59.                     happyValue = happyValue + keywordDict[word]
  60.                 if tweet == 0:
  61.                     continue
  62.                 tweetTotal = happyValue / tweet      # average sentiment value
  63.  
  64.  
  65.         if latMin <= float(line[0]) <= latMax and happyValue != 0:
  66.             if easternLow < float(line[1]) <= easternHigh:
  67.                 eastTweet = eastTweet + 1
  68.                 eastTotal = eastTotal + tweetTotal
  69.  
  70.             elif centralLow < float(line[1]) <= centralHigh:
  71.                 centralTweet = centralTweet + 1
  72.                 centralTotal = centralTotal + tweetTotal
  73.  
  74.             elif mountainLow < float(line[1]) <= mountainHigh:
  75.                 mountainTweet = mountainTweet + 1
  76.                 mountainTotal = mountainTotal + tweetTotal
  77.  
  78.             elif pacificLow < float(line[1]) <= pacificHigh:
  79.                 pacificTweet = pacificTweet + 1
  80.                 pacificTotal = pacificTotal + tweetTotal
  81.  
  82.         tweetFile.close()       # end of file, close loop
  83.  
  84.         averageHappyValueEast = eastTotal/eastTweet
  85.         averageHappyValueCent = centralTotal/centralTweet
  86.         averageHappyValueMount = mountainTotal/mountainTweet
  87.         averageHappyValuePac = pacificTotal/pacificTweet
  88.  
  89.         Eastern = (averageHappyValueEast, eastTweet, eastTotal)
  90.         Central = (averageHappyValueCent, centralTweet, centralTotal)
  91.         Mountain = (averageHappyValueMount, mountainTweet, mountainTotal)
  92.         Pacific = (averageHappyValuePac, pacificTweet, pacificTotal)
  93.  
  94.         tuplesList.append(Eastern)
  95.         tuplesList.append(Central)
  96.         tuplesList.append(Mountain)
  97.         tuplesList.append(Pacific)
  98.  
  99.         return tuplesList
  100.  
  101.     except IOError:
  102.         exit("An error occurred! File not found! ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement