-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexical_diversity.py
More file actions
51 lines (34 loc) · 1.24 KB
/
lexical_diversity.py
File metadata and controls
51 lines (34 loc) · 1.24 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
49
50
51
import twitter
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
OAUTH_TOKEN = 'XXXX'
OAUTH_TOKEN_SECRET = 'XXXX'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
q = '#MentionSomeoneImportantForYou' #can replace this with anything
count = 100
search_results = twitter_api.search.tweets(q=q, count=count)
statuses = search_results['statuses']
status_texts = [ status['text']
for status in statuses]
screen_names = [user_mention['screen_name']
for status in statuses
for user_mention in status['entities']['user_mentions']]
hashtags = [hashtag['text']
for status in statuses
for hashtag in status['entities']['hashtags']]
#Compute a collection of all words from all tweets
words=[w
for t in status_texts
for w in t.split()]
# A function for computing lexical diversity
def lexical_diversity(tokens):
return 1.0*len(set(tokens))/len(tokens)
# A function for computing the average number of words per tweet
def average_words(statuses):
total_words = sum([ len(s.split()) for s in statuses ])
return 1.0*total_words/len(statuses)
print lexical_diversity(words)
print lexical_diversity(screen_names)
print lexical_diversity(hashtags)
print average_words(status_texts)