-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_trust.py
More file actions
978 lines (803 loc) · 36.5 KB
/
Copy pathgenerate_trust.py
File metadata and controls
978 lines (803 loc) · 36.5 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
#!/usr/bin/env python3
"""
Seed Graph Trust Score Generator
This script generates local trust scores between users based on seed interactions:
1. Loads ALL *_seed_extended_followings.json, *_seed_followings.json, and *_seed_interactions.json from raw/seed/
OR loads {seed_graph}_{range}.json and {seed_graph}_followings.json from raw/
2. Creates local trust scores from one user to another based on interactions
3. Assigns weights to each interaction type based on config.toml trust_weights
4. Deduplicates data across seed files (same posts, followings, etc. only counted once)
5. Aggregates scores for each unique i,j pair
6. Saves merged local trust to trust/seed_graph.csv with header i,j,v
Interaction types and their sources:
- follow: from seed_followings.json (only seed users -> master_list) and seed_extended_followings.json (only towards master_list)
- mention: from seed_interactions.json (posts mentioning other users)
- reply: from seed_interactions.json (reply posts and replies list)
- retweet: from seed_interactions.json (retweet posts)
- quote: from seed_interactions.json (quote posts)
Seed Graph Follow Relationships:
- seed_followings.json: Only creates follow relationships from seed users TO master_list users
- seed_extended_followings.json: Only creates follow relationships towards master_list users
Deduplication Strategy:
- Follow relationships: tracked by (source, target) pair
- Posts/Replies: tracked by post_id to avoid counting same post multiple times
- Same interaction from duplicate data is only counted once
Note: Unlike the community version, this does NOT apply 2x weight multiplier for any posts,
as there is no concept of "community posts" in the seed graph context.
"""
import csv
import glob
import json
import os
import re
from collections import defaultdict
from datetime import datetime
import toml
def load_config():
"""Load configuration from config.toml"""
try:
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
# Config is in the same directory as the script
config_path = os.path.join(script_dir, "config.toml")
with open(config_path, "r") as f:
config = toml.load(f)
print("✓ Configuration loaded successfully")
return config
except FileNotFoundError:
print("❌ Error: config.toml not found")
return None
except Exception as e:
print(f"❌ Error loading config: {e}")
return None
def get_seed_user_ids_from_config():
"""Get all seed user IDs from config.toml [seed_graph] section."""
config = load_config()
if not config:
return set()
seed_graph_config = config.get("seed_graph", {})
seed_user_ids = set()
for community_name, user_ids in seed_graph_config.items():
if isinstance(user_ids, list):
seed_user_ids.update(str(uid) for uid in user_ids)
return seed_user_ids
def get_seed_graph_names_from_config():
"""Get all seed graph names from config.toml [seed_graph] section."""
config = load_config()
if not config:
return []
seed_graph_config = config.get("seed_graph", {})
return list(seed_graph_config.keys())
def load_json_file(file_path):
"""Load data from a JSON file"""
if not os.path.exists(file_path):
print(f"⚠️ File not found: {file_path}")
return None
try:
with open(file_path, "r", encoding="utf-8") as f:
data = json.load(f)
print(f"✓ Loaded {os.path.basename(file_path)}")
return data
except Exception as e:
print(f"❌ Error loading {file_path}: {e}")
return None
def normalize_username(username):
"""Normalize username by removing @ and converting to lowercase"""
if not username:
return ""
return username.lower().strip().lstrip("@")
def normalize_user_id(user_id):
"""Normalize user_id to string format"""
if not user_id:
return ""
return str(user_id).strip()
def build_username_to_id_map(
followings_data, extended_followings_data, interactions_data
):
"""Build a mapping from normalized username to user_id from all data sources"""
username_to_id = {}
# From followings data - master_list and seed_users
if followings_data:
for user in followings_data.get("master_list", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
for user in followings_data.get("seed_users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
# From extended followings data
if extended_followings_data:
for user in extended_followings_data.get("users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
# From interactions data
if interactions_data:
for user in interactions_data.get("users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
return username_to_id
def build_username_to_id_map_from_raw(
followings_data, interactions_data_list, extended_followings_data=None
):
"""Build a mapping from normalized username to user_id from raw data sources"""
username_to_id = {}
# From followings data - master_list and seed_users
if followings_data:
for user in followings_data.get("master_list", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
for user in followings_data.get("seed_users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
# From interactions data files
for interactions_data in interactions_data_list:
if interactions_data:
for user in interactions_data.get("users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
# From extended_followings.json users list
if extended_followings_data:
users = extended_followings_data.get("users", [])
for user in users:
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
return username_to_id
def extract_mentions(text):
"""Extract mentioned usernames from text"""
if not text:
return []
# Find all @mentions in the text
mentions = re.findall(r"@(\w+)", text)
return [normalize_username(mention) for mention in mentions]
def process_seed_followings(
followings_data, trust_weights, seen_follows, username_to_id
):
"""Process seed_followings.json to extract follow relationships towards master_list only
Args:
followings_data: The followings data structure
trust_weights: Weight configuration
seen_follows: Set of (source, target) tuples to track duplicate follows
username_to_id: Mapping from username to user_id
"""
interactions = []
if not followings_data:
return interactions
follow_weight = trust_weights.get("follow", 30)
print(f" Processing seed_followings.json with weight {follow_weight}")
# Get seed users and master list
seed_users = followings_data.get("seed_users", [])
master_list = followings_data.get("master_list", [])
if not seed_users or not master_list:
print(f" No seed users or master list found")
return interactions
# Create follow relationships from seed users to master list users
follow_count = 0
for seed_user in seed_users:
seed_user_id = normalize_user_id(seed_user.get("user_id", ""))
if not seed_user_id:
continue
# Each seed user follows all users in master_list
for master_user in master_list:
master_user_id = normalize_user_id(master_user.get("user_id", ""))
if master_user_id and seed_user_id != master_user_id:
# Check for duplicates
follow_pair = (seed_user_id, master_user_id)
if follow_pair not in seen_follows:
seen_follows.add(follow_pair)
interactions.append(
{
"type": "follow",
"source": seed_user_id,
"target": master_user_id,
"weight": follow_weight,
}
)
follow_count += 1
print(
f" Found {follow_count} unique follow relationships (seed users -> master_list)"
)
return interactions
def process_seed_extended_followings(
seed_extended_data, trust_weights, seen_follows=None
):
"""Process seed_extended_followings.json to extract follow relationships
Args:
seed_extended_data: The extended followings data structure
trust_weights: Weight configuration
seen_follows: Set of (source, target) tuples to track duplicate follows
"""
interactions = []
if seen_follows is None:
seen_follows = set()
if not seed_extended_data:
return interactions
follow_weight = trust_weights.get("follow", 30)
print(f" Processing seed_extended_followings.json with weight {follow_weight}")
users = seed_extended_data.get("users", [])
if not users:
print(f" No users found")
return interactions
follow_count = 0
total_users = len(users)
for idx, user in enumerate(users):
if (idx + 1) % 100 == 0 or idx == 0:
print(f" Processing user {idx + 1}/{total_users}...")
follower_id = normalize_user_id(user.get("user_id", ""))
if not follower_id:
continue
following_ids = user.get("following_ids", [])
for followed_id in following_ids:
followed_id_str = normalize_user_id(followed_id)
if followed_id_str and follower_id != followed_id_str:
# Check for duplicates
follow_pair = (follower_id, followed_id_str)
if follow_pair not in seen_follows:
seen_follows.add(follow_pair)
interactions.append(
{
"type": "follow",
"source": follower_id,
"target": followed_id_str,
"weight": follow_weight,
}
)
follow_count += 1
print(f" Found {follow_count} unique follow relationships")
return interactions
def process_seed_interactions(
interactions_data, trust_weights, seen_posts=None, username_to_id=None
):
"""Process seed user interactions to extract various interaction types
Args:
interactions_data: The interactions data structure
trust_weights: Weight configuration
seen_posts: Set of post_ids to track duplicate posts/replies
username_to_id: Mapping from username to user_id
"""
interactions = []
if seen_posts is None:
seen_posts = set()
if username_to_id is None:
username_to_id = {}
if not interactions_data or "users" not in interactions_data:
return interactions
mention_weight = trust_weights.get("mention", 30)
reply_weight = trust_weights.get("reply", 20)
retweet_weight = trust_weights.get("retweet", 50)
quote_weight = trust_weights.get("quote", 40)
print(f" Processing seed interactions")
print(
f" Weights: mention={mention_weight}, reply={reply_weight}, retweet={retweet_weight}, quote={quote_weight}"
)
print(f" No weight multipliers applied (seed graph has no community concept)")
interaction_counts = defaultdict(int)
for user in interactions_data["users"]:
user_id = normalize_user_id(user.get("user_id", ""))
if not user_id:
continue
# Process posts
posts = user.get("posts", [])
for post in posts:
post_id = post.get("post_id", "")
# Skip if we've already seen this post
if post_id and post_id in seen_posts:
continue
if post_id:
seen_posts.add(post_id)
post_text = post.get("text", "")
is_reply = post.get("is_reply", False)
is_retweet = post.get("is_retweet")
is_quote = post.get("is_quote")
reply_to_user_id = normalize_user_id(post.get("reply_to_user_id", ""))
# Fallback to username lookup if reply_to_user_id not available
if not reply_to_user_id:
reply_to_username = normalize_username(
post.get("reply_to_username", "")
)
reply_to_user_id = username_to_id.get(reply_to_username, "")
# Process retweets
if is_retweet:
original_creator_id = normalize_user_id(
post.get("original_post_creator_user_id", "")
)
# Fallback to username lookup
if not original_creator_id:
original_creator_username = normalize_username(
post.get("original_post_creator_username", "")
)
original_creator_id = username_to_id.get(
original_creator_username, ""
)
if original_creator_id and user_id != original_creator_id:
interactions.append(
{
"type": "retweet",
"source": user_id,
"target": original_creator_id,
"weight": retweet_weight,
}
)
interaction_counts["retweet"] += 1
# Process quotes (is_quote can be a dict or boolean)
elif is_quote:
original_creator_id = normalize_user_id(
post.get("original_post_creator_user_id", "")
)
# Fallback to username lookup
if not original_creator_id:
original_creator_username = normalize_username(
post.get("original_post_creator_username", "")
)
original_creator_id = username_to_id.get(
original_creator_username, ""
)
if original_creator_id and user_id != original_creator_id:
interactions.append(
{
"type": "quote",
"source": user_id,
"target": original_creator_id,
"weight": quote_weight,
}
)
interaction_counts["quote"] += 1
# Process replies
elif is_reply and reply_to_user_id:
if user_id != reply_to_user_id:
interactions.append(
{
"type": "reply",
"source": user_id,
"target": reply_to_user_id,
"weight": reply_weight,
}
)
interaction_counts["reply"] += 1
# Process mentions in post text (lookup user_id from username)
mentions = extract_mentions(post_text)
for mentioned_username in mentions:
mentioned_user_id = username_to_id.get(mentioned_username, "")
if mentioned_user_id and user_id != mentioned_user_id:
interactions.append(
{
"type": "mention",
"source": user_id,
"target": mentioned_user_id,
"weight": mention_weight,
}
)
interaction_counts["mention"] += 1
# Process replies (separate from posts in seed_interactions format)
replies = user.get("replies", [])
for reply in replies:
reply_id = reply.get("post_id", "")
# Skip if we've already seen this reply
if reply_id and reply_id in seen_posts:
continue
if reply_id:
seen_posts.add(reply_id)
reply_text = reply.get("text", "")
reply_to_user_id = normalize_user_id(reply.get("reply_to_user_id", ""))
# Fallback to username lookup
if not reply_to_user_id:
reply_to_username = normalize_username(
reply.get("reply_to_username", "")
)
reply_to_user_id = username_to_id.get(reply_to_username, "")
if reply_to_user_id and user_id != reply_to_user_id:
interactions.append(
{
"type": "reply",
"source": user_id,
"target": reply_to_user_id,
"weight": reply_weight,
}
)
interaction_counts["reply"] += 1
# Process mentions in reply text (lookup user_id from username)
mentions = extract_mentions(reply_text)
for mentioned_username in mentions:
mentioned_user_id = username_to_id.get(mentioned_username, "")
if mentioned_user_id and user_id != mentioned_user_id:
interactions.append(
{
"type": "mention",
"source": user_id,
"target": mentioned_user_id,
"weight": mention_weight,
}
)
interaction_counts["mention"] += 1
for interaction_type, count in sorted(interaction_counts.items()):
print(f" Found {count} {interaction_type} interactions")
return interactions
def aggregate_trust_scores(all_interactions):
"""Aggregate trust scores for unique i,j pairs"""
trust_matrix = defaultdict(float)
interaction_stats = defaultdict(int)
print(f" Aggregating {len(all_interactions)} total interactions")
for interaction in all_interactions:
source = interaction["source"]
target = interaction["target"]
weight = interaction["weight"]
interaction_type = interaction["type"]
if source and target and source != target:
pair = (source, target)
trust_matrix[pair] += weight
interaction_stats[interaction_type] += 1
print(f" Interaction type breakdown:")
for interaction_type, count in sorted(interaction_stats.items()):
print(f" {interaction_type}: {count}")
print(f" Unique trust relationships: {len(trust_matrix)}")
return trust_matrix
def save_trust_matrix(trust_matrix, output_name, trust_dir):
"""Save trust matrix to CSV file with header i,j,v"""
os.makedirs(trust_dir, exist_ok=True)
filename = os.path.join(trust_dir, f"{output_name}.csv")
# Sort pairs for consistent output
sorted_pairs = sorted(trust_matrix.items(), key=lambda x: (x[0][0], x[0][1]))
with open(filename, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
# Write header
writer.writerow(["i", "j", "v"])
# Write data
for (i, j), v in sorted_pairs:
writer.writerow([i, j, v])
print(f"✅ Trust matrix saved to: {filename}")
print(f"📊 Total pairs: {len(sorted_pairs)}")
# Show statistics
if sorted_pairs:
values = [v for (_, _), v in sorted_pairs]
min_weight = min(values)
max_weight = max(values)
avg_weight = sum(values) / len(values)
total_weight = sum(values)
print(f"📈 Trust score statistics:")
print(f" - Min: {min_weight}")
print(f" - Max: {max_weight}")
print(f" - Average: {avg_weight:.2f}")
print(f" - Total: {total_weight:.2f}")
return filename
def process_raw_data(raw_data_dir, trust_dir, trust_weights):
"""Process raw data files in the format {seed_graph}_{range}.json and {seed_graph}_followings.json
Args:
raw_data_dir: Directory containing raw data files
trust_dir: Directory to save trust output
trust_weights: Weight configuration
Returns:
List of paths to the generated trust files, or empty list if no data found
"""
print(f"\n{'=' * 60}")
print(f"Processing Raw Data Files")
print(f"{'=' * 60}")
# Get the script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Make paths absolute relative to script directory
if not os.path.isabs(raw_data_dir):
raw_data_dir = os.path.join(script_dir, raw_data_dir.lstrip("./"))
if not os.path.isabs(trust_dir):
trust_dir = os.path.join(script_dir, trust_dir.lstrip("./"))
# Get seed graph names from config
seed_graph_names = get_seed_graph_names_from_config()
if not seed_graph_names:
print("❌ No seed graph names found in config.toml [seed_graph] section")
return None
print(f"📋 Seed graph names from config: {', '.join(seed_graph_names)}")
generated_files = []
for seed_graph_name in seed_graph_names:
all_interactions = []
seen_follows = set()
seen_posts = set()
total_files_processed = 0
print(f"\n🔄 Processing seed graph: {seed_graph_name}")
# Look for followings file
followings_file = os.path.join(
raw_data_dir, f"{seed_graph_name}_followings.json"
)
followings_data = load_json_file(followings_file)
# Look for extended followings file
extended_followings_file = os.path.join(
raw_data_dir, f"{seed_graph_name}_extended_followings.json"
)
extended_followings_data = load_json_file(extended_followings_file)
# Look for interaction files matching pattern {seed_graph_name}_{id1}_{id2}.json
pattern = os.path.join(raw_data_dir, f"{seed_graph_name}_*_*.json")
interaction_files = [
f
for f in glob.glob(pattern)
if not f.endswith("_followings.json")
and not f.endswith("_extended_followings.json")
]
if (
not followings_data
and not extended_followings_data
and not interaction_files
):
print(f" ⚠️ No data files found for {seed_graph_name}")
continue
# Build username to user_id mapping from followings data first
username_to_id = {}
if followings_data:
for user in followings_data.get("master_list", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
for user in followings_data.get("seed_users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
total_files_processed += 1
# Add from extended followings
if extended_followings_data:
for user in extended_followings_data.get("users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
# Build username map from interaction files (process one at a time to save memory)
print(
f" 📁 Building username map from {len(interaction_files)} interaction files..."
)
for interaction_file in sorted(interaction_files):
data = load_json_file(interaction_file)
if data:
for user in data.get("users", []):
username = normalize_username(user.get("username", ""))
user_id = normalize_user_id(user.get("user_id", ""))
if username and user_id:
username_to_id[username] = user_id
del data # Free memory
print(f" 📝 Built username->user_id map with {len(username_to_id)} entries")
# Free followings_data - no longer needed
del followings_data
# Process extended followings (with deduplication)
if extended_followings_data:
extended_following_interactions = process_seed_extended_followings(
extended_followings_data, trust_weights, seen_follows
)
all_interactions.extend(extended_following_interactions)
total_files_processed += 1
print(
f" 📥 Added {len(extended_following_interactions)} follow interactions from extended_followings"
)
del extended_following_interactions # Free memory
# Free extended_followings_data - no longer needed
del extended_followings_data
# Process each interaction data file one at a time
print(f" 📁 Processing {len(interaction_files)} interaction files...")
for idx, interaction_file in enumerate(sorted(interaction_files)):
interactions_data = load_json_file(interaction_file)
if interactions_data:
seed_interactions = process_seed_interactions(
interactions_data, trust_weights, seen_posts, username_to_id
)
all_interactions.extend(seed_interactions)
total_files_processed += 1
del seed_interactions # Free memory
del interactions_data # Free memory
if (idx + 1) % 10 == 0:
print(f" Processed {idx + 1}/{len(interaction_files)} files...")
# Save username_to_id map to CSV
usernames_file = os.path.join(raw_data_dir, f"{seed_graph_name}_usernames.csv")
with open(usernames_file, "w", encoding="utf-8") as f:
f.write("username,user_id\n")
for username, user_id in sorted(username_to_id.items()):
f.write(f"{username},{user_id}\n")
print(f" 💾 Saved username map to: {usernames_file}")
# Free username_to_id - no longer needed
del username_to_id
print(f"\n📊 Summary for {seed_graph_name}:")
print(f" Total files processed: {total_files_processed}")
print(f" Total interactions collected: {len(all_interactions)}")
print(f" Unique follow relationships: {len(seen_follows)}")
print(f" Unique posts/replies processed: {len(seen_posts)}")
# Free deduplication sets - no longer needed
del seen_follows
del seen_posts
if not all_interactions:
print(f"⚠️ No interactions found for {seed_graph_name}")
continue
# Aggregate trust scores
trust_matrix = aggregate_trust_scores(all_interactions)
# Free all_interactions - no longer needed
del all_interactions
if not trust_matrix:
print(f"⚠️ No trust relationships calculated for {seed_graph_name}")
continue
# Save to {seed_graph_name}.csv
filename = save_trust_matrix(trust_matrix, seed_graph_name, trust_dir)
generated_files.append(filename)
# Free trust_matrix
del trust_matrix
return generated_files
def process_seed_graph(raw_data_dir, trust_dir, trust_weights):
"""Process all seed graph data files and merge into single trust graph
This function processes the old format files in raw/seed/ directory:
- {user_id}_seed_followings.json
- {user_id}_seed_interactions.json
- {user_id}_seed_extended_followings.json
"""
print(f"\n{'=' * 60}")
print(f"Processing Seed Graph - Merging All Seed Files")
print(f"{'=' * 60}")
# Get raw_data_dir and make it relative to project root
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.join(script_dir, "..")
# Make paths absolute
if not os.path.isabs(raw_data_dir):
raw_data_dir = os.path.join(project_root, raw_data_dir.lstrip("./"))
if not os.path.isabs(trust_dir):
trust_dir = os.path.join(project_root, trust_dir.lstrip("./"))
# Get configured seed user IDs from config.toml
configured_user_ids = get_seed_user_ids_from_config()
if not configured_user_ids:
print("❌ No seed user IDs found in config.toml [seed_graph] section")
return None
print(f"📋 Configured seed user IDs: {', '.join(sorted(configured_user_ids))}")
# Find ALL seed_followings files
pattern = os.path.join(raw_data_dir, "*_seed_followings.json")
all_matching_files = glob.glob(pattern)
# Filter to only include files for users in config.toml
matching_files = []
for file_path in all_matching_files:
file_user_id = os.path.basename(file_path).split("_seed_followings.json")[0]
if file_user_id in configured_user_ids:
matching_files.append(file_path)
if not matching_files:
print(
f"❌ No seed followings files found for configured users in: {raw_data_dir}"
)
print(f" Looking for user IDs: {', '.join(sorted(configured_user_ids))}")
print("Please run fetch_followings.py first to generate seed followings files.")
return None
print(
f"Found {len(all_matching_files)} total seed file(s), processing {len(matching_files)} for configured users"
)
# Collect all seed user IDs from filtered files
seed_user_ids = []
for followings_file in matching_files:
seed_user_id = os.path.basename(followings_file).split("_seed_followings.json")[
0
]
seed_user_ids.append(seed_user_id)
print(f" - Seed user ID: {seed_user_id}")
print(f"\n🔄 Processing interactions from all seed users...")
print(f"ℹ️ Deduplicating data across seed files...")
# Process each data source
all_interactions = []
all_master_usernames = set()
# Global deduplication trackers
seen_follows = set() # Track (source, target) pairs for follows
seen_posts = set() # Track post_ids to avoid duplicate posts
# Process each seed user's data
for seed_user_id in seed_user_ids:
print(f"\n Processing seed user: {seed_user_id}")
# Define file paths using seed_user_id prefix
interactions_file = os.path.join(
raw_data_dir, f"{seed_user_id}_seed_interactions.json"
)
followings_file = os.path.join(
raw_data_dir, f"{seed_user_id}_seed_followings.json"
)
extended_followings_file = os.path.join(
raw_data_dir, f"{seed_user_id}_seed_extended_followings.json"
)
# Load all data files for this seed user
interactions_data = load_json_file(interactions_file)
followings_data = load_json_file(followings_file)
extended_followings_data = load_json_file(extended_followings_file)
# Build username to user_id mapping from all data sources
username_to_id = build_username_to_id_map(
followings_data, extended_followings_data, interactions_data
)
print(f" Built username->user_id map with {len(username_to_id)} entries")
# Build master_list user_ids for this seed user
master_user_ids = None
if followings_data and "master_list" in followings_data:
master_list = followings_data.get("master_list", [])
master_user_ids = {
normalize_user_id(user.get("user_id", ""))
for user in master_list
if user.get("user_id")
}
all_master_usernames.update(master_user_ids)
print(f" Master list: {len(master_user_ids)} users")
# Process followings (with deduplication)
if followings_data:
following_interactions = process_seed_followings(
followings_data, trust_weights, seen_follows, username_to_id
)
all_interactions.extend(following_interactions)
print(f" Added {len(following_interactions)} unique follow interactions")
# Process extended followings (with deduplication)
if extended_followings_data:
extended_following_interactions = process_seed_extended_followings(
extended_followings_data, trust_weights, seen_follows
)
all_interactions.extend(extended_following_interactions)
print(
f" Added {len(extended_following_interactions)} unique extended follow interactions"
)
# Process interactions (with deduplication)
if interactions_data:
seed_interactions = process_seed_interactions(
interactions_data, trust_weights, seen_posts, username_to_id
)
all_interactions.extend(seed_interactions)
print(f" Added {len(seed_interactions)} unique interaction records")
print(f"\n Combined master list: {len(all_master_usernames)} unique user IDs")
print(f" Total unique interactions collected: {len(all_interactions)}")
print(f" Deduplication stats:")
print(f" - Unique follow relationships: {len(seen_follows)}")
print(f" - Unique posts/replies processed: {len(seen_posts)}")
if not all_interactions:
print("⚠️ No interactions found for seed graph")
return None
# Aggregate trust scores
trust_matrix = aggregate_trust_scores(all_interactions)
if not trust_matrix:
print("⚠️ No trust relationships calculated")
return None
# Always save to seed_graph.csv (merged output)
output_name = "seed_graph"
filename = save_trust_matrix(trust_matrix, output_name, trust_dir)
return filename
def main():
"""Main function to generate trust scores for seed graph"""
try:
print("🔗 SEED GRAPH TRUST SCORE GENERATOR")
print("=" * 50)
# Load configuration
config = load_config()
if not config:
return
# Get configuration values
raw_data_dir = config.get("output", {}).get("raw_data_dir", "./raw")
trust_weights = config.get("trust_weights", {})
trust_dir = "./trust"
print(f"📁 Raw data directory: {raw_data_dir}")
print(f"📁 Trust output directory: {trust_dir}")
print(f"⚖️ Trust weights: {trust_weights}")
# Try to process raw data format first (raw/{seed_graph}_{start}_{end}.json)
generated_files = process_raw_data(raw_data_dir, trust_dir, trust_weights)
if not generated_files:
print("❌ Failed to generate seed graph trust scores")
return
# Final summary
print(f"\n{'=' * 60}")
print(f"🎉 SEED GRAPH TRUST SCORE GENERATION COMPLETE")
print(f"{'=' * 60}")
print(f"✅ Successfully processed all seed files")
print(f"📁 Trust files saved in: {trust_dir}/")
# Show generated file info
for filename in generated_files:
if os.path.exists(filename):
with open(filename, "r") as f:
line_count = sum(1 for _ in f) - 1 # Subtract header
filename_base = os.path.basename(filename)
print(
f"📄 Generated file: {filename_base} ({line_count} trust relationships)"
)
except Exception as e:
print(f"❌ Fatal error: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()