-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtwi222.py
185 lines (177 loc) · 6.25 KB
/
twi222.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import argparse
import os
import os.path as osp
import time
import numpy as np
import torch
import torch.nn.functional as F
from sklearn.metrics import f1_score
import pandas
import json
import torch_geometric.transforms as T
from tqdm import tqdm
import spacy
import re
import ijson
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# {'retweeted', 'pinned', 'own', 'mentioned', 'followed', 'contain', 'discuss', 'quoted', 'following', 'membership', 'replied_to', 'followers', 'post', 'like'}
path = '../../datasets'
dataset1 = 'Twibot-22'
path1 = os.path.join(path, dataset1)
with open(os.path.join(path1, 'user.json'), 'r', encoding='UTF-8') as f:
node1 = json.load(f)
edge1 = pandas.read_csv(os.path.join(path1, 'edge.csv'))
label1 = pandas.read_csv(os.path.join(path1, 'label.csv'))
split1 = pandas.read_csv(os.path.join(path1, 'split.csv'))
nlp = spacy.load('en_core_web_sm')
i = 0
v = 0
o = 0
tweet = []
id_map = dict()
tweet_map = dict()
text_map = dict()
support=[]
for index, node in split1.iterrows():
if node['split'] == 'support':
support.append(node['id'])
for node in tqdm(node1):
if node['id'][0] == 'u' and node['id'] not in support:
id_map[node['id']] = i
i=i+1
feature = np.zeros((i, 16))
'''
for node in tqdm(node1):
if node['id'][0] == 'u' and type(node['description'])==str:
doc=nlp(node['description'])
pos = [token.pos_ for token in doc]
feature[o,1] += node['description'].count('@')
feature[o,2] += node['description'].count('#')
for s in node['description']:
if s.isupper():
feature[o,4]+=1
feature[o,14] += len(pos)
feature[o,15]+=SentimentIntensityAnalyzer().polarity_scores(node['description'])['compound']
for token in doc:
if token.like_url:
feature[o,0] += 1
for p in pos:
if p=='PUNCT':
feature[o,3] += 1
elif p=='NOUN':
feature[o,5] += 1
elif p=='PRON':
feature[o,6] += 1
elif p=='VERB':
feature[o,7] += 1
elif p=='ADV':
feature[o,8] += 1
elif p=='ADJ':
feature[o,9] += 1
elif p=='ADP':
feature[o,10] += 1
elif p=='CCONJ' or 'SCONJ':
feature[o,11] += 1
elif p=='NUM':
feature[o,12] += 1
elif p=='INTJ':
feature[o,13] += 1
o=o+1
if o==i:
break
'''
tweet_index=0
text_user=dict()
with open("/data2/whr/czl/TwiBot22-baselines/src/twibot22_Botrgcn_feature/id_tweet.json", 'r', encoding='UTF-8') as f:
idindex_tweet=json.load(f)
with open("/data2/whr/czl/TwiBot22-baselines/datasets/Twibot-22/tweet_2.json", 'r', encoding='UTF-8') as f:
obj = ijson.items(f, "item")
for x in tqdm(obj):
if x['author_id'] not in support and x['author_id'] in id_map.keys():
Text.append(x['text'])
text_user[x['text']]=id_map[x['author_id']]
with open("/data2/whr/czl/TwiBot22-baselines/datasets/Twibot-22/tweet_3.json", 'r', encoding='UTF-8') as f:
obj = ijson.items(f, "item")
for x in tqdm(obj):
if x['author_id'] not in support and x['author_id'] in id_map.keys():
Text.append(x['text'])
text_user[x['text']]=id_map[x['author_id']]
with open("/data2/whr/czl/TwiBot22-baselines/datasets/Twibot-22/tweet_4.json", 'r', encoding='UTF-8') as f:
obj = ijson.items(f, "item")
for x in tqdm(obj):
if x['author_id'] not in support and x['author_id'] in id_map.keys():
text_user[x['text']]=id_map[x['author_id']]
for text in tqdm(text_user.keys()):
Text=nlp(text)
pos = [token.pos_ for token in Text]
feature[text_user[text],1] += text.count('@')
feature[text_user[text],2] += text.count('#')
for s in text:
if s.isupper():
feature[text_user[text],4]+=1
feature[text_user[text],14] += len(pos)
feature[text_user[text],15]+=SentimentIntensityAnalyzer().polarity_scores(text)['compound']
for token in Text:
if token.like_url:
feature[text_user[text],0] += 1
for p in pos:
if p=='PUNCT':
feature[text_user[text],3] += 1
elif p=='NOUN':
feature[text_user[text],5] += 1
elif p=='PRON':
feature[text_user[text],6] += 1
elif p=='VERB':
feature[text_user[text],7] += 1
elif p=='ADV':
feature[text_user[text],8] += 1
elif p=='ADJ':
feature[text_user[text],9] += 1
elif p=='ADP':
feature[text_user[text],10] += 1
elif p=='CCONJ' or 'SCONJ':
feature[text_user[text],11] += 1
elif p=='NUM':
feature[text_user[text],12] += 1
elif p=='INTJ':
feature[text_user[text],13] += 1
'''
for text in tqdm(text_map.keys()):
if type(text)==str and text_map[text] in tweet_map.keys():
# feature[id_map[tweet_map[tweet[count]]]][0] += len(urls)
Text=nlp(text)
pos = [token.pos_ for token in Text]
feature[text_user[text],1] += text.count('@')
feature[text_user[text],2] += text.count('#')
for s in text:
if s.isupper():
feature[id_map[tweet_map[tweet[count]]]][4]+=1
feature[text_user[text],14] += len(pos)
feature[text_user[text],15]+=SentimentIntensityAnalyzer().polarity_scores(text)['compound']
for token in Text:
if token.like_url:
feature[text_user[text],0] += 1
for p in pos:
if p=='PUNCT':
feature[text_user[text],3] += 1
elif p=='NOUN':
feature[text_user[text],5] += 1
elif p=='PRON':
feature[text_user[text],6] += 1
elif p=='VERB':
feature[text_user[text],7] += 1
elif p=='ADV':
feature[text_user[text],8] += 1
elif p=='ADJ':
feature[text_user[text],9] += 1
elif p=='ADP':
feature[text_user[text],10] += 1
elif p=='CCONJ' or 'SCONJ':
feature[text_user[text],11] += 1
elif p=='NUM':
feature[text_user[text],12] += 1
elif p=='INTJ':
feature[text_user[text],13] += 1
count+=1
'''
np.savetxt('featuretwi22.1.csv', feature, delimiter = ',')