-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.py
127 lines (104 loc) · 4.27 KB
/
Score.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Twitter as tw
import math
import time
import sentimentTest as st
import sentenceAnalysis as sa
import Search
MONTH_LENGTH = 2.628e6
def scores_from_raw(raw_values):
to_ret = {}
# Number of hashtags
num_hashtags = raw_values['hashtags']
to_ret['hashtags'] = 5 if num_hashtags < 2 else max(6 - num_hashtags, 0)
# Number of retweets & likes
retweets = raw_values['retweets']
likes = raw_values['likes']
retweet_score = max(0.0, min(5.0, 1.55 * math.log((1 + retweets) / 20)))
like_score = max(0.0, min(5.0, 1.278 * math.log((1 + likes) / 20)))
to_ret['retweets'] = retweet_score
to_ret['likes'] = like_score
# Verified on Twitter
verified = raw_values['verified']
to_ret['verified'] = 5 if verified else 0
# Length of tweet (weight low)
length = raw_values['length']
to_ret['length'] = min(5 * length / 140, 5)
# Date
time_written = raw_values['written']
time_now = int(time.time())
diff = time_now - time_written
old_tweet = diff > MONTH_LENGTH
to_ret['written'] = MONTH_LENGTH / diff * 5 if old_tweet else 5
# Date of account creation
time_created = raw_values['created']
diff = time_now - time_created
old_account = diff > MONTH_LENGTH
to_ret['created'] = 5 if old_account else diff / MONTH_LENGTH * 5
# Sentiment analysis
text = raw_values['text']
direction, mag = st.sentimentDetection(text)
to_ret['direction'] = direction
to_ret['magnitude'] = mag
# Sentence Analysis
to_ret['cap'] = sa.capitalization(text)
to_ret['compx'] = sa.sentenceComplexity(text)
to_ret['misspell'] = sa.mispells(text)
return to_ret
def generate_messages(raw_scores):
messages = {'g': [], 'b': []}
if raw_scores['verified'] == 5:
messages['g'].append('The account is verified on Twitter.')
else:
messages['b'].append('The account is not verified on Twitter.')
if raw_scores['likes'] < 2 or raw_scores['likes'] < 2:
messages['b'].append('This tweet has very little interaction.')
if raw_scores['written'] == 5:
messages['g'].append('This tweet was written recently (within the last month).')
else:
messages['b'].append('This tweet may be out of date and not reflect current conditions.')
if raw_scores['created'] < 5:
messages['b'].append('The account was created recently. Take care if it is not verified as they may be a bot.')
if raw_scores['magnitude'] < 2.5:
messages['b'].append('Tone of this tweet includes overly emotional language that may be trying to appeal to pathos.')
return messages
def aggregate(raw_scores):
complexity = 7.44 * raw_scores['compx']
capitalization = 6.46 * raw_scores['cap']
spelling = 6.46 * raw_scores['misspell']
hashtags = 7.96 * raw_scores['hashtags']
retweets = 2.97 * raw_scores['retweets']
likes = 2.97 * raw_scores['likes']
verified = 13.93 * raw_scores['verified']
length = 4.97 * raw_scores['length']
written = 7.96 * raw_scores['written']
created = 10.94 * raw_scores['created']
magnitude = 27.94 * raw_scores['magnitude']
scores = [complexity, capitalization, spelling, hashtags, retweets, likes, verified, length, written, created, magnitude]
return max(0, (sum(scores) / 100 - 3) * 5 / 2)
def process_tweet_url(url):
tw.authenticate()
res = tw.get_id_from_url(url)
tweet = tw.get_tweet_by_id(res)
raw_values = tw.process_tweet(tweet)
raw_scores = scores_from_raw(raw_values)
msgs = generate_messages(raw_scores)
aggregate_score = aggregate(raw_scores)
pf_url = Search.build_politifact_url_from_tweet(tweet)
pf = Search.scrape_politifact(pf_url)
if tweet['user']['verified']:
gl_url = Search.build_google_url_from_tweet(tweet)
wiki_lnk = Search.scrape_author(gl_url)
wiki_title = 'Wikipedia'
wiki = (wiki_lnk, wiki_title)
else:
wiki = ('Could Not Find A Link', '')
return {'score': aggregate_score, 'raw_score': raw_scores, 'messages': msgs, 'wiki': wiki, 'politifact': pf}
def main():
tw.authenticate()
user_input = input('What tweet to show? ')
while user_input != 'exit':
res = process_tweet_url(user_input)
print(res)
user_input = input('What tweet to show? ')
if __name__ == "__main__":
main()