Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from TwitterAPI import TwitterAPI
  4. import re
  5. import urllib, json
  6. import math
  7.  
  8. ## returns 1 if resp shows
  9. ## the work is polysyllabic
  10. def get_resp(url,word):
  11.         resp = urllib.urlopen(url)
  12.         data = json.loads(resp.read())
  13.         if(len(data) > 1):
  14.                 print word+' is polysyllabic'
  15.                 return 1
  16.  
  17.         return 0
  18.  
  19. #
  20. #       TWITTER
  21. #
  22.  
  23. # load API credentials
  24. access_key = '4910286850-syevdK8ekAwO3gn9RQNhErNKsOZBogESIbLriIr'
  25. access_secret = 'HWFSPU6KJTQ5XH3jac38bXe9mLz8JHU6sc5UuJothu4dZ'
  26. consumer_key = 'pB5ceckuQE0g9MdJ8U0I0eGnS'
  27. consumer_secret = 'gwapza3xUxLZUjmV4djQCZeHfy6kjKOke6xzeumOF6CzMLIHJG'
  28.  
  29. username='BarackObama'
  30. num_sentences = 30
  31.  
  32. # perform api request
  33. api = TwitterAPI(consumer_key, consumer_secret, access_key, access_secret)
  34. twitter_resp = api.request('statuses/user_timeline', {'screen_name':username,'count':num_sentences})
  35.  
  36. # get tweets
  37. tweets = [item['text'] for item in twitter_resp]
  38.  
  39. #
  40. #       WORDNIK
  41. #
  42.  
  43. print ''
  44. words = [re.findall(r'\w+',tweet) for tweet in tweets]
  45. api_key = 'a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5'
  46. url_base = 'http://api.wordnik.com/v4/word.json/'
  47. url_param = '/hyphenation?useCanonical=false&limit=50&api_key='
  48. wordnik_resp = [[get_resp(url_base+word+url_param+api_key,word) for word in word_arr] for word_arr in words]
  49.  
  50. # count total polysyllables
  51. count_per_tweet = [sum(is_poly) for is_poly in wordnik_resp]
  52. num_poly = sum(count_per_tweet)
  53.  
  54. # calculate and print grade
  55. grade = 1.0430*math.sqrt(num_poly*(30/num_sentences))+3.1291
  56. print ''
  57. print 'GRADE: ' + str(grade)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement