cathw commited on
Commit
a5df15a
1 Parent(s): bff96c9

Upload reddit_climate_comment.py

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