Upload self-annotated_reddit_climate_comment.py
Browse files
self-annotated_reddit_climate_comment.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datasets import GeneratorBasedBuilder, Features, Value, Sequence, SplitGenerator, BuilderConfig, DatasetInfo, Split, Image
|
| 5 |
+
import logging
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from typing import Dict
|
| 8 |
+
|
| 9 |
+
CITATION = ""
|
| 10 |
+
_DESCRIPTION = "Demo"
|
| 11 |
+
_HOMEPAGE = "https://huggingface.co/datasets/SarcasmNet/self-annotated_reddit_climate_comment"
|
| 12 |
+
_LICENSE = "MIT"
|
| 13 |
+
|
| 14 |
+
_URL = "https://github.com/catherine-ywang/Reddit-Climate-Environment-Sarcasm-Self-Annotated-Data/raw/main/self_annotated_comments.csv"
|
| 15 |
+
|
| 16 |
+
class NewDataset(GeneratorBasedBuilder):
|
| 17 |
+
def _info(self):
|
| 18 |
+
return DatasetInfo(
|
| 19 |
+
description=_DESCRIPTION,
|
| 20 |
+
features=Features({
|
| 21 |
+
"id": Value("string"),
|
| 22 |
+
"post_title": Value("string"),
|
| 23 |
+
"post_author": Value("string"),
|
| 24 |
+
"post_body": Value("string"),
|
| 25 |
+
"post_url": Value("string"),
|
| 26 |
+
"post_pic": Image(),
|
| 27 |
+
"subreddit": Value("string"),
|
| 28 |
+
"post_timestamp": Value("string"),
|
| 29 |
+
"post_upvotes": Value("int32"),
|
| 30 |
+
"post_permalink": Value("string"),
|
| 31 |
+
"comments": Sequence({
|
| 32 |
+
"CommentID": Value("string"),
|
| 33 |
+
"CommentAuthor": Value("string"),
|
| 34 |
+
"CommentBody": Value("string"),
|
| 35 |
+
"CommentTimestamp": Value("string"),
|
| 36 |
+
"CommentUpvotes": Value("int32"),
|
| 37 |
+
"CommentPermalink": Value("string"),
|
| 38 |
+
"CommentLink": Value("int32")
|
| 39 |
+
})
|
| 40 |
+
}),
|
| 41 |
+
homepage=_HOMEPAGE,
|
| 42 |
+
)
|
| 43 |
+
def _split_generators(self, dl_manager):
|
| 44 |
+
path = dl_manager.download_and_extract(_URL)
|
| 45 |
+
train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/self_annotated_comments.csv"})
|
| 46 |
+
return [train_splits]
|
| 47 |
+
|
| 48 |
+
def _generate_examples(self, filepath):
|
| 49 |
+
df = pd.read_csv(filepath)
|
| 50 |
+
for column in df.columns:
|
| 51 |
+
df[column] = df[column].replace({pd.NA: None})
|
| 52 |
+
# Group the DataFrame by post ID
|
| 53 |
+
grouped_df = df.groupby('PostID')
|
| 54 |
+
|
| 55 |
+
for post_id, group in grouped_df:
|
| 56 |
+
post_data = group.iloc[0] # Get the data for the post
|
| 57 |
+
|
| 58 |
+
post_title = post_data['PostTitle']
|
| 59 |
+
post_author = post_data['PostAuthor']
|
| 60 |
+
post_body = post_data['PostBody']
|
| 61 |
+
post_url = post_data['PostUrl']
|
| 62 |
+
post_pic = post_data['PostPic']
|
| 63 |
+
subreddit = post_data['Subreddit']
|
| 64 |
+
post_timestamp = post_data['PostTimestamp']
|
| 65 |
+
post_upvotes = post_data['PostUpvotes']
|
| 66 |
+
post_permalink = post_data['PostPermalink']
|
| 67 |
+
|
| 68 |
+
comments = []
|
| 69 |
+
|
| 70 |
+
# Iterate over each unique comment ID
|
| 71 |
+
for comment_id in group['CommentID'].unique():
|
| 72 |
+
comment_data = group[group['CommentID'] == comment_id].iloc[0]
|
| 73 |
+
|
| 74 |
+
comment_author = comment_data['CommentAuthor']
|
| 75 |
+
comment_body = comment_data['CommentBody']
|
| 76 |
+
comment_timestamp = comment_data['CommentTimestamp']
|
| 77 |
+
comment_upvotes = comment_data['CommentUpvotes']
|
| 78 |
+
comment_permalink = comment_data['CommentPermalink']
|
| 79 |
+
comment_label = comment_data['CommentLabel']
|
| 80 |
+
|
| 81 |
+
# Add comment with its replies to the list
|
| 82 |
+
comment = {
|
| 83 |
+
"CommentID": comment_id,
|
| 84 |
+
"CommentAuthor": comment_author,
|
| 85 |
+
"CommentBody": comment_body,
|
| 86 |
+
"CommentTimestamp": comment_timestamp,
|
| 87 |
+
"CommentUpvotes": comment_upvotes,
|
| 88 |
+
"CommentPermalink": comment_permalink,
|
| 89 |
+
"CommentLabel": comment_label
|
| 90 |
+
}
|
| 91 |
+
comments.append(comment)
|
| 92 |
+
|
| 93 |
+
example = {
|
| 94 |
+
"id": post_id,
|
| 95 |
+
"post_title": post_title,
|
| 96 |
+
"post_author": post_author,
|
| 97 |
+
"post_body": post_body,
|
| 98 |
+
"post_url": post_url,
|
| 99 |
+
"post_pic": post_pic,
|
| 100 |
+
"subreddit": subreddit,
|
| 101 |
+
"post_timestamp": post_timestamp,
|
| 102 |
+
"post_upvotes": post_upvotes,
|
| 103 |
+
"post_permalink": post_permalink,
|
| 104 |
+
"comments": comments
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
yield post_id, example
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|