Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 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(keywordFileInput, tweetFileInput):
  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.         for line in tweetFile:
  44.             tweet = 0     # initializing count and happyValue variables
  45.             happyValue = 0
  46.             line = line.lower()     # lowers and splits each line
  47.             line = line.split()
  48.             line[0] = float(line[0].strip("[").strip(","))     # strips the [ and , in the first part of the coordinate (latitude in this case)
  49.             line[1] = float(line[1].strip("]"))                # strips the ] at the end of the longitude
  50.             word = line[5:]    # from element 5 and beyond, the tweet begins
  51.             for i in word:
  52.                 i = i.strip(string.punctuation)
  53.                 for word in keywordDict:
  54.                     tweet = tweet + 1
  55.                     happyValue = happyValue + keywordDict[word]
  56.                 if tweet == 0:
  57.                     continue
  58.                 tweetTotal = happyValue / tweet      # average sentiment value
  59.  
  60.  
  61.         eastTweet = centralTweet = mountainTweet = pacificTweet = 0
  62.         eastTotal = centralTotal = mountainTotal = pacificTotal = 0
  63.         if latMin <= float(line[0]) <= latMax and happyValue != 0:
  64.             if easternLow < float(line[1]) <= easternHigh:
  65.                 eastTweet = eastTweet + 1
  66.                 eastTotal = eastTotal + tweetTotal
  67.                 averageHappyValueEast = eastTotal/eastTweet
  68.                 Eastern = (averageHappyValueEast, eastTweet, eastTotal)
  69.  
  70.             if centralLow < float(line[1]) <= centralHigh:
  71.                 centralTweet = centralTweet + 1
  72.                 centralTotal = centralTotal + tweetTotal
  73.                 averageHappyValueCent = centralTotal/centralTweet
  74.                 Central = (averageHappyValueCent, centralTweet, centralTotal)
  75.  
  76.             if mountainLow < float(line[1]) <= mountainHigh:
  77.                 mountainTweet = mountainTweet + 1
  78.                 mountainTotal = mountainTotal + tweetTotal
  79.                 averageHappyValueMount = mountainTotal/mountainTweet
  80.                 Mountain = (averageHappyValueMount, mountainTweet, mountainTotal)
  81.  
  82.             if pacificLow < float(line[1]) <= pacificHigh:
  83.                 pacificTweet = pacificTweet + 1
  84.                 pacificTotal = pacificTotal + tweetTotal
  85.                 averageHappyValuePac = pacificTotal/pacificTweet
  86.                 Pacific = (averageHappyValuePac, pacificTweet, pacificTotal)
  87.         tweetFile.close()
  88.  
  89.         tuplesList.append(Eastern)
  90.         tuplesList.append(Central)
  91.         tuplesList.append(Mountain)
  92.         tuplesList.append(Pacific)
  93.  
  94.         return tuplesList
  95.  
  96.     except IOError:
  97.         exit("An error occurred! File not found! ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement