-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterSearch.py
More file actions
48 lines (40 loc) · 1.58 KB
/
Copy pathtwitterSearch.py
File metadata and controls
48 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
sys.path.insert(0,"twitter") #add to python's module search path
import twitter
from unicodeFunctions import *
from featureFunctions import *
def twitterSearch(query, classifier):
#get first 100 tweets
#twitter.api.search.tweets() returns a dictionary
listOfTweets = twitter.api.search.tweets(q = query,lang="en",count=100)["statuses"]
print "Okay... loading...\n"
numPos=0
numNeg=0
tweetNumber = 0 #used for output
for tweet in listOfTweets:
tweetNumber += 1
place=utf8(tweet["user"]["location"])
person=utf8(tweet["user"]["screen_name"])
numRetweets=tweet["retweet_count"]
geocode=tweet["geo"]
time=tweet["created_at"].split(" +")[0]
text=utf8(tweet['text'])
print str(tweetNumber) + ") " + person + ": " + text
print "(Retweeted "+str(numRetweets)+" times)"
if place=="":
print "(Location unknown)."
else:
print "(Tweeted from "+place+")."
print "(Tweeted on "+time+")"
tweetFeatures = extractFeaturesFrom(tweet["text"])
if tweetFeatures is not None:
polarity = classifier.classify(tweetFeatures)
if polarity == "neg":
print "(Detected a negative sentiment)"
numNeg=numNeg+1
elif polarity == "pos":
print "(Detected a positive sentiment)"
numPos=numPos+1
print
print "In your search, found " + str(numPos) + " positive tweets, and " + str(numNeg) + " negative ones."
return (listOfTweets, numPos, numNeg)