File size: 2,854 Bytes
ea6f77f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import csv
import pandas as pd

class CSVtoJSONTransformer:
    def __init__(self, csv_reader):
        self.csv_reader = csv_reader
    
    def transform_to_json(self):
        next(self.csv_reader)  # Skip header if present
        df = pd.DataFrame(self.csv_reader)  # Convert csv_reader to DataFrame
        df["Author"] = df["Author"].replace({pd.NA: None})
        df["Comment Body"] = df["Comment Body"].replace({pd.NA: None})

        transformed_data = []

        # Group the DataFrame by the 'Subreddit' column
        grouped_df = df.groupby('Subreddit')

        for subreddit, group in grouped_df:
            subreddit_data = {
                "Subreddit": subreddit,
                "Posts": []
            }
            
            # Dictionary to keep track of post titles and their corresponding post IDs
            post_title_to_id = {}

            for index, row in group.iterrows():
                post_title = row['Post Title']
                comment_body = row['Comment Body']
                timestamp = row['Timestamp']
                upvotes = row['Upvotes']
                ID = row['ID']
                author = row['Author']
                replies = row['Number of Replies']

                # Check if the post title already exists under the subreddit
                if post_title in post_title_to_id:
                    post_id = post_title_to_id[post_title]
                    # Append comment to existing post
                    for post in subreddit_data["Posts"]:
                        if post['PostID'] == post_id:
                            post['Comments'].append({
                                "CommentID": ID,
                                "Author": author,
                                "CommentBody": comment_body,
                                "Timestamp": timestamp,
                                "Upvotes": upvotes,
                                "NumberofReplies": replies
                            })
                            break
                else:
                    # If the post title does not exist, create a new entry
                    post_id = len(subreddit_data["Posts"]) + 1
                    post_title_to_id[post_title] = post_id
                    subreddit_data["Posts"].append({
                        "PostID": post_id,
                        "PostTitle": post_title,
                        "Comments": [{
                            "CommentID": ID,
                            "Author": author,
                            "CommentBody": comment_body,
                            "Timestamp": timestamp,
                            "Upvotes": upvotes,
                            "NumberofReplies": replies
                        }]
                    })

            transformed_data.append(subreddit_data)

        return transformed_data