cathw commited on
Commit
7e9cbee
1 Parent(s): c51ce3f

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +51 -72
reddit_climate_comment.py CHANGED
@@ -55,77 +55,56 @@ class NewDataset(GeneratorBasedBuilder):
55
 
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
58
-
59
- # Group the DataFrame by post ID
60
- grouped_df = df.groupby('PostID')
61
-
62
- for post_id, group in grouped_df:
63
- post_data = group.iloc[0] # Get the data for the post
64
-
65
- post_title = post_data['PostTitle']
66
- post_author = post_data['PostAuthor']
67
- post_body = post_data['PostBody']
68
- post_url = post_data['PostUrl']
69
- post_pic = post_data['PostPic']
70
- subreddit = post_data['Subreddit']
71
- post_timestamp = post_data['PostTimestamp']
72
- post_upvotes = post_data['PostUpvotes']
73
- post_permalink = post_data['PostPermalink']
74
-
75
- comments = []
76
-
77
- # Extract comment data
78
- for _, comment_row in group.iterrows():
79
- comment_id = str(comment_row['CommentID'])
80
-
81
- if pd.isna(comment_id): # Skip if CommentID is NaN
82
- continue
83
-
84
- comment = {
85
- "CommentID": comment_id,
86
- "CommentAuthor": comment_row['CommentAuthor'],
87
- "CommentBody": comment_row['CommentBody'],
88
- "CommentTimestamp": comment_row['CommentTimestamp'],
89
- "CommentUpvotes": comment_row['CommentUpvotes'],
90
- "CommentPermalink": comment_row['CommentPermalink'],
91
- "replies": [] # Initialize empty list for replies
92
  }
93
-
94
- # Check if there are replies for the current comment
95
- replies_data = df[df['CommentID'] == comment_id][['ReplyID', 'ReplyAuthor', 'ReplyBody', 'ReplyTimestamp', 'ReplyUpvotes', 'ReplyPermalink']]
96
- for _, reply_data in replies_data.iterrows():
97
- reply = {
98
- "ReplyID": str(reply_data['ReplyID']),
99
- "ReplyAuthor": reply_data['ReplyAuthor'],
100
- "ReplyBody": reply_data['ReplyBody'],
101
- "ReplyTimestamp": reply_data['ReplyTimestamp'],
102
- "ReplyUpvotes": reply_data['ReplyUpvotes'],
103
- "ReplyPermalink": reply_data['ReplyPermalink']
104
- }
105
- comment["replies"].append(reply)
106
-
107
- comments.append(comment)
108
-
109
- yield post_id, {
110
- "id": post_id,
111
- "post_title": post_title,
112
- "post_author": post_author,
113
- "post_body": post_body,
114
- "post_url": post_url,
115
- "post_pic": post_pic,
116
- "subreddit": subreddit,
117
- "post_timestamp": post_timestamp,
118
- "post_upvotes": post_upvotes,
119
- "post_permalink": post_permalink,
120
- "comments": comments
121
  }
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
 
 
 
 
 
 
 
 
 
 
 
 
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
+ posts = {}
63
+ for id_, row in df.iterrows():
64
+ post_id = row["PostID"]
65
+ if post_id not in posts:
66
+ post_data = {
67
+ "id": post_id,
68
+ "post_title": row["PostTitle"],
69
+ "post_author": row["PostAuthor"],
70
+ "post_body": row["PostBody"],
71
+ "post_url": row["PostUrl"],
72
+ "post_pic": row["PostPic"],
73
+ "subreddit": row["Subreddit"],
74
+ "post_timestamp": row["PostTimestamp"],
75
+ "post_upvotes": int(row["PostUpvotes"]),
76
+ "post_permalink": row["PostPermalink"],
77
+ "comments": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  }
79
+ posts[post_id] = post_data
80
+
81
+ comment_data = {
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
+ reply_id = row["ReplyID"]
93
+ if reply_id:
94
+ # Construct reply data dictionary
95
+ reply_data = {
96
+ "ReplyID": reply_id,
97
+ "ReplyAuthor": row["ReplyAuthor"],
98
+ "ReplyBody": row["ReplyBody"],
99
+ "ReplyTimestamp": row["ReplyTimestamp"],
100
+ "ReplyUpvotes": int(row["ReplyUpvotes"]),
101
+ "ReplyPermalink": row["ReplyPermalink"]
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
+ for key, post_data in posts.items():
110
+ yield key, post_data