Intent
stringlengths
4
208
Script
stringlengths
4
171
Create an empty list and assign it to interest with key 'links'
interests['links'] = []
Create an empty list and assign it to interest with key 'users'
interests['users'] = []
Create an empty list and assign it to interest with key 'hashtags'
interests['hashtags'] = []
Assign to the text the value of tweet with jey tweet
text = tweet['tweet']
Assign to links the result of the function compile of re with argument '(http.*?)\Z|(http.*?) ' then call the function findall with argument text
links = re.compile().findall(text)
For each link in links
for link in links:
If link at position 0 exists
if link[0]:
Assign to link the link at the position 0
link = link[0]
Else if link at position 1 exists
elif link[1]:
Assign to link the link at the position 1
link = link[1]
Else
else:
Continue
continue
Assign to response the result of the method urlopen of urllib2 with argument link
response = urllib2.urlopen(link)
Assign to full_link the variable url of response
full_link = response.url
Append to interests with key links the variable full_link
interests['links'].append(full_link)
Except
except:
Append to interests with key 'users' the result of the function findall with argument text called on compile of re with argument '(@\w+)'
interests['users'] += re.compile('(@\w+)').findall(text)
Append to interests with key 'hashtags' the result of the function findall with argument text called on compile of re with argument '(#\w+)'
interests['hashtags'] += re.compile('(#\w+)').findall(text)
Sort the list interests with key 'users'
interests['users'].sort()
Sort the list interests with key 'hashtags'
interests['hashtags'].sort()
Sort the list interests with key 'links'
interests['links'].sort()
Return the interests
return interests
Define the function twitter_locate with arguments self and cityFile
def twitter_locate(self, cityFile):
Assign an empty list to cities
cities = []
If cityFile is not equal to None
if cityFile != None:
For each line in the method readlines called with no arguments of the opened file cityFile
for line in open(cityFile).readlines():
Assign to city the string stripped of '\n' and '\r' in low case
city = line.strip('\\n').strip('\r').lower()
Append to the list cities the city
cities.append(city)
Assign to locations an empty list
locations = []
Assign to locCnt the value 0
locCnt = 0
Assign to cityCnt the value 0
cityCnt = 0
Assign to tweetsText an empty string
tweetsText = ''
For each tweet in tweets of self
for tweet in self.tweets:
If tweet with key 'geo' is not equal to None
if tweet['geo'] != None:
Append to locations the tweet with key 'geo'
locations.append(tweet['geo'])
Increment locCnt of 1
locCnt += 1
Concatenate to tweetsText the tweet with key 'tweet' in lower case
tweetsText += tweet['tweet'].lower()
For each city in cities
for city in cities:
If the substring city is in tweetsText
if city in tweetsText:
Append to locations the value city
locations.append(city)
Increment cityCnt of 1
cityCnt += 1
Return the value locations
return locations
Call the method quote_plus of urllib with argument the concatenation of 'from:', handle and ' since:2009-01-01 include:retweets'
query = urllib.quote_plus('from:' + handle+ ' since:2009-01-01 include:retweets')
Assign to response the result of the the method open of browser with argument the concatenation of 'http://search.twitter.com/', 'search.json?q=' and query
response = browser.open('http://search.twitter.com/'+ 'search.json?q='+ query)
Define the function load_cities with argument cityFile
def load_cities(cityFile):
Assign an empty list to cities
cities = []
For each line in the method readlines with no argument of the opened file cityFile
for line in open(cityFile).readlines():
Assign the line stripped of '\n' and '\r' in lower case to city
city=line.strip('\\n').strip('\r').lower()
Append city to cities
cities.append(city)
Return the variable cities
return cities
Define the function twitter_locate with arguments tweets and cities
def twitter_locate(tweets,cities):
Assign an empty list to locations
locations = []
Assign 0 to locCnt
locCnt = 0
Assign 0 to cityCnt
cityCnt = 0
Assign to tweetsText an empty string
tweetsText = ""
If tweet with key 'geo' is not equal to None
if tweet['geo'] != None:
Append the tweet with key 'geo' to locations
locations.append(tweet['geo'])
Increment locCnt of 1
locCnt += 1
Concatenate the tweet with key 'tweet' to tweetsText in lower case
tweetsText += tweet['tweet'].lower()
For each city in cities
for city in cities:
If the substring city is in tweetsText
if city in tweetsText:
Append the city to locations
locations.append(city)
Increment cityCnt of 1
cityCnt+=1
Return the variable locations
return locations
Define the function main with no arguments
def main():
Assign to parser the OptionParser with argument the concatenation of 'usage %prog ' and '-u <twitter handle> [-c <list of cities>]' of optparse
parser = optparse.OptionParser('usage %prog '+ '-u <twitter handle> [-c <list of cities>]')
Call the method add_option with arguments '-c', dest equals to 'cityFile', type equals to 'string' and help= equals to 'specify file containing cities to search'
parser.add_option('-c', dest='cityFile', type='string',help='specify file containing cities to search')
Assign cityFile of options to cityFile
cityFile = options.cityFile
If handle is equal to None
if (handle==None):
Call the function exit to 0
exit(0)
Assign an empty list to to cities
cities = []
If cityFile is not equal to None
if (cityFile!=None):
Call the function load_cities with argument cityFile and assign the result to cities
cities = load_cities(cityFile)
Assign the result of the function twitter_locate with argument tweets and cities to location
locations = twitter_locate(tweets,cities)
Import the library dedicated to regular expressions
import re
Import the second version of the http client
import urllib2
Define the function get_tweets with argument handle
def get_tweets(handle):
Call the method quote_plus of urllib with argument the concatenation of 'from:', handle and ' since:2009-01-01 include:retweets'
query = urllib.quote_plus('from:' + handle+ ' since:2009-01-01 include:retweets')
Assign to tweets an empty list
tweets = []
Assign to browser the class anonBrowser with no argument
browser = anonBrowser()
Call the method of browser anonymize with no arguments
browser.anonymize()
Assign to response the result of the the method open of browser with argument the concatenation of 'http://search.twitter.com/', 'search.json?q=' and query
response = browser.open('http://search.twitter.com/'+ 'search.json?q=' + query)
Assign to json_objects the result of the method load of json with argument response
json_objects = json.load(response)
For each result in json_objects with key 'result'
for result in json_objects['results']:
Assign to new_result an empty dictionary
new_result = {}
Assign to new_result with key 'from_user' the variable result with key 'from_user_name'
new_result['from_user'] = result['from_user_name']
Assign to new_result with key 'geo' the variable result with key 'geo'
new_result['geo'] = result['geo']
Assign to new_result with key 'tweet' the variable result with key 'text'
new_result['tweet'] = result['text']
Append to tweets the new_result
tweets.append(new_result)
Return the variable tweets
return tweets
Define the function find_interests with argument tweets
def find_interests(tweets):
Assign to interests an empty dictionary
interests = {}
Assign to interests with key 'links' an empty list
interests['links'] = []
Assign to interests with key 'users' an empty list
interests['users'] = []
Assign to interests with key 'hashtags' an empty list
interests['hashtags'] = []
For each tweet in tweets
for tweet in tweets:
Assign the tweet with key 'tweet' to text
text = tweet['tweet']
Call the method findall with argument text of the function compile with argument '(http.*?)\Z|(http.*?) ' of the library re and assign it to links
links = re.compile().findall(text)
For each link in links
for link in links:
If link in position 0 exists
if link[0]: