Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from TwitterAPI import TwitterAPI
- import re
- import urllib, json
- import math
- ## returns 1 if resp shows
- ## the work is polysyllabic
- def get_resp(url,word):
- resp = urllib.urlopen(url)
- data = json.loads(resp.read())
- if(len(data) > 1):
- print word+' is polysyllabic'
- return 1
- return 0
- #
- # TWITTER
- #
- # load API credentials
- access_key = '4910286850-syevdK8ekAwO3gn9RQNhErNKsOZBogESIbLriIr'
- access_secret = 'HWFSPU6KJTQ5XH3jac38bXe9mLz8JHU6sc5UuJothu4dZ'
- consumer_key = 'pB5ceckuQE0g9MdJ8U0I0eGnS'
- consumer_secret = 'gwapza3xUxLZUjmV4djQCZeHfy6kjKOke6xzeumOF6CzMLIHJG'
- username='BarackObama'
- num_sentences = 30
- # perform api request
- api = TwitterAPI(consumer_key, consumer_secret, access_key, access_secret)
- twitter_resp = api.request('statuses/user_timeline', {'screen_name':username,'count':num_sentences})
- # get tweets
- tweets = [item['text'] for item in twitter_resp]
- #
- # WORDNIK
- #
- print ''
- words = [re.findall(r'\w+',tweet) for tweet in tweets]
- api_key = 'a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5'
- url_base = 'http://api.wordnik.com/v4/word.json/'
- url_param = '/hyphenation?useCanonical=false&limit=50&api_key='
- wordnik_resp = [[get_resp(url_base+word+url_param+api_key,word) for word in word_arr] for word_arr in words]
- # count total polysyllables
- count_per_tweet = [sum(is_poly) for is_poly in wordnik_resp]
- num_poly = sum(count_per_tweet)
- # calculate and print grade
- grade = 1.0430*math.sqrt(num_poly*(30/num_sentences))+3.1291
- print ''
- print 'GRADE: ' + str(grade)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement