Spaces:
Runtime error
Runtime error
Update radical_tweet_aggregator.py
Browse files- radical_tweet_aggregator.py +19 -10
radical_tweet_aggregator.py
CHANGED
@@ -121,35 +121,44 @@ class IDPrinter(tweepy.StreamingClient):
|
|
121 |
if tweet_data != None:
|
122 |
# Use Pinpoint to identify if a Tweet is extremist or not
|
123 |
is_extremist = predictor().predict(tweet_data)
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
127 |
# of posts that are extremist
|
128 |
-
if is_extremist != None and is_extremist == 1:
|
129 |
-
tweets = client.get_users_tweets(id=tweet.author_id, max_results=
|
130 |
|
131 |
number_extreme = 0
|
132 |
tweets = tweets[0]
|
|
|
|
|
133 |
for users_tweet in tweets:
|
134 |
if users_tweet.text != None:
|
135 |
-
|
|
|
|
|
|
|
136 |
if is_extremist != None:
|
137 |
if is_extremist == True:
|
138 |
number_extreme = number_extreme + 1
|
139 |
|
140 |
#print(number_extreme)
|
141 |
-
threshold = number_extreme/len(tweets
|
|
|
|
|
|
|
142 |
#print("Threshold {}".format(threshold))
|
143 |
-
if threshold > 1: #
|
144 |
|
145 |
file_name = os.path.join("users","{}-{}-radical_users.txt".format(username,date.today().strftime("%b-%d-%Y")))
|
146 |
-
print("User {} was found to be extremist".format(username))
|
147 |
file_path = Path(file_name)
|
148 |
file_path.touch(exist_ok=True)
|
149 |
|
150 |
# Write user to a file in the user folder with the percentage of extremist posts
|
151 |
with open(file_name, 'w') as outfile:
|
152 |
-
json_to_dump = [{"username": username.id, "threshold": threshold,
|
153 |
"date": date.today().strftime("%b-%d-%Y")}]
|
154 |
json.dump(json_to_dump, outfile, indent=4)
|
155 |
print("Got user {}".format(username))
|
121 |
if tweet_data != None:
|
122 |
# Use Pinpoint to identify if a Tweet is extremist or not
|
123 |
is_extremist = predictor().predict(tweet_data)
|
124 |
+
toxicity_score = Detoxify('original').predict(tweet_data)["toxicity"]
|
125 |
+
|
126 |
+
print("user {} post extremist {}, toxicity {} - message: {}".format(username, is_extremist, toxicity_score, str(tweet_data)))
|
127 |
+
|
128 |
+
# If a tweet is extremist or toxicity above 0.5, go through 200 of that users posts and identify the percentage
|
129 |
# of posts that are extremist
|
130 |
+
if (is_extremist != None and is_extremist == 1) or (toxicity_score != None and toxicity_score >= 0.5):
|
131 |
+
tweets = client.get_users_tweets(id=tweet.author_id, max_results=100)
|
132 |
|
133 |
number_extreme = 0
|
134 |
tweets = tweets[0]
|
135 |
+
|
136 |
+
list_of_toxicity_scores = []
|
137 |
for users_tweet in tweets:
|
138 |
if users_tweet.text != None:
|
139 |
+
tweet_msg = users_tweet.text
|
140 |
+
is_extremist = predictor().predict(tweet_msg)
|
141 |
+
toxicity_score = Detoxify('original').predict(tweet_msg)["toxicity"]
|
142 |
+
list_of_toxicity_scores.append(toxicity_score)
|
143 |
if is_extremist != None:
|
144 |
if is_extremist == True:
|
145 |
number_extreme = number_extreme + 1
|
146 |
|
147 |
#print(number_extreme)
|
148 |
+
threshold = number_extreme/len(tweets)
|
149 |
+
threshold = threshold * 100
|
150 |
+
toxicity_avg = sum(list_of_toxicity_scores) / len(tweets)
|
151 |
+
toxicity_avg = toxicity_avg * 100
|
152 |
#print("Threshold {}".format(threshold))
|
153 |
+
if threshold > 1 or toxicity_avg > 1: #
|
154 |
|
155 |
file_name = os.path.join("users","{}-{}-radical_users.txt".format(username,date.today().strftime("%b-%d-%Y")))
|
|
|
156 |
file_path = Path(file_name)
|
157 |
file_path.touch(exist_ok=True)
|
158 |
|
159 |
# Write user to a file in the user folder with the percentage of extremist posts
|
160 |
with open(file_name, 'w') as outfile:
|
161 |
+
json_to_dump = [{"username": username.id, "violence-threshold": threshold, "toxicity-threshold":toxicity_avg,
|
162 |
"date": date.today().strftime("%b-%d-%Y")}]
|
163 |
json.dump(json_to_dump, outfile, indent=4)
|
164 |
print("Got user {}".format(username))
|