Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import json
  2. from watson_developer_cloud import NaturalLanguageUnderstandingV1
  3. import watson_developer_cloud.natural_language_understanding.features.v1 as Features
  4. import tweepy
  5.  
  6. # Consumer keys and access tokens, used for OAuth
  7. consumer_key = 'OBOtvJVP5CeRjleSvEntfQ6xe'
  8. consumer_secret = '94o8G3PLKsqDsMaM6aUi3CRHGNvTVM07U1634PfqPMlm3Ncmnj'
  9. access_token = '926950006640300032-jpW6aUO2vWrHop7YbMJWZNnkX19gS3O'
  10. access_token_secret = 'wQgGUK20TGWeb26Z2waXaw28fucWxrvCIUZlrtjd7RR4Z'
  11.  
  12. # OAuth process, using the keys and tokens
  13. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  14. auth.set_access_token(access_token, access_token_secret)
  15.  
  16. # Creation of the actual interface, using authentication
  17. api = tweepy.API(auth)
  18.  
  19. # Sample method, used to update a status
  20. #api.update_status('Hello Python Central!')
  21. newYorkWOEID = 2459115
  22. stateCollegeWOEID = 12764454
  23. availablePlaces = api.trends_available()
  24.  
  25. #trendList = api.trends_place(newYorkWOEID, "#")
  26. # print(trendList[0].keys())
  27. # print(trendList[0]["trends"][0])
  28.  
  29. # for element in trendList[0]["trends"]:
  30. # print(element["name"])
  31.  
  32. #Retreive all of the locations in the United States which have trends
  33. USavailablePlacesWOEID = []
  34. USavailablePlacesCity = []
  35. #top 11 cities by population in United States - may produce bias
  36. topCities = ["New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose", "Boston" ]
  37.  
  38. #This works as is - creates parrallel arrays that hold the cities and corresponding woeids
  39. # for i in range(0, len(availablePlaces)):
  40. # availPlace = availablePlaces[i]
  41. # if(availPlace["country"] == "United States"):
  42. # USavailablePlacesWOEID.append(availPlace["woeid"])
  43. # USavailablePlacesCity.append(availPlace["name"])
  44. # print(USavailablePlacesWOEID)
  45. # print(USavailablePlacesCity)
  46.  
  47. #listOfNamesOfTrends = []
  48. #this loop should compile the names of all the trends - but throws a "Rate limit exceeded" - this is the ideal case, compiling all trends
  49. # for woeid in USavailablePlacesWOEID:
  50. # trendList = api.trends_place(woeid)
  51. # for element in trendList[0]["trends"]:
  52. # listOfNamesOfTrends.append(element["name"])
  53. # print(listOfNamesOfTrends)
  54.  
  55. compiledTrendList = []
  56. smallWOEID = []
  57. for i in range(1, len(availablePlaces)):
  58. availPlace = availablePlaces[i]
  59. if(availPlace["name"] in topCities):
  60. smallWOEID.append(availPlace["woeid"])
  61. for woeid in smallWOEID:
  62. trendList = api.trends_place(woeid)
  63. for element in trendList[0]["trends"]:
  64. compiledTrendList.append(element["name"])
  65.  
  66. #not necessary to print - just to verify if it looks right
  67. for i in compiledTrendList:
  68. print(i)
  69.  
  70. #converting list to space separated string for Watson:
  71. trendString = " ".join(str(x) for x in compiledTrendList)
  72. print(trendString)
  73.  
  74. natural_language_understanding = NaturalLanguageUnderstandingV1(
  75. version='2017-02-27',
  76. username='2235dc52-736c-49b1-b52a-848550af0dca',
  77. password='kZ7LruQOOHUI')
  78.  
  79. response = natural_language_understanding.analyze(
  80. text=trendString,
  81. features=[Features.Entities(), Features.Keywords()])
  82.  
  83. print(json.dumps(response, indent=2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement