-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_data_02.py
71 lines (60 loc) · 1.9 KB
/
graph_data_02.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
import numpy as np
import pandas as pd
import os
import re
from collections import defaultdict
from tqdm import tqdm
import csv
comments = pd.read_csv(
os.path.join("polished_data", "comments_from_videos.csv"),
dtype={
"id": "Int64",
"video_id": "Int64",
"author": object,
"date": "Int64",
"likes": "Int64",
"replies": "Int64",
"reply_of": "Int64",
"text": object,
},
)
videos = pd.read_csv(os.path.join("polished_data", "videos_from_influencers.csv"))
hashtags = pd.read_csv(
os.path.join("polished_data", "hashtags_from_influencers.csv"),
names=["ht", "count"],
)
data: defaultdict[tuple, int] = defaultdict(int)
for comment in tqdm(comments.itertuples(), total=comments.shape[0]):
# Comments and replies
if not pd.isna(comment.reply_of):
try:
data[(comment.id, comment.reply_of)] += 1
except:
pass
else:
data[(comment.id, comment.video_id)] += 1
# Authors of the comments
data[(comment.author, comment.id)] += 1
data[(comment.id, comment.author)] += 1
# Data for hashtags
ht_regex = r"#(\w+)"
if isinstance(comment.text, str):
ht_list = re.findall(ht_regex, comment.text)
for ht in ht_list:
data[(comment.id, f"hashtag_{ht}")] += 1
for video in tqdm(videos.itertuples(), total=videos.shape[0]):
for ht in hashtags.ht:
if videos.loc[video.Index, f"hashtag_{ht}"]:
data[(video.id, f"hashtag_{ht}")] += 1
data[(video.id, video.author)] += 1
data[(video.author, video.id)] += 1
with open(
os.path.join("polished_data", "complete_graph.csv"),
"w",
encoding="utf-8",
newline="",
) as fp:
file_writer = csv.writer(fp, quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
file_writer.writerow(["Source", "Target", "Weight"])
for x in data:
file_writer.writerow([x[0], x[1], data[x]])