Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +32 -27
reddit_climate_comment.py
CHANGED
|
@@ -55,44 +55,48 @@ class NewDataset(GeneratorBasedBuilder):
|
|
| 55 |
|
| 56 |
def _generate_examples(self, filepath):
|
| 57 |
df = pd.read_csv(filepath)
|
|
|
|
| 58 |
|
| 59 |
-
for
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
comments = []
|
| 72 |
-
|
| 73 |
comment = {
|
| 74 |
-
"CommentID": str(
|
| 75 |
-
"CommentAuthor":
|
| 76 |
-
"CommentBody":
|
| 77 |
-
"CommentTimestamp":
|
| 78 |
-
"CommentUpvotes":
|
| 79 |
-
"CommentPermalink":
|
| 80 |
"replies": []
|
| 81 |
}
|
| 82 |
-
comments.append(comment)
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
|
|
|
| 86 |
reply = {
|
| 87 |
-
"ReplyID": str(
|
| 88 |
-
"ReplyAuthor":
|
| 89 |
-
"ReplyBody":
|
| 90 |
-
"ReplyTimestamp":
|
| 91 |
-
"ReplyUpvotes":
|
| 92 |
-
"ReplyPermalink":
|
| 93 |
}
|
| 94 |
comment["replies"].append(reply)
|
| 95 |
|
|
|
|
|
|
|
| 96 |
yield post_id, {
|
| 97 |
"id": post_id,
|
| 98 |
"post_title": post_title,
|
|
@@ -106,4 +110,5 @@ class NewDataset(GeneratorBasedBuilder):
|
|
| 106 |
"post_permalink": post_permalink,
|
| 107 |
"comments": comments
|
| 108 |
}
|
|
|
|
| 109 |
|
|
|
|
| 55 |
|
| 56 |
def _generate_examples(self, filepath):
|
| 57 |
df = pd.read_csv(filepath)
|
| 58 |
+
grouped_df = df.groupby('PostID')
|
| 59 |
|
| 60 |
+
for post_id, group in grouped_df:
|
| 61 |
+
post_data = group.iloc[0] # Get the data for the post
|
| 62 |
+
|
| 63 |
+
post_title = post_data['PostTitle']
|
| 64 |
+
post_author = post_data['PostAuthor']
|
| 65 |
+
post_body = post_data['PostBody']
|
| 66 |
+
post_url = post_data['PostUrl']
|
| 67 |
+
post_pic = post_data['PostPic']
|
| 68 |
+
subreddit = post_data['Subreddit']
|
| 69 |
+
post_timestamp = post_data['PostTimestamp']
|
| 70 |
+
post_upvotes = post_data['PostUpvotes']
|
| 71 |
+
post_permalink = post_data['PostPermalink']
|
| 72 |
|
| 73 |
comments = []
|
| 74 |
+
for index, comment_row in group.iterrows():
|
| 75 |
comment = {
|
| 76 |
+
"CommentID": str(comment_row['CommentID']),
|
| 77 |
+
"CommentAuthor": comment_row['CommentAuthor'],
|
| 78 |
+
"CommentBody": comment_row['CommentBody'],
|
| 79 |
+
"CommentTimestamp": comment_row['CommentTimestamp'],
|
| 80 |
+
"CommentUpvotes": comment_row['CommentUpvotes'],
|
| 81 |
+
"CommentPermalink": comment_row['CommentPermalink'],
|
| 82 |
"replies": []
|
| 83 |
}
|
|
|
|
| 84 |
|
| 85 |
+
# Add replies for the current comment
|
| 86 |
+
replies = group[group['CommentID'] == comment_row['CommentID']]
|
| 87 |
+
for _, reply_row in replies.iterrows():
|
| 88 |
reply = {
|
| 89 |
+
"ReplyID": str(reply_row['ReplyID']),
|
| 90 |
+
"ReplyAuthor": reply_row['ReplyAuthor'],
|
| 91 |
+
"ReplyBody": reply_row['ReplyBody'],
|
| 92 |
+
"ReplyTimestamp": reply_row['ReplyTimestamp'],
|
| 93 |
+
"ReplyUpvotes": reply_row['ReplyUpvotes'],
|
| 94 |
+
"ReplyPermalink": reply_row['ReplyPermalink']
|
| 95 |
}
|
| 96 |
comment["replies"].append(reply)
|
| 97 |
|
| 98 |
+
comments.append(comment)
|
| 99 |
+
|
| 100 |
yield post_id, {
|
| 101 |
"id": post_id,
|
| 102 |
"post_title": post_title,
|
|
|
|
| 110 |
"post_permalink": post_permalink,
|
| 111 |
"comments": comments
|
| 112 |
}
|
| 113 |
+
|
| 114 |
|