Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +65 -41
reddit_climate_comment.py
CHANGED
@@ -74,48 +74,71 @@ class NewDataset(GeneratorBasedBuilder):
|
|
74 |
|
75 |
comments = []
|
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 |
yield post_id, {
|
121 |
"id": post_id,
|
@@ -132,3 +155,4 @@ class NewDataset(GeneratorBasedBuilder):
|
|
132 |
}
|
133 |
|
134 |
|
|
|
|
74 |
|
75 |
comments = []
|
76 |
|
77 |
+
# Check if 'ParentCommentID' column exists
|
78 |
+
if 'ParentCommentID' in df.columns:
|
79 |
+
# Iterate over each unique comment ID
|
80 |
+
for comment_id in group['CommentID'].unique():
|
81 |
+
comment_data = group[group['CommentID'] == comment_id].iloc[0]
|
82 |
+
|
83 |
+
comment_author = comment_data['CommentAuthor']
|
84 |
+
comment_body = comment_data['CommentBody']
|
85 |
+
comment_timestamp = comment_data['CommentTimestamp']
|
86 |
+
comment_upvotes = comment_data['CommentUpvotes']
|
87 |
+
comment_permalink = comment_data['CommentPermalink']
|
88 |
+
|
89 |
+
# Get all replies for the current comment
|
90 |
+
replies = []
|
91 |
+
reply_group = df[df['ParentCommentID'] == comment_id]
|
92 |
+
for _, reply_data in reply_group.iterrows():
|
93 |
+
reply_id = reply_data['ReplyID']
|
94 |
+
reply_author = reply_data['ReplyAuthor']
|
95 |
+
reply_body = reply_data['ReplyBody']
|
96 |
+
reply_timestamp = reply_data['ReplyTimestamp']
|
97 |
+
reply_upvotes = reply_data['ReplyUpvotes']
|
98 |
+
reply_permalink = reply_data['ReplyPermalink']
|
99 |
+
|
100 |
+
reply = {
|
101 |
+
"ReplyID": reply_id,
|
102 |
+
"ReplyAuthor": reply_author,
|
103 |
+
"ReplyBody": reply_body,
|
104 |
+
"ReplyTimestamp": reply_timestamp,
|
105 |
+
"ReplyUpvotes": reply_upvotes,
|
106 |
+
"ReplyPermalink": reply_permalink
|
107 |
+
}
|
108 |
+
replies.append(reply)
|
109 |
+
|
110 |
+
# Add comment with its replies to the list
|
111 |
+
comment = {
|
112 |
+
"CommentID": comment_id,
|
113 |
+
"CommentAuthor": comment_author,
|
114 |
+
"CommentBody": comment_body,
|
115 |
+
"CommentTimestamp": comment_timestamp,
|
116 |
+
"CommentUpvotes": comment_upvotes,
|
117 |
+
"CommentPermalink": comment_permalink,
|
118 |
+
"replies": replies
|
119 |
}
|
120 |
+
comments.append(comment)
|
121 |
+
else:
|
122 |
+
# If 'ParentCommentID' column does not exist, assume no nested comments
|
123 |
+
for _, comment_data in group.iterrows():
|
124 |
+
comment_id = comment_data['CommentID']
|
125 |
+
comment_author = comment_data['CommentAuthor']
|
126 |
+
comment_body = comment_data['CommentBody']
|
127 |
+
comment_timestamp = comment_data['CommentTimestamp']
|
128 |
+
comment_upvotes = comment_data['CommentUpvotes']
|
129 |
+
comment_permalink = comment_data['CommentPermalink']
|
130 |
+
|
131 |
+
# Add comment without replies
|
132 |
+
comment = {
|
133 |
+
"CommentID": comment_id,
|
134 |
+
"CommentAuthor": comment_author,
|
135 |
+
"CommentBody": comment_body,
|
136 |
+
"CommentTimestamp": comment_timestamp,
|
137 |
+
"CommentUpvotes": comment_upvotes,
|
138 |
+
"CommentPermalink": comment_permalink,
|
139 |
+
"replies": [] # No replies for this comment
|
140 |
+
}
|
141 |
+
comments.append(comment)
|
142 |
|
143 |
yield post_id, {
|
144 |
"id": post_id,
|
|
|
155 |
}
|
156 |
|
157 |
|
158 |
+
|