-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.py
More file actions
28 lines (19 loc) · 775 Bytes
/
Analysis.py
File metadata and controls
28 lines (19 loc) · 775 Bytes
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
import sqlite3
import os
import pandas as pd
from os.path import join, dirname
from dotenv import find_dotenv, load_dotenv
class Analysis:
def __init__(self):
envPath = join(dirname(__file__), '.env')
load_dotenv(envPath)
db_file = os.environ['db_file']
self.conn = sqlite3.connect(db_file)
def find_general_sentiment(self):
df = pd.read_sql_query("SELECT * from tweets", self.conn)
return df["polarity"].mean()
# Creates a new table in the DB which maps a user's tweet sentiments to the user
def generate_user_sentiment(self):
df = pd.read_sql_query("SELECT * from users LEFT JOIN tweets using (user_id)", self.conn)
new_df = df.groupby(["user_id", "name"])["polarity"].mean()
new_df.to_sql("user_sentiments", self.conn, if_exists="replace")