Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +120 -0
reddit_climate_comment.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datasets import GeneratorBasedBuilder, Features, Value, Sequence, SplitGenerator, BuilderConfig, DatasetInfo, Split
|
| 5 |
+
import logging
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from typing import Dict
|
| 8 |
+
|
| 9 |
+
CITATION = ""
|
| 10 |
+
_DESCRIPTION = "Demo"
|
| 11 |
+
_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 |
+
comments = []
|
| 72 |
+
for comment in post['Comments']:
|
| 73 |
+
comment_id = comment['CommentID']
|
| 74 |
+
comment_author = comment['CommentAuthor']
|
| 75 |
+
comment_body = comment['CommentBody'] if comment['CommentBody'] != "" else None
|
| 76 |
+
comment_timestamp = comment['CommentTimestamp']
|
| 77 |
+
comment_upvotes = int(comment['CommentUpvotes']) if comment['CommentUpvotes'] else None
|
| 78 |
+
comment_permalink = comment['CommentPermalink']
|
| 79 |
+
|
| 80 |
+
replies = []
|
| 81 |
+
for reply in comment.get('Replies', []):
|
| 82 |
+
reply_id = reply['ReplyID']
|
| 83 |
+
reply_author = reply['ReplyAuthor']
|
| 84 |
+
reply_body = reply['ReplyBody'] if reply['ReplyBody'] != "" else None
|
| 85 |
+
reply_timestamp = reply['ReplyTimestamp']
|
| 86 |
+
reply_upvotes = int(reply['ReplyUpvotes']) if reply['ReplyUpvotes'] else None
|
| 87 |
+
reply_permalink = reply['ReplyPermalink']
|
| 88 |
+
|
| 89 |
+
replies.append({
|
| 90 |
+
"ReplyID": reply_id,
|
| 91 |
+
"ReplyAuthor": reply_author,
|
| 92 |
+
"ReplyBody": reply_body,
|
| 93 |
+
"ReplyTimestamp": reply_timestamp,
|
| 94 |
+
"ReplyUpvotes": reply_upvotes,
|
| 95 |
+
"ReplyPermalink": reply_permalink
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
comments.append({
|
| 99 |
+
"CommentID": comment_id,
|
| 100 |
+
"CommentAuthor": comment_author,
|
| 101 |
+
"CommentBody": comment_body,
|
| 102 |
+
"CommentTimestamp": comment_timestamp,
|
| 103 |
+
"CommentUpvotes": comment_upvotes,
|
| 104 |
+
"CommentPermalink": comment_permalink,
|
| 105 |
+
"Replies": replies
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
yield post_id, {
|
| 109 |
+
"id": post_id,
|
| 110 |
+
"post_title": post_title,
|
| 111 |
+
"post_author": post_author,
|
| 112 |
+
"post_body": post_body,
|
| 113 |
+
"post_url": post_url,
|
| 114 |
+
"post_pic": post_pic,
|
| 115 |
+
"subreddit": subreddit,
|
| 116 |
+
"post_timestamp": post_timestamp,
|
| 117 |
+
"post_upvotes": post_upvotes,
|
| 118 |
+
"post_permalink": post_permalink,
|
| 119 |
+
"comments": comments
|
| 120 |
+
}
|