cathw commited on
Commit
4d1d596
·
verified ·
1 Parent(s): 0a0b00b

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +47 -46
reddit_climate_comment.py CHANGED
@@ -55,54 +55,55 @@ 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
- 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
- if (len(row["CommentID"]) != 0):
82
- for i in range(0,len(row["CommentID"])):
83
- comment_data = {
84
- "CommentID": row["CommentID"][i],
85
- "CommentAuthor": row["CommentAuthor"][i],
86
- "CommentBody": row["CommentBody"][i],
87
- "CommentTimestamp": row["CommentTimestamp"][i],
88
- "CommentUpvotes": int(row["CommentUpvotes"][i]),
89
- "CommentPermalink": row["CommentPermalink"][i],
90
- "replies": []
91
  }
92
- reply = {}
93
- reply_id = row["ReplyID"]
94
- if reply_id:
95
- for j in range(0,len(reply_id)):
96
- reply_data = {"ReplyID": reply_id[j],
97
- "ReplyAuthor": row["ReplyAuthor"][j],
98
- "ReplyBody": row["ReplyBody"][j],
99
- "ReplyTimestamp": row["ReplyTimestamp"][j],
100
- "ReplyUpvotes": int(row["ReplyUpvotes"][j]),
101
- "ReplyPermalink": row["ReplyPermalink"][j]
102
- }
103
- comment_data["replies"].append(reply_data)
104
- post_data["comments"].append(comment_data)
105
 
106
- for key, post_data in posts.items():
107
- yield key, post_data
 
 
 
 
 
 
 
 
 
 
 
108
 
 
55
 
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
 
 
 
58
 
59
+ for index, row in df.iterrows():
60
+ post_id = str(row['PostID'])
61
+ post_title = row['PostTitle']
62
+ post_author = row['PostAuthor']
63
+ post_body = row['PostBody']
64
+ post_url = row['PostUrl']
65
+ post_pic = row['PostPic']
66
+ subreddit = row['Subreddit']
67
+ post_timestamp = row['PostTimestamp']
68
+ post_upvotes = row['PostUpvotes']
69
+ post_permalink = row['PostPermalink']
70
+
71
+ comments = []
72
+ if not pd.isnull(row['CommentID']):
73
+ comment = {
74
+ "CommentID": str(row['CommentID']),
75
+ "CommentAuthor": row['CommentAuthor'],
76
+ "CommentBody": row['CommentBody'],
77
+ "CommentTimestamp": row['CommentTimestamp'],
78
+ "CommentUpvotes": row['CommentUpvotes'],
79
+ "CommentPermalink": row['CommentPermalink'],
80
+ "replies": []
81
  }
82
+ comments.append(comment)
83
+
84
+ # If there are replies, add them to the comment
85
+ if not pd.isnull(row['ReplyID']):
86
+ reply = {
87
+ "ReplyID": str(row['ReplyID']),
88
+ "ReplyAuthor": row['ReplyAuthor'],
89
+ "ReplyBody": row['ReplyBody'],
90
+ "ReplyTimestamp": row['ReplyTimestamp'],
91
+ "ReplyUpvotes": row['ReplyUpvotes'],
92
+ "ReplyPermalink": row['ReplyPermalink']
 
93
  }
94
+ comment["replies"].append(reply)
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ yield post_id, {
97
+ "id": post_id,
98
+ "post_title": post_title,
99
+ "post_author": post_author,
100
+ "post_body": post_body,
101
+ "post_url": post_url,
102
+ "post_pic": post_pic,
103
+ "subreddit": subreddit,
104
+ "post_timestamp": post_timestamp,
105
+ "post_upvotes": post_upvotes,
106
+ "post_permalink": post_permalink,
107
+ "comments": comments
108
+ }
109