cathw commited on
Commit
0a0b00b
1 Parent(s): 63c8e77

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +80 -91
reddit_climate_comment.py CHANGED
@@ -12,108 +12,97 @@ _URL = ""
12
  _HOMEPAGE = ""
13
  _LICENSE = ""
14
 
15
- _URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.json.zip"
16
 
17
  class NewDataset(GeneratorBasedBuilder):
18
  def _info(self):
19
  return DatasetInfo(
20
  description=_DESCRIPTION,
21
- features = Features({
22
- "id": Value("string"),
23
- "post_title": Value("string"),
24
- "post_author": Value("string"),
25
- "post_body": Value("string"),
26
- "post_url": Value("string"),
27
- "post_pic": Value("string"),
28
- "subreddit": Value("string"),
29
- "post_timestamp": Value("string"),
30
- "post_upvotes": Value("int32"),
31
- "post_permalink": Value("string"),
32
- "comments": Sequence({
33
- "CommentID": Value("string"),
34
- "CommentAuthor": Value("string"),
35
- "CommentBody": Value("string"),
36
- "CommentTimestamp": Value("string"),
37
- "CommentUpvotes": Value("int32"),
38
- "CommentPermalink": Value("string"),
39
- "Replies": Sequence({
40
- "ReplyID": Value("string"),
41
- "ReplyAuthor": Value("string"),
42
- "ReplyBody": Value("string"),
43
- "ReplyTimestamp": Value("string"),
44
- "ReplyUpvotes": Value("int32"),
45
- "ReplyPermalink": Value("string"),
46
- })
47
- })
48
- }),
49
  homepage=_HOMEPAGE,
50
  )
51
  def _split_generators(self, dl_manager):
52
  path = dl_manager.download_and_extract(_URL)
53
- train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.json"})
54
  return [train_splits]
 
55
  def _generate_examples(self, filepath):
56
- with open(filepath, "r", encoding="utf-8") as f:
57
- data = json.load(f)
58
-
59
- for post in data['Posts']:
60
- post_id = post['PostID']
61
- post_title = post['PostTitle']
62
- post_author = post['PostAuthor']
63
- post_body = post['PostBody'] if post['PostBody'] != "" else None
64
- post_url = post['PostUrl']
65
- post_pic = post['PostPic'] if post['PostPic'] != "" else None
66
- subreddit = post['Subreddit']
67
- post_timestamp = post['PostTimestamp']
68
- post_upvotes = int(post['PostUpvotes']) if post['PostUpvotes'] else None
69
- post_permalink = post['PostPermalink']
70
-
71
- examples = []
72
- for i in range(len(post['Comments'])):
73
- comment_id = post['Comments'][i]['CommentID']
74
- comment_author = post['Comments'][i]['CommentAuthor']
75
- comment_body = post['Comments'][i]['CommentBody'] if post['Comments'][i]['CommentBody'] != "" else None
76
- comment_timestamp = post['Comments'][i]['CommentTimestamp']
77
- comment_upvotes = int(post['Comments'][i]['CommentUpvotes']) if post['Comments'][i]['CommentUpvotes'] else None
78
- comment_permalink = post['Comments'][i]['CommentPermalink']
79
- replies = []
80
- for reply in post['Comments'][i].get('Replies', []):
81
- reply_id = reply['ReplyID']
82
- reply_author = reply['ReplyAuthor']
83
- reply_body = reply['ReplyBody'] if reply['ReplyBody'] != "" else None
84
- reply_timestamp = reply['ReplyTimestamp']
85
- reply_upvotes = int(reply['ReplyUpvotes']) if reply['ReplyUpvotes'] else None
86
- reply_permalink = reply['ReplyPermalink']
87
- replies.append({
88
- "ReplyID": reply_id,
89
- "ReplyAuthor": reply_author,
90
- "ReplyBody": reply_body,
91
- "ReplyTimestamp": reply_timestamp,
92
- "ReplyUpvotes": reply_upvotes,
93
- "ReplyPermalink": reply_permalink
94
- })
95
- comment_dict = {
96
- "CommentID": comment_id,
97
- "CommentAuthor": comment_author,
98
- "CommentBody": comment_body,
99
- "CommentTimestamp": comment_timestamp,
100
- "CommentUpvotes": comment_upvotes,
101
- "CommentPermalink": comment_permalink,
102
- "Replies": replies
103
  }
104
- examples.append(comment_dict)
105
 
106
- yield post_id, {
107
- "id": post_id,
108
- "post_title": post_title,
109
- "post_author": post_author,
110
- "post_body": post_body,
111
- "post_url": post_url,
112
- "post_pic": post_pic,
113
- "subreddit": subreddit,
114
- "post_timestamp": post_timestamp,
115
- "post_upvotes": post_upvotes,
116
- "post_permalink": post_permalink,
117
- "comments": examples
118
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
 
12
  _HOMEPAGE = ""
13
  _LICENSE = ""
14
 
15
+ _URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.csv.zip"
16
 
17
  class NewDataset(GeneratorBasedBuilder):
18
  def _info(self):
19
  return DatasetInfo(
20
  description=_DESCRIPTION,
21
+ features=Features({
22
+ "id": Value("string"),
23
+ "post_title": Value("string"),
24
+ "post_author": Value("string"),
25
+ "post_body": Value("string"),
26
+ "post_url": Value("string"),
27
+ "post_pic": Value("string"),
28
+ "subreddit": Value("string"),
29
+ "post_timestamp": Value("string"),
30
+ "post_upvotes": Value("int32"),
31
+ "post_permalink": Value("string"),
32
+ "comments": Sequence({
33
+ "CommentID": Value("string"),
34
+ "CommentAuthor": Value("string"),
35
+ "CommentBody": Value("string"),
36
+ "CommentTimestamp": Value("string"),
37
+ "CommentUpvotes": Value("int32"),
38
+ "CommentPermalink": Value("string"),
39
+ "replies": Sequence({
40
+ "ReplyID": Value("string"),
41
+ "ReplyAuthor": Value("string"),
42
+ "ReplyBody": Value("string"),
43
+ "ReplyTimestamp": Value("string"),
44
+ "ReplyUpvotes": Value("int32"),
45
+ "ReplyPermalink": Value("string"),
46
+ })
47
+ })
48
+ }),
49
  homepage=_HOMEPAGE,
50
  )
51
  def _split_generators(self, dl_manager):
52
  path = dl_manager.download_and_extract(_URL)
53
+ train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.csv"})
54
  return [train_splits]
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