示例#1
0


#Test 1: extract hashtags- takes a string and returns an array of hashtags
print 'TEST 1: Extract Hashtags'
actual = tweetLibrary.extract_hashtags('I like to #climb #every #day')
expected = ['#climb', '#every', '#day']
print actual == expected


#Test 2: JSON decoding: are tweets being properly converted to Python dictionaries?
#Check get_timestamp method
print 'TEST 2: JSON decoding and Getting Timestamps'
tweets = open('tweets.txt')
tweets = tweets.readlines()
tweet_1 =  tweetLibrary.decode_tweet(tweets[0])
tweet_2 =  tweetLibrary.decode_tweet(tweets[1])
tweet_3 =  tweetLibrary.decode_tweet(tweets[2])
tweet_4 =  tweetLibrary.decode_tweet(tweets[3])
print tweetLibrary.get_timestamp(tweet_1) == 'Thu Oct 29 17:51:50 +0000 2015'
print tweetLibrary.get_timestamp(tweet_2) == 'Thu Oct 29 17:51:51 +0000 2015'
print tweetLibrary.get_timestamp(tweet_3) == "Thu Oct 29 18:10:49 +0000 2015"
print tweetLibrary.get_timestamp(tweet_4) == "Thu Oct 29 18:10:49 +0000 2015"
print tweet_2["text"]
print tweet_1["text"]
print tweet_3["text"]
print tweet_4["text"]

#Test 3: extract hashtags
print 'TEST 3: Extracting Hashtags'
tweet_5 = tweetLibrary.get_tweet_text(tweetLibrary.decode_tweet(tweets[10]))
import json
import tweetLibrary


tweets = open('tweets.txt')
tweets = tweets.readlines()
tweet_timestamps = []
tweet_hashtags = []
output = open('tweet_output/ft2.txt', 'w') 

for i in range(0,len(tweets)):
	tweet = tweetLibrary.decode_tweet(tweets[i])
	tweet_timestamps.append(int(tweetLibrary.get_ms_timestamp(tweet)))
	tweet_hashtags.append(tweetLibrary.extract_hashtags(tweetLibrary.get_tweet_text(tweet)))

current_timestamps=[]
lowest_index = 0
for i in range(1,len(tweet_timestamps)):
	current_timestamps.append(tweet_timestamps[i]) 
	print i 
	#lowest_index is the lowest index of current_timestamps where the element at that index is
	#within 60 seconds of element i 
	lowest_index = tweetLibrary.compare_timestamp_list(current_timestamps)
	del current_timestamps[0:lowest_index]
	current_hashtags = tweet_hashtags[i-len(current_timestamps):i]
	output.write('Current average degree:')
	output.write(str(tweetLibrary.get_average_degree(tweetLibrary.get_hashtag_dictionary(current_hashtags))))
	output.write('\n')


output.close()