Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Python 3.5
- import tweepy
- from tweepy import OAuthHandler
- from collections import *
- from nltk.tokenize import word_tokenize
- from nltk.corpus import stopwords
- import nltk
- import string
- import numpy as np
- from flask import Flask, json, jsonify, request, make_response
- # from flask_restplus import Api, Resource, fields
- from flask_restful import Resource, Api, reqparse
- ckey = '3yI33cFzg0V5DBSTP35oSDtJE'
- csecret = 'nsz1YmNo431dTJAo99LhuqWiqgfs99f3eXRigN2G59cxocb6t3'
- atoken = '1000946197790683137-nLPGfJXdyI5OnQf18Bl6HyGgpIsDHE'
- asecret = 'elMUbkeKnouymctjlwhwNRlbud9Ygixpwmr7KWxz6Qgsb'
- simple_pos = 'ADV'
- num_of_tweets = 12
- num_most_common = 12
- auth = OAuthHandler(ckey, csecret)
- auth.set_access_token(atoken, asecret)
- apii = tweepy.API(auth)
- stop_words = set(stopwords.words("english"))
- newStopWords = ('RT', '...','https', 'amp', '&', '@')
- stop_words.update(newStopWords)
- partsos = 'ADV', 'ADP', 'ADJ', 'DET', 'NOUN', 'PRON', 'VERB'
- handle_input ='realDonaldTrump'
- def tweet_list(twitname):
- tweet_list = apii.user_timeline(twitname, count=num_of_tweets, tweet_mode="extended")
- tweet_pos_dict = {}
- big_tweet = ""
- for tweet in tweet_list:
- if not tweet.retweeted and ('RT @' not in tweet.full_text):
- big_tweet = big_tweet + str(tweet.full_text.translate(string.punctuation).encode('ascii', 'ignore').decode("utf-8")) + " "
- return big_tweet
- def counter_total(tweet_string):
- tweet_string = tweet_string.split()
- total_words = Counter()
- c = Counter() #initialize coutner object
- for word in tweet_string:
- filtered_sentence = []
- if word not in stop_words:
- filtered_sentence.append(word.encode('ascii', 'ignore').decode("utf-8")) #create new sentence with stopwords removed
- c = Counter(filtered_sentence) #creates counter object from new sentence
- total_words = Counter(c) + total_words #running total counter object
- total_words = total_words.most_common(30)
- x_list = []
- y_list = []
- for word in total_words:
- x_list.append(word[0])
- y_list.append(word[1])
- return x_list, y_list
- def simple_pos_most_common(tweet_string, simple_pos):
- co = Counter()
- text = word_tokenize(tweet_string)
- taggedTweets = nltk.pos_tag(text, tagset='universal')
- compiledWords =''
- c = Counter
- output_list = []
- i = 0
- for i in range(0,len(taggedTweets)): #for i in rnage (index 0 through length of posTagged)
- if taggedTweets[i][1] == simple_pos and taggedTweets[i][0] not in stop_words: #if at current loops position i, look at index 1 in touple (word, NLTKTAG <-)
- output_list.append((taggedTweets[i][0]))
- c.update(taggedTweets[i][0])
- i += 1
- for word in output_list:
- co[word] += 1
- return co.most_common(num_most_common)
- def plot_stuff(sorted_dict, pos):
- x_list = []
- y_list = []
- list_30_common = sorted_dict[pos]
- for i in range(0,len(list_30_common)):
- x = list_30_common[i][0]
- x_list.append(x)
- y = list_30_common[i][1]
- y_list.append(y)
- return x_list, y_list
- def sort_common_by_pos(tweet_string):
- tweet_pos_dict = {}
- for word in partsos:
- a = simple_pos_most_common(tweet_string, word)
- for words in a:
- if words not in stop_words and "//t" not in words:
- tweet_pos_dict[word] = a
- return tweet_pos_dict
- def update_pos_output(pos, usr):
- tweet_string = tweet_list(usr.encode("ascii", "ignore"))
- pos_sorted_words = sort_common_by_pos(tweet_string)
- pos_x, pos_y = plot_stuff(pos_sorted_words, pos)
- return pos_x, pos_y
- def update_total_output(usr):
- tweet_string = tweet_list(usr)
- total_x, total_y = counter_total(tweet_string)
- return total_x, total_y
- def getPos(pos,tHandle):
- data_dict[pos] = update_pos_output(pos, tHandle)
- return(data_dict[pos])
- app = Flask(__name__)
- api = Api(app)
- noun = 'NOUN'
- data_dict = {}
- class pos(Resource):
- def post(self):
- parser = reqparse.RequestParser()
- parser.add_argument('handle')
- parser.add_argument('pos')
- args = parser.parse_args()
- toJson=[]
- toJson = [args]
- return json.dumps(toJson), 201
- api.add_resource(pos, '/')
- if __name__ == '__main__':
- app.run(debug=True)
- # def get(self, tPos):
- # jsonList = []
- # jsonList.append(handle_input)
- # jsonList.append(getPos(tPos, handle_input))
- # return jsonify(json.dumps(jsonList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement