Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

0.5.1 #11

Merged
merged 18 commits into from
Mar 17, 2023
Prev Previous commit
Next Next commit
Update db_manager.py
  • Loading branch information
Daethyra authored Mar 17, 2023
commit 17d3f45494ccaf2272a31e13f892c2c2d1b46643
39 changes: 22 additions & 17 deletions db_manager.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import sqlite3
from prettytable import PrettyTable
import itertools

def init_db():
conn = sqlite3.connect("sentiment_results.db")
conn = sqlite3.connect('sentiment_analysis.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS sentiment_results
(id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
sentiment TEXT NOT NULL,
violence_intent TEXT NOT NULL)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS results
(id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, sentiment TEXT, violence_intention TEXT)''')
conn.commit()
return conn

def save_sentiment_results(conn, results):
cursor = conn.cursor()
cursor.executemany("INSERT INTO sentiment_results (text, sentiment, violence_intent) VALUES (?, ?, ?)", results)
cursor.executemany('INSERT INTO results (text, sentiment, violence_intention) VALUES (?, ?, ?)', results)
conn.commit()

def print_db_results(conn):
cursor = conn.cursor()
cursor.execute("SELECT * FROM sentiment_results")
rows = cursor.fetchall()
table = PrettyTable(["ID", "Text", "Sentiment", "Violence Intent"])
for row in rows:
table.add_row(row)
print("╔══════════════════════════════════════════════════╗")
print("║ Sentiment Results ║")
print("╚══════════════════════════════════════════════════╝")
print(table)
cursor.execute('SELECT * FROM results')
results = cursor.fetchall()
print("|{:^4}|{:^50}|{:^10}|{:^15}|".format("ID", "Text", "Sentiment", "Violence Intention"))
print("-" * 82)
for row in results:
print("|{:^4}|{:^50}|{:^10}|{:^15}|".format(row[0], row[1][:50], row[2], row[3]))

def flag_dangerous_users(usernames, tweets, violence_intentions, threshold):
dangerous_users = []
tweet_violence_intentions = list(zip(usernames, tweets, violence_intentions))

for user, grouped_tweets in itertools.groupby(sorted(tweet_violence_intentions, key=lambda x: x[0]), key=lambda x: x[0]):
violent_tweets = sum(1 for tweet in grouped_tweets if tweet[2] == "Violent")
if violent_tweets >= threshold:
dangerous_users.append(user)

return dangerous_users