Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +60 -48
reddit_climate_comment.py
CHANGED
@@ -55,56 +55,68 @@ class NewDataset(GeneratorBasedBuilder):
|
|
55 |
|
56 |
def _generate_examples(self, filepath):
|
57 |
df = pd.read_csv(filepath)
|
58 |
-
# Replace pd.NA with None for all columns
|
59 |
-
for column in df.columns:
|
60 |
-
df[column] = df[column].replace({pd.NA: None})
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
"CommentID": row["CommentID"],
|
83 |
-
"CommentAuthor": row["CommentAuthor"],
|
84 |
-
"CommentBody": row["CommentBody"],
|
85 |
-
"CommentTimestamp": row["CommentTimestamp"],
|
86 |
-
"CommentUpvotes": int(row["CommentUpvotes"]),
|
87 |
-
"CommentPermalink": row["CommentPermalink"],
|
88 |
-
"replies": []
|
89 |
-
}
|
90 |
-
posts[post_id]["comments"].append(comment_data)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
"
|
99 |
-
"
|
100 |
-
"
|
101 |
-
"
|
|
|
|
|
|
|
102 |
}
|
103 |
-
# Append reply data to replies list of corresponding comment
|
104 |
-
for comment in posts[post_id]["comments"]:
|
105 |
-
if comment["CommentID"] == row["CommentID"]:
|
106 |
-
comment["replies"].append(reply_data)
|
107 |
-
break
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
def _generate_examples(self, filepath):
|
57 |
df = pd.read_csv(filepath)
|
|
|
|
|
|
|
58 |
|
59 |
+
# Drop duplicate comments
|
60 |
+
df = df.drop_duplicates(subset=['CommentID', 'CommentBody'])
|
61 |
+
|
62 |
+
# Group the DataFrame by post ID
|
63 |
+
grouped_df = df.groupby('PostID')
|
64 |
+
|
65 |
+
for post_id, group in grouped_df:
|
66 |
+
post_data = group.iloc[0] # Get the data for the post
|
67 |
+
|
68 |
+
post_title = post_data['PostTitle']
|
69 |
+
post_author = post_data['PostAuthor']
|
70 |
+
post_body = post_data['PostBody']
|
71 |
+
post_url = post_data['PostUrl']
|
72 |
+
post_pic = post_data['PostPic']
|
73 |
+
subreddit = post_data['Subreddit']
|
74 |
+
post_timestamp = post_data['PostTimestamp']
|
75 |
+
post_upvotes = post_data['PostUpvotes']
|
76 |
+
post_permalink = post_data['PostPermalink']
|
77 |
|
78 |
+
comments = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Extract unique comments
|
81 |
+
unique_comment_ids = group['CommentID'].unique()
|
82 |
+
for comment_id in unique_comment_ids:
|
83 |
+
comment_row = group[group['CommentID'] == comment_id].iloc[0]
|
84 |
+
|
85 |
+
comment = {
|
86 |
+
"CommentID": comment_id,
|
87 |
+
"CommentAuthor": comment_row['CommentAuthor'],
|
88 |
+
"CommentBody": comment_row['CommentBody'],
|
89 |
+
"CommentTimestamp": comment_row['CommentTimestamp'],
|
90 |
+
"CommentUpvotes": comment_row['CommentUpvotes'],
|
91 |
+
"CommentPermalink": comment_row['CommentPermalink'],
|
92 |
+
"replies": [] # Initialize empty list for replies
|
93 |
}
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
# Check if there are replies for the current comment
|
96 |
+
replies_data = df[df['CommentID'] == comment_id][['ReplyID', 'ReplyAuthor', 'ReplyBody', 'ReplyTimestamp', 'ReplyUpvotes', 'ReplyPermalink']]
|
97 |
+
for _, reply_data in replies_data.iterrows():
|
98 |
+
reply = {
|
99 |
+
"ReplyID": str(reply_data['ReplyID']),
|
100 |
+
"ReplyAuthor": reply_data['ReplyAuthor'],
|
101 |
+
"ReplyBody": reply_data['ReplyBody'],
|
102 |
+
"ReplyTimestamp": reply_data['ReplyTimestamp'],
|
103 |
+
"ReplyUpvotes": reply_data['ReplyUpvotes'],
|
104 |
+
"ReplyPermalink": reply_data['ReplyPermalink']
|
105 |
+
}
|
106 |
+
comment["replies"].append(reply)
|
107 |
+
|
108 |
+
comments.append(comment)
|
109 |
+
|
110 |
+
yield post_id, {
|
111 |
+
"id": post_id,
|
112 |
+
"post_title": post_title,
|
113 |
+
"post_author": post_author,
|
114 |
+
"post_body": post_body,
|
115 |
+
"post_url": post_url,
|
116 |
+
"post_pic": post_pic,
|
117 |
+
"subreddit": subreddit,
|
118 |
+
"post_timestamp": post_timestamp,
|
119 |
+
"post_upvotes": post_upvotes,
|
120 |
+
"post_permalink": post_permalink,
|
121 |
+
"comments": comments
|
122 |
+
}
|