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.csv.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.csv"}) return [train_splits] def _generate_examples(self, filepath): df = pd.read_csv(filepath) # Replace pd.NA with None for all columns for column in df.columns: df[column] = df[column].replace({pd.NA: None}) posts = {} for id_, row in df.iterrows(): post_id = row["PostID"] if post_id not in posts: post_data = { "id": post_id, "post_title": row["PostTitle"], "post_author": row["PostAuthor"], "post_body": row["PostBody"], "post_url": row["PostUrl"], "post_pic": row["PostPic"], "subreddit": row["Subreddit"], "post_timestamp": row["PostTimestamp"], "post_upvotes": int(row["PostUpvotes"]), "post_permalink": row["PostPermalink"], "comments": [] } posts[post_id] = post_data comment_data = { "CommentID": row["CommentID"], "CommentAuthor": row["CommentAuthor"], "CommentBody": row["CommentBody"], "CommentTimestamp": row["CommentTimestamp"], "CommentUpvotes": int(row["CommentUpvotes"]), "CommentPermalink": row["CommentPermalink"], "replies": [] } posts[post_id]["comments"].append(comment_data) reply_id = row["ReplyID"] if reply_id: # Construct reply data dictionary reply_data = { "ReplyID": reply_id, "ReplyAuthor": row["ReplyAuthor"], "ReplyBody": row["ReplyBody"], "ReplyTimestamp": row["ReplyTimestamp"], "ReplyUpvotes": int(row["ReplyUpvotes"]), "ReplyPermalink": row["ReplyPermalink"] } # Append reply data to replies list of corresponding comment for comment in posts[post_id]["comments"]: if comment["CommentID"] == row["CommentID"]: comment["replies"].append(reply_data) break for key, post_data in posts.items(): yield key, post_data