def analyzeNewTweets(query, N):
	#normalize input, query should be short, N should not be larger
	#than 500. ?not implemented?

	#collect no more than N data
	tweets = searchAndExtractTweets(query, N)
	print "extracted " + str(len(tweets)) + " tweets"
	# text summarization
	summerizer = NaiveSummarizer()
	textdoc = ""
	for tweet in tweets:
		textdoc += (tweet["text"] + ". ")
	summ = summerizer.summarize(textdoc, 4)
	print "summarize: " + summ
	# sentiment analysis
	payloaddata = json.dumps({'language': 'en', 'data': tweets})
	req = urllib2.Request(url="http://www.sentiment140.com/api/[email protected]", data = payloaddata)
	res = urllib2.urlopen(req)
	# results into json
	res = res.read().decode("UTF-8", 'ignore')
	resdict = json.loads(res)
	negList = []
	neuList = []
	posList = []
	for sent in resdict["data"]:
		if sent["polarity"] == 0:
			negList.append(sent["text"])
		if sent["polarity"] == 4:
			posList.append(sent["text"])
		if sent["polarity"] == 2:
			neuList.append(sent["text"])
	return {"positive": posList, "neutral": neuList, "negative": negList, "summary": summ}
def analyzeNewFBPosts(topic, number, token):
	payloaddata = retriveData(topic, number, token)
	# summary
	summerizer = NaiveSummarizer()
	textdoc = ""
	for data in payloaddata:
		textdoc += (data + ". ")
	summ = summerizer.summarize(textdoc, 4)
	print "summarize: " + summ
	# result into json
	posList = []
	neuList = []
	negList = []
	index = 0
	for post in payloaddata:
		res = getSentiment(post)
		if res is not "none":
			if res == "positive":
				posList.append(post)
			if res == "negative":
				negList.append(post)
			# if res == "neutral":
			# 	neuList.append(post)
		index += 1
		print str(index) + "/" + str(len(payloaddata))
	# print json.dumps(results, indent = 4)
	return {"positive": posList, "negative": negList, "neutral": neuList, "summary": summ}

# analyzeNewPosts('AI', 'CAACEdEose0cBAJPsfkUp71nxbZAFfCjgE1VPZAN3h5vE68T5PD9Ju00d8eedNqKaooZAVCU7AbYJYZAWNJ1sWfXO2WOMFHu4qxrS75GIUattFwZB8x0g4yoqzzZBxZCSqcyQnRUz5IrB3B9gKyvQfSf1UHKZBGNgcNFikjg3OhNK5pjYdM1ZALmu0Y6yuc5iCXXObtHEJkNaXfwZDZD')