reddit_climate_comment / reddit_climate_comment.py
cathw's picture
Upload reddit_climate_comment.py
cdbcdbd verified
raw
history blame
5.25 kB
import csv
import json
import os
from datasets import GeneratorBasedBuilder, Features, Value, Sequence, SplitGenerator, BuilderConfig, DatasetInfo, Split
import logging
import pandas as pd
from typing import Dict
CITATION = ""
_DESCRIPTION = "Demo"
_URL = ""
_HOMEPAGE = ""
_LICENSE = ""
_URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.json.zip"
class NewDataset(GeneratorBasedBuilder):
def _info(self):
return DatasetInfo(
description=_DESCRIPTION,
features = Features({
"id": Value("string"),
"post_title": Value("string"),
"post_author": Value("string"),
"post_body": Value("string"),
"post_url": Value("string"),
"post_pic": Value("string"),
"subreddit": Value("string"),
"post_timestamp": Value("string"),
"post_upvotes": Value("int32"),
"post_permalink": Value("string"),
"comments": Sequence({
"CommentID": Value("string"),
"CommentAuthor": Value("string"),
"CommentBody": Value("string"),
"CommentTimestamp": Value("string"),
"CommentUpvotes": Value("int32"),
"CommentPermalink": Value("string"),
"Replies": Sequence({
"ReplyID": Value("string"),
"ReplyAuthor": Value("string"),
"ReplyBody": Value("string"),
"ReplyTimestamp": Value("string"),
"ReplyUpvotes": Value("int32"),
"ReplyPermalink": Value("string"),
})
})
}),
homepage=_HOMEPAGE,
)
def _split_generators(self, dl_manager):
path = dl_manager.download_and_extract(_URL)
train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.json"})
return [train_splits]
def _generate_examples(self, filepath):
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
for post in data['Posts']:
post_id = post['PostID']
post_title = post['PostTitle']
post_author = post['PostAuthor']
post_body = post['PostBody'] if post['PostBody'] != "" else None
post_url = post['PostUrl']
post_pic = post['PostPic'] if post['PostPic'] != "" else None
subreddit = post['Subreddit']
post_timestamp = post['PostTimestamp']
post_upvotes = int(post['PostUpvotes']) if post['PostUpvotes'] else None
post_permalink = post['PostPermalink']
comments = []
if 'comments' in post:
for comment_id, comment_author in zip(post['comments']['CommentID'], post['comments']['CommentAuthor']):
comment_body = post['comments'].get(comment_id, {}).get('CommentBody')
comment_timestamp = post['comments'].get(comment_id, {}).get('CommentTimestamp')
comment_upvotes = post['comments'].get(comment_id, {}).get('CommentUpvotes')
comment_permalink = post['comments'].get(comment_id, {}).get('CommentPermalink')
replies = []
for reply_id, reply_author in zip(post['comments'].get(comment_id, {}).get('Replies', {}).get('ReplyID', []), post['comments'].get(comment_id, {}).get('Replies', {}).get('ReplyAuthor', [])):
reply_body = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyBody')
reply_timestamp = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyTimestamp')
reply_upvotes = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyUpvotes')
reply_permalink = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyPermalink')
replies.append({
"ReplyID": reply_id,
"ReplyAuthor": reply_author,
"ReplyBody": reply_body,
"ReplyTimestamp": reply_timestamp,
"ReplyUpvotes": reply_upvotes,
"ReplyPermalink": reply_permalink
})
comments.append({
"CommentID": comment_id,
"CommentAuthor": comment_author,
"CommentBody": comment_body,
"CommentTimestamp": comment_timestamp,
"CommentUpvotes": comment_upvotes,
"CommentPermalink": comment_permalink,
"Replies": replies
})
yield post_id, {
"id": post_id,
"post_title": post_title,
"post_author": post_author,
"post_body": post_body,
"post_url": post_url,
"post_pic": post_pic,
"subreddit": subreddit,
"post_timestamp": post_timestamp,
"post_upvotes": post_upvotes,
"post_permalink": post_permalink,
"comments": comments
}