File size: 1,110 Bytes
58c2482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import requests
import io
from pymongo import MongoClient

#### **1. MongoDB Connection**
def get_mongo_client():
    client = MongoClient("mongodb+srv://groupA:[email protected]/?retryWrites=true&w=majority&appName=SentimentCluster")
    db = client["sentiment_db"]
    return db["tweets"]

#### **2. Insert Data If Collection is Empty**
def insert_data_if_empty():
    collection = get_mongo_client()

    if collection.count_documents({}) == 0:
        print("🟢 No data found. Inserting dataset...")

        csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"

        try:
            response = requests.get(csv_url)
            response.raise_for_status()  # Ensure request was successful
            df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")

            # Insert into MongoDB
            collection.insert_many(df.to_dict("records"))
            print("✅ Data Inserted into MongoDB!")
        except Exception as e:
            print(f"❌ Error loading dataset: {e}")