-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtwibot-22.py
146 lines (121 loc) · 5.04 KB
/
twibot-22.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import torch
import numpy as np
import pandas as pd
from datetime import datetime as dt
from dataset import fast_merge,df_to_mask
from tqdm import tqdm
dataset_names = [
'botometer-feedback-2019', 'botwiki-2019', 'celebrity-2019', 'cresci-2015', 'cresci-2017', 'cresci-rtbust-2019', 'cresci-stock-2018', 'gilani-2017', 'midterm-2018', 'political-bots-2019', 'pronbots-2019', 'vendor-purchased-2019', 'verified-2019', "Twibot-20"
]
path='./twibot-22/'
from utils import entropy,bigrams_freq
node=pd.read_json("../datasets/Twibot-22/user.json")
following_count=[]
for i,each in enumerate(node['public_metrics']):
if i==len(node):
break
if each is not None and isinstance(each,dict):
if each['following_count'] is not None:
following_count.append(each['following_count'])
else:
following_count.append(0)
else:
following_count.append(0)
statues=[]
for i,each in enumerate(node['public_metrics']):
if i==len(node):
break
if each is not None and isinstance(each,dict):
if each['tweet_count'] is not None:
statues.append(each['tweet_count'])
else:
statues.append(0)
else:
statues.append(0)
listed_count=[]
for i,each in enumerate(node['public_metrics']):
if i==len(node):
break
if each is not None and isinstance(each,dict):
if each['tweet_count'] is not None:
listed_count.append(each['listed_count'])
else:
listed_count.append(0)
else:
listed_count.append(0)
followers_count=[]
for each in node['public_metrics']:
if each is not None and each['followers_count'] is not None:
followers_count.append(int(each['followers_count']))
else:
followers_count.append(0)
num_username=[]
for each in node['username']:
if each is not None:
num_username.append(len(each))
else:
num_username.append(int(0))
created_at=node['created_at']
created_at=pd.to_datetime(created_at,unit='s')
followers_count=pd.DataFrame(followers_count)
followers_count=(followers_count-followers_count.mean())/followers_count.std()
followers_count=torch.tensor(np.array(followers_count),dtype=torch.float32)
date0=dt.strptime('Tue Sep 5 00:00:00 +0000 2020 ','%a %b %d %X %z %Y ')
active_days=[]
for each in created_at:
active_days.append((date0-each).days)
active_days=pd.DataFrame(active_days)
active_days=active_days.fillna(int(1)).astype(np.float32)
active_days=active_days.replace(0,1).astype(np.float32)
listed_count=pd.DataFrame(listed_count)
tweet_freq=listed_count/active_days
tweet_freq=tweet_freq.fillna(0)
tweet_freq=(tweet_freq-tweet_freq.mean())/tweet_freq.std()
tweet_freq=(tweet_freq-tweet_freq.mean())/tweet_freq.std()
tweet_freq=torch.tensor(np.array(tweet_freq),dtype=torch.float32)
listed_count=(listed_count-listed_count.mean())/listed_count.std()
listed_count=torch.tensor(np.array(listed_count),dtype=torch.float32)
active_days=(active_days-active_days.mean())/active_days.std()
active_days=torch.tensor(np.array(active_days),dtype=torch.float32)
num_username=pd.DataFrame(num_username)
num_username=(num_username-num_username.mean())/num_username.std()
num_username=torch.tensor(np.array(num_username),dtype=torch.float32)
following_count=pd.DataFrame(following_count)
following_count=(following_count-following_count.mean())/following_count.std()
following_count=torch.tensor(np.array(following_count),dtype=torch.float32)
statues=pd.DataFrame(statues)
statues=(statues-statues.mean())/statues.std()
statues=torch.tensor(np.array(statues),dtype=torch.float32)
screen_name_freq=[]
for each in tqdm(node['name']):
if each is None or len(each)<=1:
screen_name_freq.append(0)
else:
screen_name_freq.append(bigrams_freq(each))
screen_name_freq=pd.DataFrame(screen_name_freq)
screen_name_freq=(screen_name_freq-screen_name_freq.mean())/screen_name_freq.std()
screen_name_freq=torch.tensor(np.array(screen_name_freq),dtype=torch.float32)
name_entropy=[]
for each in tqdm(node['name']):
if each is None or len(each)==0:
name_entropy.append(0)
else:
name_entropy.append(entropy(each))
name_entropy=pd.DataFrame(name_entropy)
name_entropy=(name_entropy-name_entropy.mean())/name_entropy.std()
name_entropy=torch.tensor(np.array(name_entropy),dtype=torch.float32)
des_entropy=[]
for each in tqdm(node['description']):
if each is None or len(each)==0:
des_entropy.append(0)
else:
des_entropy.append(entropy(each))
des_entropy=pd.DataFrame(des_entropy)
des_entropy=(des_entropy-des_entropy.mean())/des_entropy.std()
des_entropy=torch.tensor(np.array(des_entropy),dtype=torch.float32)
num_properties_tensor=torch.cat([followers_count,tweet_freq,num_username,following_count,statues,listed_count,screen_name_freq,name_entropy,des_entropy],dim=1)
pd.DataFrame(num_properties_tensor.detach().numpy()).isna().value_counts()
torch.save(num_properties_tensor,path+'num_properties_tensor.pt')
node['description']=node['description'].replace('', 'missing')
np.save(path+'des.npy',list(node['description']))
print('finished')